private void WriteRstStreamUsingVoidPromise(int streamId)
        {
            _handler = NewHandler();
            var cause = new Http2RuntimeException("fake exception");

            _stream.Setup(x => x.Id).Returns(STREAM_ID);
            _frameWriter
            .Setup(x => x.WriteRstStreamAsync(
                       It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                       It.Is <int>(v => v == streamId),
                       It.IsAny <Http2Error>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, int, Http2Error, IPromise>((ctx, id, err, p) =>
            {
                Assert.False(p.IsVoid);
                p.SetException(cause);
                return(p.Task);
            });
            _handler.ResetStreamAsync(_ctx.Object, streamId, Http2Error.StreamClosed, Http2TestUtil.NewVoidPromise(_channel.Object));
            _frameWriter.Verify(
                x => x.WriteRstStreamAsync(
                    It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                    It.Is <int>(v => v == streamId),
                    It.IsAny <Http2Error>(),
                    It.IsAny <IPromise>()));
            _pipeline.Verify(x => x.FireExceptionCaught(It.Is <Exception>(v => ReferenceEquals(v, cause))));
        }
        public void WriteMultipleRstFramesForSameStream()
        {
            _handler = NewHandler();
            _stream.SetupGet(x => x.Id).Returns(STREAM_ID);
            AtomicBoolean resetSent = new AtomicBoolean();

            _stream.Setup(x => x.ResetSent()).Returns(() =>
            {
                resetSent.Value = true;
                return(_stream.Object);
            });
            _stream.SetupGet(x => x.IsResetSent).Returns(() => resetSent.Value);
            _frameWriter
            .Setup(x => x.WriteRstStreamAsync(
                       It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                       It.Is <int>(v => v == STREAM_ID),
                       It.IsAny <Http2Error>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, int, Http2Error, IPromise>((ctx, id, err, p) =>
            {
                p.TryComplete();
                return(p.Task);
            });

            var promise  = new TaskCompletionSource();
            var promise2 = new TaskCompletionSource();

            promise.Task.ContinueWith(t =>
            {
                _handler.ResetStreamAsync(_ctx.Object, STREAM_ID, Http2Error.StreamClosed, promise2);
            }, TaskContinuationOptions.ExecuteSynchronously);

            _handler.ResetStreamAsync(_ctx.Object, STREAM_ID, Http2Error.Cancel, promise);
            _frameWriter.Verify(
                x => x.WriteRstStreamAsync(
                    It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                    It.Is <int>(v => v == STREAM_ID),
                    It.IsAny <Http2Error>(),
                    It.IsAny <IPromise>()));
            Assert.True(promise.IsSuccess);
            Assert.True(promise2.IsSuccess);
        }
 public void WriteRstOnIdleStreamShouldNotWriteButStillSucceed()
 {
     _handler = NewHandler();
     _stream.Setup(x => x.State).Returns(Http2StreamState.Idle);
     _handler.ResetStreamAsync(_ctx.Object, STREAM_ID, Http2Error.StreamClosed, _promise);
     _frameWriter
     .Verify(x => x.WriteRstStreamAsync(
                 It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                 It.Is <int>(v => v == STREAM_ID),
                 It.IsAny <Http2Error>(),
                 It.IsAny <IPromise>()),
             Times.Never());
     _stream.Verify(x => x.Close());
 }
 public void WriteRstOnNonExistantStreamShouldSucceed()
 {
     _handler = NewHandler();
     _frameWriter
     .Setup(x => x.WriteRstStreamAsync(
                It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                It.Is <int>(v => v == NON_EXISTANT_STREAM_ID),
                It.Is <Http2Error>(v => v == Http2Error.StreamClosed),
                It.Is <IPromise>(v => v == _promise)))
     .Returns(_future);
     _handler.ResetStreamAsync(_ctx.Object, NON_EXISTANT_STREAM_ID, Http2Error.StreamClosed, _promise);
     _frameWriter
     .Verify(x => x.WriteRstStreamAsync(
                 It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                 It.Is <int>(v => v == NON_EXISTANT_STREAM_ID),
                 It.Is <Http2Error>(v => v == Http2Error.StreamClosed),
                 It.Is <IPromise>(v => v == _promise)));
 }
 public void WriteRstOnClosedStreamShouldSucceed()
 {
     _handler = NewHandler();
     _stream.Setup(x => x.Id).Returns(STREAM_ID);
     _frameWriter
     .Setup(x => x.WriteRstStreamAsync(
                It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                It.Is <int>(v => v == STREAM_ID),
                It.IsAny <Http2Error>(),
                It.IsAny <IPromise>()))
     .Returns(_future);
     _stream.Setup(x => x.State).Returns(Http2StreamState.Closed);
     _stream.Setup(x => x.IsHeadersSent).Returns(true);
     // The stream is "closed" but is still known about by the connection (connection().stream(..)
     // will return the stream). We should still write a RST_STREAM frame in this scenario.
     _handler.ResetStreamAsync(_ctx.Object, STREAM_ID, Http2Error.StreamClosed, _promise);
     _frameWriter
     .Verify(x => x.WriteRstStreamAsync(
                 It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                 It.Is <int>(v => v == STREAM_ID),
                 It.IsAny <Http2Error>(),
                 It.IsAny <IPromise>()));
 }