|
|
|
@ -17,9 +17,6 @@ |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
using System; |
|
|
|
|
using System.Buffers; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Runtime.InteropServices; |
|
|
|
|
using Grpc.Core; |
|
|
|
|
using Grpc.Core.Internal; |
|
|
|
|
using Grpc.Core.Utils; |
|
|
|
@ -32,23 +29,29 @@ namespace Grpc.Core.Internal.Tests |
|
|
|
|
[TestCase] |
|
|
|
|
public void CompleteAllowedOnlyOnce() |
|
|
|
|
{ |
|
|
|
|
var context = new DefaultSerializationContext(); |
|
|
|
|
using (var scope = NewDefaultSerializationContextScope()) |
|
|
|
|
{ |
|
|
|
|
var context = scope.Context; |
|
|
|
|
var buffer = GetTestBuffer(10); |
|
|
|
|
|
|
|
|
|
context.Complete(buffer); |
|
|
|
|
Assert.Throws(typeof(InvalidOperationException), () => context.Complete(buffer)); |
|
|
|
|
Assert.Throws(typeof(InvalidOperationException), () => context.Complete()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[TestCase] |
|
|
|
|
public void CompleteAllowedOnlyOnce2() |
|
|
|
|
{ |
|
|
|
|
var context = new DefaultSerializationContext(); |
|
|
|
|
using (var scope = NewDefaultSerializationContextScope()) |
|
|
|
|
{ |
|
|
|
|
var context = scope.Context; |
|
|
|
|
|
|
|
|
|
context.Complete(); |
|
|
|
|
Assert.Throws(typeof(InvalidOperationException), () => context.Complete(GetTestBuffer(10))); |
|
|
|
|
Assert.Throws(typeof(InvalidOperationException), () => context.Complete()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[TestCase(0)] |
|
|
|
|
[TestCase(1)] |
|
|
|
@ -57,7 +60,9 @@ namespace Grpc.Core.Internal.Tests |
|
|
|
|
[TestCase(1000)] |
|
|
|
|
public void ByteArrayPayload(int payloadSize) |
|
|
|
|
{ |
|
|
|
|
var context = new DefaultSerializationContext(); |
|
|
|
|
using (var scope = NewDefaultSerializationContextScope()) |
|
|
|
|
{ |
|
|
|
|
var context = scope.Context; |
|
|
|
|
var origPayload = GetTestBuffer(payloadSize); |
|
|
|
|
|
|
|
|
|
context.Complete(origPayload); |
|
|
|
@ -65,6 +70,7 @@ namespace Grpc.Core.Internal.Tests |
|
|
|
|
var nativePayload = context.GetPayload().ToByteArray(); |
|
|
|
|
CollectionAssert.AreEqual(origPayload, nativePayload); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[TestCase(0)] |
|
|
|
|
[TestCase(1)] |
|
|
|
@ -73,7 +79,9 @@ namespace Grpc.Core.Internal.Tests |
|
|
|
|
[TestCase(1000)] |
|
|
|
|
public void BufferWriter_OneSegment(int payloadSize) |
|
|
|
|
{ |
|
|
|
|
var context = new DefaultSerializationContext(); |
|
|
|
|
using (var scope = NewDefaultSerializationContextScope()) |
|
|
|
|
{ |
|
|
|
|
var context = scope.Context; |
|
|
|
|
var origPayload = GetTestBuffer(payloadSize); |
|
|
|
|
|
|
|
|
|
var bufferWriter = context.GetBufferWriter(); |
|
|
|
@ -84,6 +92,28 @@ namespace Grpc.Core.Internal.Tests |
|
|
|
|
var nativePayload = context.GetPayload().ToByteArray(); |
|
|
|
|
CollectionAssert.AreEqual(origPayload, nativePayload); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[TestCase(0)] |
|
|
|
|
[TestCase(1)] |
|
|
|
|
[TestCase(10)] |
|
|
|
|
[TestCase(100)] |
|
|
|
|
[TestCase(1000)] |
|
|
|
|
public void BufferWriter_OneSegment_GetMemory(int payloadSize) |
|
|
|
|
{ |
|
|
|
|
using (var scope = NewDefaultSerializationContextScope()) |
|
|
|
|
{ |
|
|
|
|
var context = scope.Context; |
|
|
|
|
var origPayload = GetTestBuffer(payloadSize); |
|
|
|
|
|
|
|
|
|
var bufferWriter = context.GetBufferWriter(); |
|
|
|
|
origPayload.AsSpan().CopyTo(bufferWriter.GetMemory(payloadSize).Span); |
|
|
|
|
bufferWriter.Advance(payloadSize); |
|
|
|
|
|
|
|
|
|
var nativePayload = context.GetPayload().ToByteArray(); |
|
|
|
|
CollectionAssert.AreEqual(origPayload, nativePayload); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[TestCase(1, 4)] // small slice size tests grpc_slice with inline data |
|
|
|
|
[TestCase(10, 4)] |
|
|
|
@ -95,7 +125,9 @@ namespace Grpc.Core.Internal.Tests |
|
|
|
|
[TestCase(1000, 64)] |
|
|
|
|
public void BufferWriter_MultipleSegments(int payloadSize, int maxSliceSize) |
|
|
|
|
{ |
|
|
|
|
var context = new DefaultSerializationContext(); |
|
|
|
|
using (var scope = NewDefaultSerializationContextScope()) |
|
|
|
|
{ |
|
|
|
|
var context = scope.Context; |
|
|
|
|
var origPayload = GetTestBuffer(payloadSize); |
|
|
|
|
|
|
|
|
|
var bufferWriter = context.GetBufferWriter(); |
|
|
|
@ -112,24 +144,49 @@ namespace Grpc.Core.Internal.Tests |
|
|
|
|
|
|
|
|
|
var nativePayload = context.GetPayload().ToByteArray(); |
|
|
|
|
CollectionAssert.AreEqual(origPayload, nativePayload); |
|
|
|
|
|
|
|
|
|
context.GetPayload().Dispose(); // TODO: do it better.. (use the scope...) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[TestCase] |
|
|
|
|
public void ContextIsReusable() |
|
|
|
|
{ |
|
|
|
|
using (var scope = NewDefaultSerializationContextScope()) |
|
|
|
|
{ |
|
|
|
|
var context = scope.Context; |
|
|
|
|
|
|
|
|
|
// AdjustTailSpace(0) if previous tail size is 0.... |
|
|
|
|
var origPayload1 = GetTestBuffer(10); |
|
|
|
|
context.Complete(origPayload1); |
|
|
|
|
CollectionAssert.AreEqual(origPayload1, context.GetPayload().ToByteArray()); |
|
|
|
|
|
|
|
|
|
// test that context.Complete() call is required... |
|
|
|
|
context.Reset(); |
|
|
|
|
|
|
|
|
|
var origPayload2 = GetTestBuffer(20); |
|
|
|
|
|
|
|
|
|
var bufferWriter = context.GetBufferWriter(); |
|
|
|
|
origPayload2.AsSpan().CopyTo(bufferWriter.GetMemory(origPayload2.Length).Span); |
|
|
|
|
bufferWriter.Advance(origPayload2.Length); |
|
|
|
|
CollectionAssert.AreEqual(origPayload2, context.GetPayload().ToByteArray()); |
|
|
|
|
|
|
|
|
|
// BufferWriter.GetMemory... (add refs to the original memory?) |
|
|
|
|
context.Reset(); |
|
|
|
|
|
|
|
|
|
// TODO: set payload and then get IBufferWriter should throw? |
|
|
|
|
// TODO: that's should be a null payload... |
|
|
|
|
CollectionAssert.AreEqual(new byte[0], context.GetPayload().ToByteArray()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: test Reset()... |
|
|
|
|
//test ideas: |
|
|
|
|
|
|
|
|
|
// TODO: destroy SliceBufferSafeHandles... (use usagescope...) |
|
|
|
|
// test that context.Complete() call is required... |
|
|
|
|
|
|
|
|
|
// set payload with Complete([]) and then get IBufferWriter should throw (because it's been completed already?) |
|
|
|
|
|
|
|
|
|
// other ideas: |
|
|
|
|
// AdjustTailSpace(0) if previous tail size is 0... (better for SliceBufferSafeHandle) |
|
|
|
|
|
|
|
|
|
private DefaultSerializationContext.UsageScope NewDefaultSerializationContextScope() |
|
|
|
|
{ |
|
|
|
|
return new DefaultSerializationContext.UsageScope(new DefaultSerializationContext()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private byte[] GetTestBuffer(int length) |
|
|
|
|
{ |
|
|
|
|