Пример #1
0
        public void FlowControlShouldBeResilientToMissingStreams()
        {
            IHttp2Connection        conn  = new DefaultHttp2Connection(true);
            IHttp2ConnectionEncoder enc   = new DefaultHttp2ConnectionEncoder(conn, new DefaultHttp2FrameWriter());
            IHttp2ConnectionDecoder dec   = new DefaultHttp2ConnectionDecoder(conn, enc, new DefaultHttp2FrameReader());
            Http2FrameCodec         codec = new Http2FrameCodec(enc, dec, new Http2Settings(), false);
            EmbeddedChannel         em    = new EmbeddedChannel(codec);

            // We call #consumeBytes on a stream id which has not been seen yet to emulate the case
            // where a stream is deregistered which in reality can happen in response to a RST.
            Assert.False(codec.ConsumeBytes(1, 1));
            Assert.True(em.FinishAndReleaseAll());
            Assert.True(true);
        }
Пример #2
0
        protected virtual void SetInitialServerChannelPipeline(IChannel ch)
        {
            this.serverConnectedChannel = ch;
            var p = ch.Pipeline;
            IHttp2FrameWriter frameWriter = new DefaultHttp2FrameWriter();

            this.serverConnection.Remote.FlowController = new DefaultHttp2RemoteFlowController(this.serverConnection);
            this.serverConnection.Local.FlowController  = new DefaultHttp2LocalFlowController(this.serverConnection).FrameWriter(frameWriter);
            IHttp2ConnectionEncoder encoder = new CompressorHttp2ConnectionEncoder(
                new DefaultHttp2ConnectionEncoder(this.serverConnection, frameWriter));
            IHttp2ConnectionDecoder decoder =
                new DefaultHttp2ConnectionDecoder(this.serverConnection, encoder, new DefaultHttp2FrameReader());
            Http2ConnectionHandler connectionHandler = new Http2ConnectionHandlerBuilder()
            {
                FrameListener = new DelegatingDecompressorFrameListener(this.serverConnection, this.serverListener.Object)
            }
            .Codec(decoder, encoder).Build();

            p.AddLast(connectionHandler);
        }
