Search in sources :

Example 81 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class Http2FrameCodecTest method newOutboundStreamsShouldBeBuffered.

@Test
public void newOutboundStreamsShouldBeBuffered() throws Exception {
    setUp(Http2FrameCodecBuilder.forServer().encoderEnforceMaxConcurrentStreams(true), new Http2Settings().maxConcurrentStreams(1));
    Http2FrameStream stream1 = frameCodec.newStream();
    Http2FrameStream stream2 = frameCodec.newStream();
    ChannelPromise promise1 = channel.newPromise();
    ChannelPromise promise2 = channel.newPromise();
    channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream1), promise1);
    channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()).stream(stream2), promise2);
    assertTrue(isStreamIdValid(stream1.id()));
    channel.runPendingTasks();
    assertTrue(isStreamIdValid(stream2.id()));
    assertTrue(promise1.syncUninterruptibly().isSuccess());
    assertFalse(promise2.isDone());
    // Increase concurrent streams limit to 2
    frameInboundWriter.writeInboundSettings(new Http2Settings().maxConcurrentStreams(2));
    channel.flush();
    assertTrue(promise2.syncUninterruptibly().isSuccess());
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) Http2TestUtil.anyChannelPromise(io.netty.handler.codec.http2.Http2TestUtil.anyChannelPromise) Http2TestUtil.anyHttp2Settings(io.netty.handler.codec.http2.Http2TestUtil.anyHttp2Settings) Test(org.junit.jupiter.api.Test)

Example 82 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class Http2ConnectionRoundtripTest method writeOfEmptyReleasedBufferQueuedInFlowControllerShouldFail.

private void writeOfEmptyReleasedBufferQueuedInFlowControllerShouldFail(final WriteEmptyBufferMode mode) throws Exception {
    bootstrapEnv(1, 1, 2, 1);
    final ChannelPromise emptyDataPromise = newPromise();
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Client.encoder().writeHeaders(ctx(), 3, EmptyHttp2Headers.INSTANCE, 0, (short) 16, false, 0, false, newPromise());
            ByteBuf emptyBuf = Unpooled.buffer();
            emptyBuf.release();
            switch(mode) {
                case SINGLE_END_OF_STREAM:
                    http2Client.encoder().writeData(ctx(), 3, emptyBuf, 0, true, emptyDataPromise);
                    break;
                case SECOND_END_OF_STREAM:
                    http2Client.encoder().writeData(ctx(), 3, emptyBuf, 0, false, emptyDataPromise);
                    http2Client.encoder().writeData(ctx(), 3, randomBytes(8), 0, true, newPromise());
                    break;
                case SINGLE_WITH_TRAILERS:
                    http2Client.encoder().writeData(ctx(), 3, emptyBuf, 0, false, emptyDataPromise);
                    http2Client.encoder().writeHeaders(ctx(), 3, EmptyHttp2Headers.INSTANCE, 0, (short) 16, false, 0, true, newPromise());
                    break;
                case SECOND_WITH_TRAILERS:
                    http2Client.encoder().writeData(ctx(), 3, emptyBuf, 0, false, emptyDataPromise);
                    http2Client.encoder().writeData(ctx(), 3, randomBytes(8), 0, false, newPromise());
                    http2Client.encoder().writeHeaders(ctx(), 3, EmptyHttp2Headers.INSTANCE, 0, (short) 16, false, 0, true, newPromise());
                    break;
                default:
                    throw new Error();
            }
            http2Client.flush(ctx());
        }
    });
    ExecutionException e = assertThrows(ExecutionException.class, new Executable() {

        @Override
        public void execute() throws Throwable {
            emptyDataPromise.get();
        }
    });
    assertThat(e.getCause(), is(instanceOf(IllegalReferenceCountException.class)));
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) ExecutionException(java.util.concurrent.ExecutionException) Executable(org.junit.jupiter.api.function.Executable)

Example 83 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class Http2ControlFrameLimitEncoderTest method teardown.

@AfterEach
public void teardown() {
    // Close and release any buffered frames.
    encoder.close();
    // debugData.
    for (; ; ) {
        ChannelPromise promise = goAwayPromises.poll();
        if (promise == null) {
            break;
        }
        promise.setSuccess();
    }
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) AfterEach(org.junit.jupiter.api.AfterEach)

Example 84 with ChannelPromise

use of org.apache.flink.shaded.netty4.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 85 with ChannelPromise

use of org.apache.flink.shaded.netty4.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)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)223 Test (org.junit.jupiter.api.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)63 ChannelFuture (io.netty.channel.ChannelFuture)62 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)58 ByteBuf (io.netty.buffer.ByteBuf)56 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)30 Test (org.junit.Test)25 Channel (io.netty.channel.Channel)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 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)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 Bootstrap (io.netty.bootstrap.Bootstrap)12