public void VerifyChannelHandlerCanBeReusedInPipeline()
        {
            _connection.Setup(x => x.IsServer).Returns(true);
            _handler = NewHandler();
            // Only read the connection preface...after preface is read internal state of Http2ConnectionHandler
            // is expected to change relative to the pipeline.
            IByteBuffer preface = Http2CodecUtil.ConnectionPrefaceBuf();

            _handler.ChannelRead(_ctx.Object, preface);
            _decoder.Verify(
                x => x.DecodeFrame(
                    It.IsAny <IChannelHandlerContext>(),
                    It.IsAny <IByteBuffer>(),
                    It.IsAny <List <object> >()),
                Times.Never());

            // Now remove and add the this.handler...this is setting up the test condition.
            _handler.HandlerRemoved(_ctx.Object);
            _handler.HandlerAdded(_ctx.Object);

            // Now verify we can continue as normal, reading connection preface plus more.
            IByteBuffer prefacePlusSome = AddSettingsHeader(Unpooled.Buffer().WriteBytes(Http2CodecUtil.ConnectionPrefaceBuf()));

            _handler.ChannelRead(_ctx.Object, prefacePlusSome);
            _decoder.Verify(
                x => x.DecodeFrame(
                    It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                    It.IsAny <IByteBuffer>(),
                    It.IsAny <List <object> >()),
                Times.AtLeastOnce);
        }
        public void PrefaceUserEventProcessed()
        {
            var latch = new CountdownEvent(1);

            _handler = new PrefaceUserEventHttp2ConnectionHandler(latch, _decoder.Object, _encoder.Object, new Http2Settings());
            _handler.HandlerAdded(_ctx.Object);
            Assert.True(latch.Wait(TimeSpan.FromSeconds(5)));
        }
        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);
        }