Ejemplo n.º 1
0
        public void StreamingSession_ReceiveRequest_ParameterValidation(Header header, ReceiveRequest request, 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.ReceiveRequest(header, request));
        }
Ejemplo n.º 2
0
        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);
        }