Пример #3
0
        protected virtual void SetInitialChannelPipeline(IChannel ch)
        {
            var p = ch.Pipeline;
            IHttp2FrameWriter frameWriter = new DefaultHttp2FrameWriter();

            this.clientConnection.Remote.FlowController = new DefaultHttp2RemoteFlowController(this.clientConnection);
            this.clientConnection.Local.FlowController  = new DefaultHttp2LocalFlowController(this.clientConnection).FrameWriter(frameWriter);
            this.clientEncoder = new CompressorHttp2ConnectionEncoder(
                new DefaultHttp2ConnectionEncoder(this.clientConnection, frameWriter));

            IHttp2ConnectionDecoder decoder =
                new DefaultHttp2ConnectionDecoder(this.clientConnection, this.clientEncoder,
                                                  new DefaultHttp2FrameReader());

            this.clientHandler = new Http2ConnectionHandlerBuilder()
            {
                FrameListener = new DelegatingDecompressorFrameListener(this.clientConnection, this.clientListener.Object),
                // By default tests don't wait for server to gracefully shutdown streams
                GracefulShutdownTimeout = TimeSpan.Zero
            }
            .Codec(decoder, this.clientEncoder).Build();
            p.AddLast(this.clientHandler);
        }
        public StreamBufferingEncoderTest()
        {
            _writer        = new Mock <IHttp2FrameWriter>();
            _ctx           = new Mock <IChannelHandlerContext>();
            _channel       = new Mock <IChannel>();
            _channelUnsafe = new Mock <IChannelUnsafe>();
            _config        = new Mock <IChannelConfiguration>();
            _executor      = new Mock <IEventExecutor>();

            var configuration   = new Mock <IHttp2FrameWriterConfiguration>();
            var frameSizePolicy = new Mock <IHttp2FrameSizePolicy>();

            _writer.SetupGet(x => x.Configuration).Returns(() => configuration.Object);
            configuration.SetupGet(x => x.FrameSizePolicy).Returns(() => frameSizePolicy.Object);
            frameSizePolicy.SetupGet(x => x.MaxFrameSize).Returns(Http2CodecUtil.DefaultMaxFrameSize);
            _writer
            .Setup(x => x.WriteDataAsync(
                       It.IsAny <IChannelHandlerContext>(),
                       It.IsAny <int>(),
                       It.IsAny <IByteBuffer>(),
                       It.IsAny <int>(),
                       It.IsAny <bool>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, int, IByteBuffer, int, bool, IPromise>((c, i, buf, x, y, p) => SuccessAnswer(buf));
            _writer
            .Setup(x => x.WriteRstStreamAsync(
                       It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                       It.IsAny <int>(),
                       It.IsAny <Http2Error>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, int, Http2Error, IPromise>((x, y, z, p) => SuccessAnswer());
            _writer
            .Setup(x => x.WriteGoAwayAsync(
                       It.IsAny <IChannelHandlerContext>(),
                       It.IsAny <int>(),
                       It.IsAny <Http2Error>(),
                       It.IsAny <IByteBuffer>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, int, Http2Error, IByteBuffer, IPromise>((c, i, e, buf, p) => SuccessAnswer(buf));

            _connection = new DefaultHttp2Connection(false);
            _connection.Remote.FlowController = new DefaultHttp2RemoteFlowController(_connection);
            _connection.Local.FlowController  = new DefaultHttp2LocalFlowController(_connection).FrameWriter(_writer.Object);

            var defaultEncoder = new DefaultHttp2ConnectionEncoder(_connection, _writer.Object);

            _encoder = new StreamBufferingEncoder(defaultEncoder);
            var decoder = new DefaultHttp2ConnectionDecoder(_connection, _encoder, new Mock <IHttp2FrameReader>().Object);
            var builder = new Http2ConnectionHandlerBuilder()
            {
                FrameListener = new Mock <IHttp2FrameListener>().Object
            };
            var handler = builder.Codec(decoder, _encoder).Build();

            // Set LifeCycleManager on encoder and decoder
            _ctx.SetupGet(x => x.Channel).Returns(_channel.Object);
            _ctx.SetupGet(x => x.Allocator).Returns(UnpooledByteBufferAllocator.Default);
            _channel.SetupGet(x => x.Allocator).Returns(UnpooledByteBufferAllocator.Default);
            _executor.SetupGet(x => x.InEventLoop).Returns(true);
            _ctx.Setup(x => x.NewPromise()).Returns(() => NewPromise());
            _ctx.SetupGet(x => x.Executor).Returns(() => _executor.Object);
            _channel.SetupGet(x => x.IsActive).Returns(false);
            _channel.SetupGet(x => x.Configuration).Returns(() => _config.Object);
            _channel.SetupGet(x => x.IsWritable).Returns(true);
            _channel.SetupGet(x => x.BytesBeforeUnwritable).Returns(long.MaxValue);
            _config.SetupGet(x => x.WriteBufferHighWaterMark).Returns(int.MaxValue);
            _config.SetupGet(x => x.MessageSizeEstimator).Returns(DefaultMessageSizeEstimator.Default);
            ChannelMetadata metadata = new ChannelMetadata(false, 16);

            _channel.SetupGet(x => x.Metadata).Returns(metadata);
            _channel.SetupGet(x => x.Unsafe).Returns(() => _channelUnsafe.Object);
            handler.HandlerAdded(_ctx.Object);
        }
        public Http2ControlFrameLimitEncoderTest()
        {
            _writer   = new Mock <IHttp2FrameWriter>();
            _ctx      = new Mock <IChannelHandlerContext>();
            _channel  = new Mock <IChannel>();
            _unsafe   = new Mock <IChannelUnsafe>();
            _config   = new Mock <IChannelConfiguration>();
            _executor = new Mock <IEventExecutor>();

            _numWrites = 0;

            var configuration   = new Mock <IHttp2FrameWriterConfiguration>();
            var frameSizePolicy = new Mock <IHttp2FrameSizePolicy>();

            _writer.SetupGet(x => x.Configuration).Returns(configuration.Object);
            configuration.SetupGet(x => x.FrameSizePolicy).Returns(frameSizePolicy.Object);
            frameSizePolicy.SetupGet(x => x.MaxFrameSize).Returns(Http2CodecUtil.DefaultMaxFrameSize);

            _writer
            .Setup(x => x.WriteRstStreamAsync(
                       It.IsAny <IChannelHandlerContext>(),
                       It.IsAny <int>(),
                       It.IsAny <Http2Error>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, int, Http2Error, IPromise>((ctx, streamId, errorCode, p) =>
            {
                return(HandlePromise(p, 3).Task);
            });
            _writer
            .Setup(x => x.WriteSettingsAckAsync(
                       It.IsAny <IChannelHandlerContext>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, IPromise>((ctx, p) =>
            {
                return(HandlePromise(p, 1).Task);
            });
            _writer
            .Setup(x => x.WritePingAsync(
                       It.IsAny <IChannelHandlerContext>(),
                       It.IsAny <bool>(),
                       It.IsAny <long>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, bool, long, IPromise>((ctx, ack, data, p) =>
            {
                var promise = HandlePromise(p, 3);
                if (ack == false)
                {
                    promise.TryComplete();
                }
                return(promise.Task);
            });
            _writer
            .Setup(x => x.WriteGoAwayAsync(
                       It.IsAny <IChannelHandlerContext>(),
                       It.IsAny <int>(),
                       It.IsAny <Http2Error>(),
                       It.IsAny <IByteBuffer>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, int, Http2Error, IByteBuffer, IPromise>((ctx, streamId, errCode, debugData, p) =>
            {
                ReferenceCountUtil.Release(debugData);
                _goAwayPromises.AddLast​(p);
                return(p.Task);
            });
            IHttp2Connection connection = new DefaultHttp2Connection(false);

            connection.Remote.FlowController = new DefaultHttp2RemoteFlowController(connection);
            connection.Local.FlowController  = new DefaultHttp2LocalFlowController(connection).FrameWriter(_writer.Object);

            DefaultHttp2ConnectionEncoder defaultEncoder =
                new DefaultHttp2ConnectionEncoder(connection, _writer.Object);

            _encoder = new Http2ControlFrameLimitEncoder(defaultEncoder, 2);
            DefaultHttp2ConnectionDecoder decoder =
                new DefaultHttp2ConnectionDecoder(connection, _encoder, (new Mock <IHttp2FrameReader>()).Object);
            var builder = new Http2ConnectionHandlerBuilder();

            builder.FrameListener = (new Mock <IHttp2FrameListener>()).Object;
            Http2ConnectionHandler handler = builder.Codec(decoder, _encoder).Build();

            // Set LifeCycleManager on _encoder and decoder
            _ctx.SetupGet(x => x.Channel).Returns(_channel.Object);
            _ctx.SetupGet(x => x.Allocator).Returns(UnpooledByteBufferAllocator.Default);
            _channel.SetupGet(x => x.Allocator).Returns(UnpooledByteBufferAllocator.Default);
            _executor.SetupGet(x => x.InEventLoop).Returns(true);
            _ctx.Setup(x => x.NewPromise()).Returns(() => NewPromise());
            _ctx.SetupGet(x => x.Executor).Returns(_executor.Object);
            _channel.SetupGet(x => x.IsActive).Returns(false);
            _channel.SetupGet(x => x.Configuration).Returns(_config.Object);
            _channel.SetupGet(x => x.IsWritable).Returns(true);
            _channel.SetupGet(x => x.BytesBeforeUnwritable).Returns(long.MaxValue);
            _config.SetupGet(x => x.WriteBufferHighWaterMark).Returns(int.MaxValue);
            _config.SetupGet(x => x.MessageSizeEstimator).Returns(DefaultMessageSizeEstimator.Default);
            ChannelMetadata metadata = new ChannelMetadata(false, 16);

            _channel.SetupGet(x => x.Metadata).Returns(metadata);
            _channel.SetupGet(x => x.Unsafe).Returns(_unsafe.Object);
            handler.HandlerAdded(_ctx.Object);
        }