Search in sources :

Example 81 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class Http2StreamFrameToHttpObjectCodecTest method testEncodeHttpsSchemeWhenSslHandlerExists.

@Test
public void testEncodeHttpsSchemeWhenSslHandlerExists() throws Exception {
    final Queue<Http2StreamFrame> frames = new ConcurrentLinkedQueue<Http2StreamFrame>();
    final SslContext ctx = SslContextBuilder.forClient().sslProvider(SslProvider.JDK).build();
    EmbeddedChannel ch = new EmbeddedChannel(ctx.newHandler(ByteBufAllocator.DEFAULT), new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            if (msg instanceof Http2StreamFrame) {
                frames.add((Http2StreamFrame) msg);
                ctx.write(Unpooled.EMPTY_BUFFER, promise);
            } else {
                ctx.write(msg, promise);
            }
        }
    }, new Http2StreamFrameToHttpObjectCodec(false));
    try {
        FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/hello/world");
        assertTrue(ch.writeOutbound(req));
        ch.finishAndReleaseAll();
        Http2HeadersFrame headersFrame = (Http2HeadersFrame) frames.poll();
        Http2Headers headers = headersFrame.headers();
        assertThat(headers.scheme().toString(), is("https"));
        assertThat(headers.method().toString(), is("GET"));
        assertThat(headers.path().toString(), is("/hello/world"));
        assertTrue(headersFrame.isEndStream());
        assertNull(frames.poll());
    } finally {
        ch.finishAndReleaseAll();
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) EncoderException(io.netty.handler.codec.EncoderException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.jupiter.api.Test)

Example 82 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class Http2MultiplexTest method outboundStreamShouldNotWriteResetFrameOnClose_IfStreamDidntExist.

@Test
public void outboundStreamShouldNotWriteResetFrameOnClose_IfStreamDidntExist() {
    when(frameWriter.writeHeaders(eqCodecCtx(), anyInt(), any(Http2Headers.class), anyInt(), anyBoolean(), any(ChannelPromise.class))).thenAnswer(new Answer<ChannelFuture>() {

        private boolean headersWritten;

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) {
            // refuses to allocate a new stream due to having received a GOAWAY.
            if (!headersWritten) {
                headersWritten = true;
                return ((ChannelPromise) invocationOnMock.getArgument(5)).setFailure(new Exception("boom"));
            }
            return ((ChannelPromise) invocationOnMock.getArgument(5)).setSuccess();
        }
    });
    Http2StreamChannel childChannel = newOutboundStream(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            ctx.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()));
            ctx.fireChannelActive();
        }
    });
    assertFalse(childChannel.isActive());
    childChannel.close();
    parentChannel.runPendingTasks();
    // The channel was never active so we should not generate a RST frame.
    verify(frameWriter, never()).writeRstStream(eqCodecCtx(), eqStreamId(childChannel), anyLong(), anyChannelPromise());
    assertTrue(parentChannel.outboundMessages().isEmpty());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelPromise(io.netty.channel.ChannelPromise) Http2TestUtil.anyChannelPromise(io.netty.handler.codec.http2.Http2TestUtil.anyChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) ClosedChannelException(java.nio.channels.ClosedChannelException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 83 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class Http2MultiplexTest method failedOutboundStreamCreationThrowsAndClosesChannel.

// Test failing the promise of the first headers frame of an outbound stream. In practice this error case would most
// likely happen due to the max concurrent streams limit being hit or the channel running out of stream identifiers.
// 
@Test
public void failedOutboundStreamCreationThrowsAndClosesChannel() throws Exception {
    LastInboundHandler handler = new LastInboundHandler();
    Http2StreamChannel childChannel = newOutboundStream(handler);
    assertTrue(childChannel.isActive());
    Http2Headers headers = new DefaultHttp2Headers();
    when(frameWriter.writeHeaders(eqCodecCtx(), anyInt(), eq(headers), anyInt(), anyBoolean(), any(ChannelPromise.class))).thenAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) {
            return ((ChannelPromise) invocationOnMock.getArgument(5)).setFailure(new Http2NoMoreStreamIdsException());
        }
    });
    final ChannelFuture future = childChannel.writeAndFlush(new DefaultHttp2HeadersFrame(headers));
    parentChannel.flush();
    assertFalse(childChannel.isActive());
    assertFalse(childChannel.isOpen());
    handler.checkException();
    assertThrows(Http2NoMoreStreamIdsException.class, new Executable() {

        @Override
        public void execute() {
            future.syncUninterruptibly();
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelPromise(io.netty.channel.ChannelPromise) Http2TestUtil.anyChannelPromise(io.netty.handler.codec.http2.Http2TestUtil.anyChannelPromise) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Example 84 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class Http2MultiplexTest method callUnsafeCloseMultipleTimes.

@Test
public void callUnsafeCloseMultipleTimes() {
    LastInboundHandler inboundHandler = new LastInboundHandler();
    Http2StreamChannel childChannel = newInboundStream(3, false, inboundHandler);
    childChannel.unsafe().close(childChannel.voidPromise());
    ChannelPromise promise = childChannel.newPromise();
    childChannel.unsafe().close(promise);
    promise.syncUninterruptibly();
    childChannel.closeFuture().syncUninterruptibly();
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) Http2TestUtil.anyChannelPromise(io.netty.handler.codec.http2.Http2TestUtil.anyChannelPromise) Test(org.junit.jupiter.api.Test)

Example 85 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class StreamBufferingEncoderTest method rstStreamClosesBufferedStream.

@Test
public void rstStreamClosesBufferedStream() {
    encoder.writeSettingsAck(ctx, newPromise());
    setMaxConcurrentStreams(0);
    encoderWriteHeaders(3, newPromise());
    assertEquals(1, encoder.numBufferedStreams());
    ChannelPromise rstStreamPromise = newPromise();
    encoder.writeRstStream(ctx, 3, CANCEL.code(), rstStreamPromise);
    assertTrue(rstStreamPromise.isSuccess());
    assertEquals(0, encoder.numBufferedStreams());
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)218 Test (org.junit.jupiter.api.Test)87 ChannelFuture (io.netty.channel.ChannelFuture)62 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)59 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)57 ByteBuf (io.netty.buffer.ByteBuf)54 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)27 Test (org.junit.Test)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 Channel (io.netty.channel.Channel)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 ClosedChannelException (java.nio.channels.ClosedChannelException)20 ChannelFutureListener (io.netty.channel.ChannelFutureListener)19 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)18 InvocationOnMock (org.mockito.invocation.InvocationOnMock)18 AsciiString (io.netty.util.AsciiString)15 IOException (java.io.IOException)13 Bootstrap (io.netty.bootstrap.Bootstrap)12 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)12