public async Task StreamingSession_SendRequest_ReceiveResponse(int streamLength, int streamCount, int chunkCount) { // Arrange var request = new StreamingRequest() { Verb = "GET", Path = "api/version", Streams = new List <ResponseMessageStream>() }; request.AddStream(new StringContent("Hello human, I'm Bender!")); var requestHandler = new Mock <RequestHandler>(); var requestCompletionSource = new TaskCompletionSource <bool>(); requestHandler .Setup(r => r.ProcessRequestAsync(It.IsAny <ReceiveRequest>(), null, null, CancellationToken.None)) .ReturnsAsync(() => new StreamingResponse() { StatusCode = 200 }) .Callback(() => requestCompletionSource.SetResult(true)); var transportHandler = new Mock <TransportHandler>(new Mock <IDuplexPipe>().Object, NullLogger.Instance); var responseCompletionSource = new TaskCompletionSource <bool>(); var transportHandlerSetup = transportHandler.Setup(t => t.SendRequestAsync(It.IsAny <Guid>(), It.IsAny <RequestModel>(), CancellationToken.None)); var session = new StreamingSession(requestHandler.Object, transportHandler.Object, NullLogger.Instance); Header responseHeader = null; ReceiveResponse response = null; transportHandlerSetup.Callback( (Guid requestId, RequestModel requestPayload, CancellationToken cancellationToken) => { responseHeader = new Header() { Id = requestId, Type = PayloadTypes.Response }; response = new ReceiveResponse() { StatusCode = 200, Streams = StreamingDataGenerator.CreateStreams(requestId, streamLength, streamCount, chunkCount, PayloadTypes.Response) }; session.ReceiveResponse(responseHeader, response); foreach (AugmentedStreamDefinition definition in response.Streams) { var chunkList = definition.Chunks; for (int i = 0; i < chunkCount; i++) { bool isLast = i == chunkCount - 1; session.ReceiveStream( new Header() { End = isLast, Id = definition.Id, PayloadLength = chunkList[i].Length, Type = PayloadTypes.Stream }, chunkList[i]); } } }); // Act var responseTask = session.SendRequestAsync(request, CancellationToken.None); var responseWithTimeout = await Task.WhenAny(responseTask, Task.Delay(TimeSpan.FromSeconds(5))); // Assert Assert.Equal(responseTask, responseWithTimeout); var receivedResponse = await responseTask; Assert.Equal(response.StatusCode, receivedResponse.StatusCode); Assert.Equal(response.Streams.Count, receivedResponse.Streams.Count); Assert.True(response.Streams.SequenceEqual(receivedResponse.Streams)); }
public async Task StreamingSession_RequestWithStreams_SentToHandler(int streamLength, int streamCount, int chunkCount) { // Arrange var requestId = Guid.NewGuid(); var request = new ReceiveRequest() { Verb = "GET", Path = "api/version", Streams = new List <IContentStream>() }; request.Streams = StreamingDataGenerator.CreateStreams(requestId, streamLength, streamCount, chunkCount); var requestHandler = new Mock <RequestHandler>(); var requestCompletionSource = new TaskCompletionSource <bool>(); requestHandler .Setup(r => r.ProcessRequestAsync(It.IsAny <ReceiveRequest>(), null, null, CancellationToken.None)) .ReturnsAsync(() => new StreamingResponse() { StatusCode = 200 }) .Callback(() => requestCompletionSource.SetResult(true)); var transportHandler = new Mock <TransportHandler>(new Mock <IDuplexPipe>().Object, NullLogger.Instance); var responseCompletionSource = new TaskCompletionSource <bool>(); transportHandler .Setup(t => t.SendResponseAsync(It.IsAny <Guid>(), It.Is <ResponseModel>(r => r.StatusCode == 200), CancellationToken.None)) .Callback(() => responseCompletionSource.SetResult(true)); // Act var session = new StreamingSession(requestHandler.Object, transportHandler.Object, NullLogger.Instance); session.ReceiveRequest(new Header() { Id = requestId, Type = PayloadTypes.Request }, request); foreach (AugmentedStreamDefinition definition in request.Streams) { var chunkList = definition.Chunks; for (int i = 0; i < chunkList.Count; i++) { bool isLast = i == chunkList.Count - 1; session.ReceiveStream( new Header() { End = isLast, Id = definition.Id, PayloadLength = chunkList[i].Length, Type = PayloadTypes.Stream }, chunkList[i]); } } var roundtripTask = Task.WhenAll(requestCompletionSource.Task, responseCompletionSource.Task); var result = await Task.WhenAny(roundtripTask, Task.Delay(TimeSpan.FromSeconds(5))); // Assert Assert.Equal(result, roundtripTask); }