public async Task StreamedMessage_SuccessResponseAfterMessageReceived() { // Arrange var requestMessage = new HelloRequest { Name = "World" }; var ms = new MemoryStream(); MessageHelpers.WriteMessage(ms, requestMessage); var streamingContent = new StreamingContent(); var httpRequest = GrpcHttpHelper.Create("Greet.Greeter/SayHello"); httpRequest.Content = streamingContent; // Act var responseTask = Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead); // Assert Assert.IsFalse(responseTask.IsCompleted, "Server should wait for client to finish streaming"); var requestStream = await streamingContent.GetRequestStreamAsync().DefaultTimeout(); await requestStream.WriteAsync(ms.ToArray()).AsTask().DefaultTimeout(); streamingContent.Complete(); var response = await responseTask.DefaultTimeout(); var responseMessage = await response.GetSuccessfulGrpcMessageAsync <HelloReply>().DefaultTimeout(); Assert.AreEqual("Hello World", responseMessage.Message); }
public async Task AdditionalDataAfterStreamedMessage_ErrorResponse() { // Arrange SetExpectedErrorsFilter(writeContext => { if (writeContext.LoggerName == TestConstants.ServerCallHandlerTestName && writeContext.EventId.Name == "RpcConnectionError" && writeContext.State.ToString() == "Error status code 'Internal' raised.") { return(true); } if (writeContext.LoggerName == TestConstants.ServerCallHandlerTestName && writeContext.EventId.Name == "ErrorReadingMessage" && writeContext.State.ToString() == "Error reading message.") { return(true); } return(false); }); var requestMessage = new HelloRequest { Name = "World" }; var ms = new MemoryStream(); MessageHelpers.WriteMessage(ms, requestMessage); var streamingContent = new StreamingContent(); var httpRequest = GrpcHttpHelper.Create("Greet.Greeter/SayHello"); httpRequest.Content = streamingContent; // Act var responseTask = Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead); // Assert Assert.IsFalse(responseTask.IsCompleted, "Server should wait for client to finish streaming"); var requestStream = await streamingContent.GetRequestStreamAsync().DefaultTimeout(); await requestStream.WriteAsync(ms.ToArray()).AsTask().DefaultTimeout(); await requestStream.WriteAsync(ms.ToArray()).AsTask().DefaultTimeout(); streamingContent.Complete(); var response = await responseTask.DefaultTimeout(); // Read to end of response so headers are available await response.Content.CopyToAsync(new MemoryStream()).DefaultTimeout(); response.AssertTrailerStatus(StatusCode.Internal, "Additional data after the message received."); }
public async Task MultipleMessagesFromOneClient_SuccessResponses() { // Arrange var ms = new MemoryStream(); MessageHelpers.WriteMessage(ms, new ChatMessage { Name = "John", Message = "Hello Jill" }); var streamingContent = new StreamingContent(); var httpRequest = GrpcHttpHelper.Create("Chat.Chatter/Chat"); httpRequest.Content = streamingContent; // Act var responseTask = Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead); // Assert Assert.IsFalse(responseTask.IsCompleted, "Server should wait for first message from client"); var requestStream = await streamingContent.GetRequestStreamAsync().DefaultTimeout(); await requestStream.WriteAsync(ms.ToArray()).AsTask().DefaultTimeout(); await requestStream.FlushAsync().DefaultTimeout(); var response = await responseTask.DefaultTimeout(); response.AssertIsSuccessfulGrpcRequest(); var responseStream = await response.Content.ReadAsStreamAsync().DefaultTimeout(); var pipeReader = PipeReader.Create(responseStream); var message1Task = MessageHelpers.AssertReadStreamMessageAsync <ChatMessage>(pipeReader); var message1 = await message1Task.DefaultTimeout(); Assert.AreEqual("John", message1.Name); Assert.AreEqual("Hello Jill", message1.Message); var message2Task = MessageHelpers.AssertReadStreamMessageAsync <ChatMessage>(pipeReader); Assert.IsFalse(message2Task.IsCompleted, "Server is waiting for messages from client"); ms = new MemoryStream(); MessageHelpers.WriteMessage(ms, new ChatMessage { Name = "Jill", Message = "Hello John" }); await requestStream.WriteAsync(ms.ToArray()).AsTask().DefaultTimeout(); await requestStream.FlushAsync().DefaultTimeout(); var message2 = await message2Task.DefaultTimeout(); Assert.AreEqual("Jill", message2.Name); Assert.AreEqual("Hello John", message2.Message); var finishedTask = MessageHelpers.AssertReadStreamMessageAsync <ChatMessage>(pipeReader); Assert.IsFalse(finishedTask.IsCompleted, "Server is waiting for client to end streaming"); // Complete request stream streamingContent.Complete(); await finishedTask.DefaultTimeout(); response.AssertTrailerStatus(); }