public async Task ReadStreamMessageAsync_MessageSplitAcrossReadsWithAdditionalData_ExamineMessageOnly() { // Arrange var emptyMessage = new byte[] { 0x00, // compression = 0 0x00, 0x00, 0x00, 0x00, // length = 0 0x00, // compression = 0 }; var followingMessage = new byte[] { 0x00, 0x00, 0x00, 0x00, // length = 0 0x00, // extra data }; var requestStream = new SyncPointMemoryStream(runContinuationsAsynchronously: false); var pipeReader = new TestPipeReader(PipeReader.Create(requestStream)); var testServerCallContext = HttpContextServerCallContextHelper.CreateServerCallContext(); // Act 1 var messageData1Task = pipeReader.ReadStreamMessageAsync(testServerCallContext, TestDataMarshaller.ContextualDeserializer).AsTask(); await requestStream.AddDataAndWait(emptyMessage).DefaultTimeout(); // Assert 1 Assert.AreEqual(0, (await messageData1Task.DefaultTimeout()) !.Span.Length); Assert.AreEqual(5, pipeReader.Consumed); Assert.AreEqual(5, pipeReader.Examined); // Act 2 var messageData2Task = pipeReader.ReadStreamMessageAsync(testServerCallContext, TestDataMarshaller.ContextualDeserializer).AsTask(); await requestStream.AddDataAndWait(followingMessage).DefaultTimeout(); // Assert 2 Assert.AreEqual(0, (await messageData2Task.DefaultTimeout()) !.Span.Length); Assert.AreEqual(10, pipeReader.Consumed); Assert.AreEqual(10, pipeReader.Examined); // Act 3 var messageData3Task = pipeReader.ReadStreamMessageAsync(testServerCallContext, TestDataMarshaller.ContextualDeserializer).AsTask(); await requestStream.AddDataAndWait(Array.Empty <byte>()).DefaultTimeout(); // Assert 3 var ex = await ExceptionAssert.ThrowsAsync <RpcException>(() => messageData3Task).DefaultTimeout(); Assert.AreEqual("Incomplete message.", ex.Status.Detail); Assert.AreEqual(10, pipeReader.Consumed); Assert.AreEqual(11, pipeReader.Examined); // Examined ahead to ask for more data }
public async Task ReadStreamMessageAsync_MultipleEmptyMessages_ReturnNoDataMessageThenComplete() { // Arrange var emptyMessage = new byte[] { 0x00, // compression = 0 0x00, 0x00, 0x00, 0x00 // length = 0 }; var ms = new MemoryStream(emptyMessage.Concat(emptyMessage).ToArray()); var pipeReader = new TestPipeReader(PipeReader.Create(ms)); var testServerCallContext = HttpContextServerCallContextHelper.CreateServerCallContext(); // Act 1 var messageData1 = await pipeReader.ReadStreamMessageAsync(testServerCallContext, TestDataMarshaller.ContextualDeserializer); // Assert 1 Assert.AreEqual(0, messageData1 !.Span.Length); Assert.AreEqual(emptyMessage.Length, pipeReader.Consumed); // Act 2 var messageData2 = await pipeReader.ReadStreamMessageAsync(testServerCallContext, TestDataMarshaller.ContextualDeserializer); // Assert 2 Assert.AreEqual(0, messageData2 !.Span.Length); Assert.AreEqual(emptyMessage.Length * 2, pipeReader.Consumed); // Act 3 var messageData3 = await pipeReader.ReadStreamMessageAsync(testServerCallContext, TestDataMarshaller.ContextualDeserializer); // Assert 3 Assert.IsNull(messageData3); Assert.AreEqual(emptyMessage.Length * 2, pipeReader.Consumed); }
public async Task ReadStreamMessageAsync_MultipleMessages_Success() { // Arrange var data = Encoding.UTF8.GetBytes("AAAAAAYKBHRlc3Q=AAAAAAYKBHRlc3Q=AAAAAAYKBHRlc3Q=AAAAAAYKBHRlc3Q=AAAAAAYKBHRlc3Q="); var ms = new MemoryStream(data); var pipeReader = new TestPipeReader(new Base64PipeReader(PipeReader.Create(ms))); // Act 1 var messageData = await pipeReader.ReadStreamMessageAsync(HttpContextServerCallContextHelper.CreateServerCallContext(), MarshallerEchoResponse.ContextualDeserializer); // Assert 1 Assert.AreEqual("test", messageData !.Message); Assert.AreEqual(11, pipeReader.Consumed); // Act 2 messageData = await pipeReader.ReadStreamMessageAsync(HttpContextServerCallContextHelper.CreateServerCallContext(), MarshallerEchoResponse.ContextualDeserializer); // Assert 2 Assert.AreEqual("test", messageData !.Message); Assert.AreEqual(22, pipeReader.Consumed); // Act 3 messageData = await pipeReader.ReadStreamMessageAsync(HttpContextServerCallContextHelper.CreateServerCallContext(), MarshallerEchoResponse.ContextualDeserializer); // Assert 3 Assert.AreEqual("test", messageData !.Message); Assert.AreEqual(33, pipeReader.Consumed); // Act 4 messageData = await pipeReader.ReadStreamMessageAsync(HttpContextServerCallContextHelper.CreateServerCallContext(), MarshallerEchoResponse.ContextualDeserializer); // Assert 4 Assert.AreEqual("test", messageData !.Message); Assert.AreEqual(44, pipeReader.Consumed); // Act 5 messageData = await pipeReader.ReadStreamMessageAsync(HttpContextServerCallContextHelper.CreateServerCallContext(), MarshallerEchoResponse.ContextualDeserializer); // Assert 5 Assert.AreEqual("test", messageData !.Message); Assert.AreEqual(55, pipeReader.Consumed); // Act 6 messageData = await pipeReader.ReadStreamMessageAsync(HttpContextServerCallContextHelper.CreateServerCallContext(), MarshallerEchoResponse.ContextualDeserializer); // Assert 6 Assert.IsNull(messageData); }