Beispiel #1
0
        public void StreamingSession_ReceiveResponse_ParameterValidation(Header header, ReceiveResponse response, Type exceptionType)
        {
            // Arrange
            var transportHandler = new Mock <TransportHandler>(new Mock <IDuplexPipe>().Object, NullLogger.Instance);
            var session          = new StreamingSession(new Mock <RequestHandler>().Object, transportHandler.Object, NullLogger.Instance);

            // Act + Assert
            Assert.Throws(exceptionType, () => session.ReceiveResponse(header, response));
        }
Beispiel #2
0
        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));
        }