Example #1
0
        [Fact]//(timeout = 5000)
        public async Task NewOutboundStream()
        {
            IHttp2FrameStream stream = _frameCodec.NewStream();

            Assert.NotNull(stream);
            Assert.False(Http2CodecUtil.IsStreamIdValid(stream.Id));

            var listenerExecuted = new TaskCompletionSource();

            await _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers(), false) { Stream = stream });

            Assert.True(Http2CodecUtil.IsStreamIdValid(stream.Id));

            var data = Unpooled.Buffer().WriteZero(100);
            await _channel.WriteAndFlushAsync(new DefaultHttp2DataFrame(data) { Stream = stream });
        }
Example #2
0
        public void MultipleOutboundStreams()
        {
            IHttp2StreamChannel childChannel1 = this.NewOutboundStream(new TestChannelInitializer());

            Assert.True(childChannel1.Active);
            Assert.False(Http2CodecUtil.IsStreamIdValid(childChannel1.Stream.Id));
            IHttp2StreamChannel childChannel2 = this.NewOutboundStream(new TestChannelInitializer());

            Assert.True(childChannel2.Active);
            Assert.False(Http2CodecUtil.IsStreamIdValid(childChannel2.Stream.Id));

            IHttp2Headers headers1 = new DefaultHttp2Headers();
            IHttp2Headers headers2 = new DefaultHttp2Headers();

            // Test that streams can be made active (headers sent) in different order than the corresponding channels
            // have been created.
            childChannel2.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(headers2));
            childChannel1.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(headers1));

            IHttp2HeadersFrame headersFrame2 = serverLastInboundHandler.BlockingReadInbound <IHttp2HeadersFrame>();

            Assert.NotNull(headersFrame2);
            Assert.Equal(3, headersFrame2.Stream.Id);

            IHttp2HeadersFrame headersFrame1 = serverLastInboundHandler.BlockingReadInbound <IHttp2HeadersFrame>();

            Assert.NotNull(headersFrame1);
            Assert.Equal(5, headersFrame1.Stream.Id);

            Assert.Equal(3, childChannel2.Stream.Id);
            Assert.Equal(5, childChannel1.Stream.Id);

            childChannel1.CloseAsync();
            childChannel2.CloseAsync();

            serverLastInboundHandler.CheckException();
        }
Example #3
0
        public void NewOutboundStreamsShouldBeBuffered()
        {
            var builder = Http2FrameCodecBuilder.ForServer();

            builder.EncoderEnforceMaxConcurrentStreams = true;
            SetUp(builder, new Http2Settings().MaxConcurrentStreams(1));

            var stream1 = _frameCodec.NewStream();
            var stream2 = _frameCodec.NewStream();

            var promise1 = _channel.NewPromise();
            var promise2 = _channel.NewPromise();

            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream1
            }, promise1);
            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream2
            }, promise2);

            Assert.True(Http2CodecUtil.IsStreamIdValid(stream1.Id));
            _channel.RunPendingTasks();
            Assert.True(Http2CodecUtil.IsStreamIdValid(stream2.Id));

            Assert.True(promise1.IsSuccess);
            Assert.False(promise2.IsCompleted);

            // Increase concurrent streams limit to 2
            _frameInboundWriter.WriteInboundSettings(new Http2Settings().MaxConcurrentStreams(2));

            _channel.Flush();

            Assert.True(promise2.IsSuccess);
        }
Example #4
0
        public void MultipleNewOutboundStreamsShouldBeBuffered()
        {
            var builder = Http2FrameCodecBuilder.ForServer();

            builder.EncoderEnforceMaxConcurrentStreams = true;
            // We use a limit of 1 and then increase it step by step.
            SetUp(builder, new Http2Settings().MaxConcurrentStreams(1));

            IHttp2FrameStream stream1 = _frameCodec.NewStream();
            IHttp2FrameStream stream2 = _frameCodec.NewStream();
            IHttp2FrameStream stream3 = _frameCodec.NewStream();

            IPromise promise1 = _channel.NewPromise();
            IPromise promise2 = _channel.NewPromise();
            IPromise promise3 = _channel.NewPromise();

            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream1
            }, promise1);
            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream2
            }, promise2);
            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream3
            }, promise3);

            Assert.True(Http2CodecUtil.IsStreamIdValid(stream1.Id));
            _channel.RunPendingTasks();
            Assert.True(Http2CodecUtil.IsStreamIdValid(stream2.Id));

            if (promise1.IsCompleted)
            {
                Assert.True(promise1.Task.IsSuccess());
            }
            else
            {
                try
                {
                    promise1.Task.GetAwaiter().GetResult();
                }
                catch (Exception)
                {
                    Assert.False(true);
                }
            }
            Assert.False(promise2.IsCompleted);
            Assert.False(promise3.IsCompleted);

            // Increase concurrent streams limit to 2
            _frameInboundWriter.WriteInboundSettings(new Http2Settings().MaxConcurrentStreams(2));
            _channel.Flush();

            // As we increased the limit to 2 we should have also succeed the second frame.
            if (promise2.IsCompleted)
            {
                Assert.True(promise2.Task.IsSuccess());
            }
            else
            {
                try
                {
                    promise2.Task.GetAwaiter().GetResult();
                }
                catch (Exception)
                {
                    Assert.False(true);
                }
            }
            Assert.False(promise3.IsCompleted);

            _frameInboundWriter.WriteInboundSettings(new Http2Settings().MaxConcurrentStreams(3));
            _channel.Flush();

            // With the max streams of 3 all streams should be succeed now.
            if (promise3.IsCompleted)
            {
                Assert.True(promise3.Task.IsSuccess());
            }
            else
            {
                try
                {
                    promise3.Task.GetAwaiter().GetResult();
                }
                catch (Exception)
                {
                    Assert.False(true);
                }
            }

            Assert.False(_channel.FinishAndReleaseAll());
        }