Skip to content

Commit 507b297

Browse files
Generate CBOR response protocol tests
1 parent aff6fd2 commit 507b297

File tree

65 files changed

+1916
-539
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

+1916
-539
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: 36 additions & 30 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,17 +121,18 @@ 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
{
129+
// TODO: update this method to read the stream in chunks incase the stream is larger than int.MaxValue
126130
// If we know the size, we can read directly into a buffer of exact size
127-
var buffer = new byte[streamSize.Value];
128-
int totalRead = 0;
131+
rentedBuffer = ArrayPool<byte>.Shared.Rent((int)streamSize.Value);
129132

130133
while (totalRead < streamSize.Value)
131134
{
132-
int bytesRead = stream.Read(buffer, totalRead, (int)(streamSize.Value - totalRead));
135+
int bytesRead = stream.Read(rentedBuffer, totalRead, (int)(streamSize.Value - totalRead));
133136
if (bytesRead == 0)
134137
{
135138
// If no bytes are read, it means we've reached the end of the stream before reading the whole streamSize.
@@ -138,53 +141,43 @@ public static CborReader CreateCborReaderFromStream(Stream stream, long? streamS
138141

139142
totalRead += bytesRead;
140143
}
141-
142-
return new CborReader(buffer);
143144
}
144-
145-
const int InitialBufferSize = 1024 * 8; // 8kb
146-
var tempBuffer = ArrayPool<byte>.Shared.Rent(InitialBufferSize);
147-
148-
try
145+
else
149146
{
150-
int totalRead = 0;
147+
const int InitialBufferSize = 1024 * 8; // 8kb
148+
rentedBuffer = ArrayPool<byte>.Shared.Rent(InitialBufferSize);
149+
151150
while (true)
152151
{
153-
int read = stream.Read(tempBuffer, totalRead, tempBuffer.Length - totalRead);
152+
int read = stream.Read(rentedBuffer, totalRead, rentedBuffer.Length - totalRead);
154153
if (read == 0)
155154
break;
156155

157156
totalRead += read;
158157

159-
if (totalRead == tempBuffer.Length)
158+
if (totalRead == rentedBuffer.Length)
160159
{
161160
// Expand the buffer size by doubling it
162-
var newBuffer = ArrayPool<byte>.Shared.Rent(tempBuffer.Length * 2);
161+
var newBuffer = ArrayPool<byte>.Shared.Rent(rentedBuffer.Length * 2);
163162
try
164163
{
165-
Buffer.BlockCopy(tempBuffer, 0, newBuffer, 0, totalRead);
164+
Buffer.BlockCopy(rentedBuffer, 0, newBuffer, 0, totalRead);
166165
}
167166
catch
168167
{
169168
ArrayPool<byte>.Shared.Return(newBuffer);
169+
ArrayPool<byte>.Shared.Return(rentedBuffer);
170+
rentedBuffer = null;
170171
throw;
171172
}
172-
ArrayPool<byte>.Shared.Return(tempBuffer);
173-
tempBuffer = newBuffer;
173+
ArrayPool<byte>.Shared.Return(rentedBuffer);
174+
rentedBuffer = newBuffer;
174175
}
175176
}
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);
187177
}
178+
179+
var actualBytes = new ReadOnlyMemory<byte>(rentedBuffer, 0, totalRead);
180+
return new CborReader(actualBytes);
188181
}
189182

190183
/// <summary>
@@ -206,5 +199,18 @@ public string PopPathSegment()
206199
{
207200
return _pathStack.Pop();
208201
}
202+
203+
protected override void Dispose(bool disposing)
204+
{
205+
if (!this.disposed)
206+
{
207+
if (disposing && rentedBuffer != null)
208+
{
209+
ArrayPool<byte>.Shared.Return(rentedBuffer);
210+
}
211+
disposed = true;
212+
}
213+
base.Dispose(disposing);
214+
}
209215
}
210216
}

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)