Search in sources :

Example 91 with ChannelFuture

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

the class Http2ConnectionRoundtripTest method encodeViolatesMaxHeaderListSizeCanStillUseConnection.

@Test
public void encodeViolatesMaxHeaderListSizeCanStillUseConnection() throws Exception {
    bootstrapEnv(1, 2, 1, 0, 0);
    final CountDownLatch serverSettingsAckLatch1 = new CountDownLatch(2);
    final CountDownLatch serverSettingsAckLatch2 = new CountDownLatch(3);
    final CountDownLatch clientSettingsLatch1 = new CountDownLatch(3);
    final CountDownLatch serverRevHeadersLatch = new CountDownLatch(1);
    final CountDownLatch clientHeadersLatch = new CountDownLatch(1);
    final CountDownLatch clientDataWrite = new CountDownLatch(1);
    final AtomicReference<Throwable> clientHeadersWriteException = new AtomicReference<Throwable>();
    final AtomicReference<Throwable> clientHeadersWriteException2 = new AtomicReference<Throwable>();
    final AtomicReference<Throwable> clientDataWriteException = new AtomicReference<Throwable>();
    final Http2Headers headers = dummyHeaders();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            serverSettingsAckLatch1.countDown();
            serverSettingsAckLatch2.countDown();
            return null;
        }
    }).when(serverListener).onSettingsAckRead(any(ChannelHandlerContext.class));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            clientSettingsLatch1.countDown();
            return null;
        }
    }).when(clientListener).onSettingsRead(any(ChannelHandlerContext.class), any(Http2Settings.class));
    // Manually add a listener for when we receive the expected headers on the server.
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            serverRevHeadersLatch.countDown();
            return null;
        }
    }).when(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(5), eq(headers), anyInt(), anyShort(), anyBoolean(), eq(0), eq(true));
    // Set the maxHeaderListSize to 100 so we may be able to write some headers, but not all. We want to verify
    // that we don't corrupt state if some can be written but not all.
    runInChannel(serverConnectedChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Server.encoder().writeSettings(serverCtx(), new Http2Settings().copyFrom(http2Server.decoder().localSettings()).maxHeaderListSize(100), serverNewPromise());
            http2Server.flush(serverCtx());
        }
    });
    assertTrue(serverSettingsAckLatch1.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Client.encoder().writeHeaders(ctx(), 3, headers, 0, false, newPromise()).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    clientHeadersWriteException.set(future.cause());
                }
            });
            // It is expected that this write should fail locally and the remote peer will never see this.
            http2Client.encoder().writeData(ctx(), 3, Unpooled.buffer(), 0, true, newPromise()).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    clientDataWriteException.set(future.cause());
                    clientDataWrite.countDown();
                }
            });
            http2Client.flush(ctx());
        }
    });
    assertTrue(clientDataWrite.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    assertNotNull("Header encode should have exceeded maxHeaderListSize!", clientHeadersWriteException.get());
    assertNotNull("Data on closed stream should fail!", clientDataWriteException.get());
    // Set the maxHeaderListSize to the max value so we can send the headers.
    runInChannel(serverConnectedChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Server.encoder().writeSettings(serverCtx(), new Http2Settings().copyFrom(http2Server.decoder().localSettings()).maxHeaderListSize(Http2CodecUtil.MAX_HEADER_LIST_SIZE), serverNewPromise());
            http2Server.flush(serverCtx());
        }
    });
    assertTrue(clientSettingsLatch1.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    assertTrue(serverSettingsAckLatch2.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Client.encoder().writeHeaders(ctx(), 5, headers, 0, true, newPromise()).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    clientHeadersWriteException2.set(future.cause());
                    clientHeadersLatch.countDown();
                }
            });
            http2Client.flush(ctx());
        }
    });
    assertTrue(clientHeadersLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    assertNull("Client write of headers should succeed with increased header list size!", clientHeadersWriteException2.get());
    assertTrue(serverRevHeadersLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    verify(serverListener, never()).onDataRead(any(ChannelHandlerContext.class), anyInt(), any(ByteBuf.class), anyInt(), anyBoolean());
    // Verify that no errors have been received.
    verify(serverListener, never()).onGoAwayRead(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class));
    verify(serverListener, never()).onRstStreamRead(any(ChannelHandlerContext.class), anyInt(), anyLong());
    verify(clientListener, never()).onGoAwayRead(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class));
    verify(clientListener, never()).onRstStreamRead(any(ChannelHandlerContext.class), anyInt(), anyLong());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 92 with ChannelFuture

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

