Skip to content

Commit 465b51f

Browse files
Generate CBOR response protocol tests
1 parent 2ece81a commit 465b51f

File tree

65 files changed

+1920
-546
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1920
-546
lines changed

extensions/src/AWSSDK.Extensions.CborProtocol/CborWriterExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
using System.Formats.Cbor;
1818
using Amazon.Util;
1919

20-
namespace AWSSDK.Extensions.CborProtocol
20+
namespace Amazon.Extensions.CborProtocol
2121
{
2222
public static class CborWriterExtensions
2323
{

extensions/src/AWSSDK.Extensions.CborProtocol/Internal/CborWriterPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
using System.Formats.Cbor;
1818
using System.Threading;
1919

20-
namespace AWSSDK.Extensions.CborProtocol.Internal
20+
namespace Amazon.Extensions.CborProtocol.Internal
2121
{
2222
public static class CborWriterPool
2323
{

extensions/src/AWSSDK.Extensions.CborProtocol/Internal/Transform/CborErrorResponseUnmarshaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
using System.Formats.Cbor;
2323
using System.IO;
2424

25-
namespace AWSSDK.Extensions.CborProtocol.Internal.Transform
25+
namespace Amazon.Extensions.CborProtocol.Internal.Transform
2626
{
2727
/// <summary>
2828
/// Class for unmarshalling CBOR service responses.

extensions/src/AWSSDK.Extensions.CborProtocol/Internal/Transform/CborMarshallerContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
using System.Formats.Cbor;
2424
using System.IO;
2525

26-
namespace AWSSDK.Extensions.CborProtocol.Internal.Transform
26+
namespace Amazon.Extensions.CborProtocol.Internal.Transform
2727
{
2828
public class CborMarshallerContext : MarshallerContext
2929
{

extensions/src/AWSSDK.Extensions.CborProtocol/Internal/Transform/CborResponseUnmarshaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
using System.IO;
2525
using System.Net;
2626

27-
namespace AWSSDK.Extensions.CborProtocol.Internal.Transform
27+
namespace Amazon.Extensions.CborProtocol.Internal.Transform
2828
{
2929
public abstract class CborResponseUnmarshaller : ResponseUnmarshaller
3030
{

extensions/src/AWSSDK.Extensions.CborProtocol/Internal/Transform/CborSimpleTypeUnmarshaller.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
using Amazon.Runtime.Internal.Util;
2323
using Amazon.Util;
2424

25-
namespace AWSSDK.Extensions.CborProtocol.Internal.Transform
25+
namespace Amazon.Extensions.CborProtocol.Internal.Transform
2626
{
2727
static class CborSimpleTypeUnmarshaller<T>
2828
{
@@ -45,7 +45,7 @@ public static T Unmarshall(CborUnmarshallerContext context)
4545
)
4646
{
4747
reader.ReadNull();
48-
value = null;
48+
value = default(T);
4949
}
5050
else if (typeof(T) == typeof(string))
5151
value = reader.ReadTextString();

extensions/src/AWSSDK.Extensions.CborProtocol/Internal/Transform/CborUnmarshallerContext.cs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
using Amazon.Runtime.Internal.Transform;
2525
using Amazon.Runtime.Internal.Util;
2626

27-
namespace AWSSDK.Extensions.CborProtocol.Internal.Transform
27+
namespace Amazon.Extensions.CborProtocol.Internal.Transform
2828
{
2929
public class CborUnmarshallerContext : UnmarshallerContext
3030
{
31+
private bool disposed = false;
32+
private byte[] rentedBuffer = null;
3133
private readonly Stack<string> _pathStack = new Stack<string>();
3234

3335
// There isn't a direct way to check if the reader is at the start of the document,
@@ -119,13 +121,13 @@ out long contentLength
119121
);
120122
}
121123

122-
public static CborReader CreateCborReaderFromStream(Stream stream, long? streamSize = null)
124+
private CborReader CreateCborReaderFromStream(Stream stream, long? streamSize = null)
123125
{
126+
int totalRead = 0;
124127
if (streamSize.HasValue)
125128
{
126129
// If we know the size, we can read directly into a buffer of exact size
127130
var buffer = new byte[streamSize.Value];
128-
int totalRead = 0;
129131

130132
while (totalRead < streamSize.Value)
131133
{
@@ -143,48 +145,36 @@ public static CborReader CreateCborReaderFromStream(Stream stream, long? streamS
143145
}
144146

145147
const int InitialBufferSize = 1024 * 8; // 8kb
146-
var tempBuffer = ArrayPool<byte>.Shared.Rent(InitialBufferSize);
148+
rentedBuffer = ArrayPool<byte>.Shared.Rent(InitialBufferSize);
147149

148-
try
150+
while (true)
149151
{
150-
int totalRead = 0;
151-
while (true)
152-
{
153-
int read = stream.Read(tempBuffer, totalRead, tempBuffer.Length - totalRead);
154-
if (read == 0)
155-
break;
152+
int read = stream.Read(rentedBuffer, totalRead, rentedBuffer.Length - totalRead);
153+
if (read == 0)
154+
break;
156155

157-
totalRead += read;
156+
totalRead += read;
158157

159-
if (totalRead == tempBuffer.Length)
158+
if (totalRead == rentedBuffer.Length)
159+
{
160+
// Expand the buffer size by doubling it
161+
var newBuffer = ArrayPool<byte>.Shared.Rent(rentedBuffer.Length * 2);
162+
try
163+
{
164+
Buffer.BlockCopy(rentedBuffer, 0, newBuffer, 0, totalRead);
165+
}
166+
catch
160167
{
161-
// Expand the buffer size by doubling it
162-
var newBuffer = ArrayPool<byte>.Shared.Rent(tempBuffer.Length * 2);
163-
try
164-
{
165-
Buffer.BlockCopy(tempBuffer, 0, newBuffer, 0, totalRead);
166-
}
167-
catch
168-
{
169-
ArrayPool<byte>.Shared.Return(newBuffer);
170-
throw;
171-
}
172-
ArrayPool<byte>.Shared.Return(tempBuffer);
173-
tempBuffer = newBuffer;
168+
ArrayPool<byte>.Shared.Return(newBuffer);
169+
throw;
174170
}
171+
ArrayPool<byte>.Shared.Return(rentedBuffer);
172+
rentedBuffer = newBuffer;
175173
}
176-
177-
// Create a new byte array to hold only the read data.
178-
var actualBytes = new byte[totalRead];
179-
Buffer.BlockCopy(tempBuffer, 0, actualBytes, 0, totalRead);
180-
181-
return new CborReader(actualBytes);
182-
}
183-
finally
184-
{
185-
// Return the buffer to the pool when done
186-
ArrayPool<byte>.Shared.Return(tempBuffer);
187174
}
175+
176+
var actualBytes = new ReadOnlyMemory<byte>(rentedBuffer, 0, totalRead);
177+
return new CborReader(actualBytes);
188178
}
189179

190180
/// <summary>
@@ -206,5 +196,18 @@ public string PopPathSegment()
206196
{
207197
return _pathStack.Pop();
208198
}
199+
200+
protected override void Dispose(bool disposing)
201+
{
202+
if (!this.disposed)
203+
{
204+
if (disposing && rentedBuffer != null)
205+
{
206+
ArrayPool<byte>.Shared.Return(rentedBuffer);
207+
}
208+
disposed = true;
209+
}
210+
base.Dispose(disposing);
211+
}
209212
}
210213
}

extensions/src/AWSSDK.Extensions.CborProtocol/Internal/Transform/ICborErrorResponseUnmarshaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
using Amazon.Runtime.Internal;
1717

18-
namespace AWSSDK.Extensions.CborProtocol.Internal.Transform
18+
namespace Amazon.Extensions.CborProtocol.Internal.Transform
1919
{
2020
/// <summary>
2121
/// The interface for unmarshalling a cbor error response.

extensions/src/AWSSDK.Extensions.CborProtocol/Internal/Transform/ICborUnmarshaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
namespace AWSSDK.Extensions.CborProtocol.Internal.Transform
16+
namespace Amazon.Extensions.CborProtocol.Internal.Transform
1717
{
1818
/// <summary>
1919
/// Interface for unmarshallers which unmarshall objects from response data.

extensions/test/CborProtocol.Tests/WriteDateTimeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Formats.Cbor;
33
using Xunit;
4-
using AWSSDK.Extensions.CborProtocol;
4+
using Amazon.Extensions.CborProtocol;
55

66
namespace Amazon.CborProtocol.Tests;
77

0 commit comments

Comments
 (0)