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);
        }