the class Http2FrameCodecTest method windowUpdateMayFail.

@Test
public void windowUpdateMayFail() throws Exception {
    frameListener.onHeadersRead(http2HandlerCtx, 3, request, 31, false);
    Http2Connection connection = framingCodec.connectionHandler().connection();
    Http2Stream stream = connection.stream(3);
    assertNotNull(stream);
    // Fails, cause trying to return too many bytes to the flow controller
    ChannelFuture f = channel.write(new DefaultHttp2WindowUpdateFrame(100).streamId(stream.id()));
    assertTrue(f.isDone());
    assertFalse(f.isSuccess());
    assertThat(f.cause(), instanceOf(Http2Exception.class));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Test(org.junit.Test)

Example 93 with ChannelFuture

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

the class Http2FrameCodecTest method unknownFrameTypeShouldThrowAndBeReleased.

@Test
public void unknownFrameTypeShouldThrowAndBeReleased() throws Exception {
    class UnknownHttp2Frame extends AbstractReferenceCounted implements Http2Frame {

        @Override
        public String name() {
            return "UNKNOWN";
        }

        @Override
        protected void deallocate() {
        }

        @Override
        public ReferenceCounted touch(Object hint) {
            return this;
        }
    }
    UnknownHttp2Frame frame = new UnknownHttp2Frame();
    assertEquals(1, frame.refCnt());
    ChannelFuture f = channel.write(frame);
    f.await();
    assertTrue(f.isDone());
    assertFalse(f.isSuccess());
    assertThat(f.cause(), instanceOf(UnsupportedMessageTypeException.class));
    assertEquals(0, frame.refCnt());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AbstractReferenceCounted(io.netty.util.AbstractReferenceCounted) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException) Test(org.junit.Test)

Example 94 with ChannelFuture

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

the class Http2FrameCodecTest method windowUpdateFrameDecrementsConsumedBytes.

@Test
public void windowUpdateFrameDecrementsConsumedBytes() throws Exception {
    frameListener.onHeadersRead(http2HandlerCtx, 3, request, 31, false);
    Http2Connection connection = framingCodec.connectionHandler().connection();
    Http2Stream stream = connection.stream(3);
    assertNotNull(stream);
    ByteBuf data = Unpooled.buffer(100).writeZero(100);
    frameListener.onDataRead(http2HandlerCtx, 3, data, 0, true);
    int before = connection.local().flowController().unconsumedBytes(stream);
    ChannelFuture f = channel.write(new DefaultHttp2WindowUpdateFrame(100).streamId(stream.id()));
    int after = connection.local().flowController().unconsumedBytes(stream);
    assertEquals(100, before - after);
    assertTrue(f.isSuccess());
    data.release();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 95 with ChannelFuture

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

the class HttpToHttp2ConnectionHandlerTest method testNoSchemeRequestTargetHandled.

@Test
public void testNoSchemeRequestTargetHandled() throws Exception {
    bootstrapEnv(2, 1, 0);
    final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/");
    final HttpHeaders httpHeaders = request.headers();
    httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 5);
    httpHeaders.set(HttpHeaderNames.HOST, "localhost");
    ChannelPromise writePromise = newPromise();
    ChannelFuture writeFuture = clientChannel.writeAndFlush(request, writePromise);
    assertTrue(writePromise.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(writePromise.isDone());
    assertFalse(writePromise.isSuccess());
    assertTrue(writeFuture.isDone());
    assertFalse(writeFuture.isSuccess());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.Test)

Aggregations

ChannelFuture (io.netty.channel.ChannelFuture)383 Test (org.junit.Test)131 Channel (io.netty.channel.Channel)120 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)105 Bootstrap (io.netty.bootstrap.Bootstrap)98 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)94 ChannelFutureListener (io.netty.channel.ChannelFutureListener)88 ByteBuf (io.netty.buffer.ByteBuf)81 InetSocketAddress (java.net.InetSocketAddress)79 EventLoopGroup (io.netty.channel.EventLoopGroup)78 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)76 IOException (java.io.IOException)69 ChannelPipeline (io.netty.channel.ChannelPipeline)67 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)64 ClosedChannelException (java.nio.channels.ClosedChannelException)56 ChannelInitializer (io.netty.channel.ChannelInitializer)47 AtomicReference (java.util.concurrent.atomic.AtomicReference)47 ArrayList (java.util.ArrayList)46 SslHandler (io.netty.handler.ssl.SslHandler)45 Http2Headers (io.netty.handler.codec.http2.Http2Headers)44