Search in sources :

Example 26 with Http2Runnable

use of io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable in project netty by netty.

the class DataCompressionHttp2Test method gzipEncodingMultipleMessages.

@Test
public void gzipEncodingMultipleMessages() throws Exception {
    final String text1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccc";
    final String text2 = "dddddddddddddddddddeeeeeeeeeeeeeeeeeeeffffffffffffffffffff";
    final ByteBuf data1 = Unpooled.copiedBuffer(text1.getBytes());
    final ByteBuf data2 = Unpooled.copiedBuffer(text2.getBytes());
    bootstrapEnv(data1.readableBytes() + data2.readableBytes());
    try {
        final Http2Headers headers = new DefaultHttp2Headers().method(POST).path(PATH).set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientEncoder.writeHeaders(ctxClient(), 3, headers, 0, false, newPromiseClient());
                clientEncoder.writeData(ctxClient(), 3, data1.retain(), 0, false, newPromiseClient());
                clientEncoder.writeData(ctxClient(), 3, data2.retain(), 0, true, newPromiseClient());
                clientHandler.flush(ctxClient());
            }
        });
        awaitServer();
        assertEquals(text1 + text2, serverOut.toString(CharsetUtil.UTF_8.name()));
    } finally {
        data1.release();
        data2.release();
    }
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 27 with Http2Runnable

use of io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable in project netty by netty.

the class Http2ConnectionRoundtripTest method headersWriteForPeerStreamWhichWasResetShouldNotGoAway.

@Test
public void headersWriteForPeerStreamWhichWasResetShouldNotGoAway() throws Exception {
    bootstrapEnv(1, 1, 1, 0);
    final CountDownLatch serverGotRstLatch = new CountDownLatch(1);
    final CountDownLatch serverWriteHeadersLatch = new CountDownLatch(1);
    final AtomicReference<Throwable> serverWriteHeadersCauseRef = new AtomicReference<Throwable>();
    final int streamId = 3;
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            if (streamId == (Integer) invocationOnMock.getArgument(1)) {
                serverGotRstLatch.countDown();
            }
            return null;
        }
    }).when(serverListener).onRstStreamRead(any(ChannelHandlerContext.class), eq(streamId), anyLong());
    final Http2Headers headers = dummyHeaders();
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Client.encoder().writeHeaders(ctx(), streamId, headers, CONNECTION_STREAM_ID, DEFAULT_PRIORITY_WEIGHT, false, 0, false, newPromise());
            http2Client.encoder().writeRstStream(ctx(), streamId, Http2Error.CANCEL.code(), newPromise());
            http2Client.flush(ctx());
        }
    });
    assertTrue(serverSettingsAckLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    assertTrue(serverGotRstLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    verify(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(streamId), eq(headers), anyInt(), anyShort(), anyBoolean(), anyInt(), eq(false));
    // Now have the server attempt to send a headers frame simulating some asynchronous work.
    runInChannel(serverConnectedChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Server.encoder().writeHeaders(serverCtx(), streamId, headers, 0, true, serverNewPromise()).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    serverWriteHeadersCauseRef.set(future.cause());
                    serverWriteHeadersLatch.countDown();
                }
            });
            http2Server.flush(serverCtx());
        }
    });
    assertTrue(serverWriteHeadersLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    Throwable serverWriteHeadersCause = serverWriteHeadersCauseRef.get();
    assertNotNull(serverWriteHeadersCause);
    assertThat(serverWriteHeadersCauseRef.get(), not(instanceOf(Http2Exception.class)));
    // Server should receive a RST_STREAM for stream 3.
    verify(serverListener, never()).onGoAwayRead(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class));
    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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.jupiter.api.Test)

Example 28 with Http2Runnable

use of io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable in project netty by netty.

the class Http2ConnectionRoundtripTest method writeFailureFlowControllerRemoveFrame.

@Test
public void writeFailureFlowControllerRemoveFrame() throws Exception {
    bootstrapEnv(1, 1, 2, 1);
    final ChannelPromise dataPromise = newPromise();
    final ChannelPromise assertPromise = 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());
            clientChannel.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {

                @Override
                public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                    ReferenceCountUtil.release(msg);
                    // Ensure we update the window size so we will try to write the rest of the frame while
                    // processing the flush.
                    http2Client.encoder().flowController().initialWindowSize(8);
                    promise.setFailure(new IllegalStateException());
                }
            });
            http2Client.encoder().flowController().initialWindowSize(4);
            http2Client.encoder().writeData(ctx(), 3, randomBytes(8), 0, false, dataPromise);
            assertTrue(http2Client.encoder().flowController().hasFlowControlled(http2Client.connection().stream(3)));
            http2Client.flush(ctx());
            try {
                // The Frame should have been removed after the write failed.
                assertFalse(http2Client.encoder().flowController().hasFlowControlled(http2Client.connection().stream(3)));
                assertPromise.setSuccess();
            } catch (Throwable error) {
                assertPromise.setFailure(error);
            }
        }
    });
    ExecutionException e = assertThrows(ExecutionException.class, new Executable() {

        @Override
        public void execute() throws Throwable {
            dataPromise.get();
        }
    });
    assertThat(e.getCause(), is(instanceOf(IllegalStateException.class)));
    assertPromise.sync();
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ExecutionException(java.util.concurrent.ExecutionException) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Example 29 with Http2Runnable

use of io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable in project netty by netty.

the class Http2ConnectionRoundtripTest method nonHttp2ExceptionInPipelineShouldNotCloseConnection.

@Test
public void nonHttp2ExceptionInPipelineShouldNotCloseConnection() throws Exception {
    bootstrapEnv(1, 1, 2, 1);
    // Create a latch to track when the close occurs.
    final CountDownLatch closeLatch = new CountDownLatch(1);
    clientChannel.closeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            closeLatch.countDown();
        }
    });
    // Create a single stream by sending a HEADERS frame to the server.
    final Http2Headers headers = dummyHeaders();
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Client.encoder().writeHeaders(ctx(), 3, headers, 0, (short) 16, false, 0, false, newPromise());
            http2Client.flush(ctx());
        }
    });
    // Wait for the server to create the stream.
    assertTrue(serverSettingsAckLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    assertTrue(requestLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    // Add a handler that will immediately throw an exception.
    clientChannel.pipeline().addFirst(new ChannelHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            throw new RuntimeException("Fake Exception");
        }
    });
    // The close should NOT occur.
    assertFalse(closeLatch.await(2, SECONDS));
    assertTrue(clientChannel.isOpen());
    // Set the timeout very low because we know graceful shutdown won't complete
    setClientGracefulShutdownTime(0);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelFutureListener(io.netty.channel.ChannelFutureListener) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 30 with Http2Runnable

use of io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable in project netty by netty.

the class Http2ConnectionRoundtripTest method http2ExceptionInPipelineShouldCloseConnection.

@Test
public void http2ExceptionInPipelineShouldCloseConnection() throws Exception {
    bootstrapEnv(1, 1, 2, 1);
    // Create a latch to track when the close occurs.
    final CountDownLatch closeLatch = new CountDownLatch(1);
    clientChannel.closeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            closeLatch.countDown();
        }
    });
    // Create a single stream by sending a HEADERS frame to the server.
    final Http2Headers headers = dummyHeaders();
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Client.encoder().writeHeaders(ctx(), 3, headers, 0, (short) 16, false, 0, false, newPromise());
            http2Client.flush(ctx());
        }
    });
    // Wait for the server to create the stream.
    assertTrue(serverSettingsAckLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    assertTrue(requestLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    // Add a handler that will immediately throw an exception.
    clientChannel.pipeline().addFirst(new ChannelHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            throw Http2Exception.connectionError(PROTOCOL_ERROR, "Fake Exception");
        }
    });
    // Wait for the close to occur.
    assertTrue(closeLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    assertFalse(clientChannel.isOpen());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelFutureListener(io.netty.channel.ChannelFutureListener) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)41 Test (org.junit.jupiter.api.Test)37 ByteBuf (io.netty.buffer.ByteBuf)28 AsciiString (io.netty.util.AsciiString)20 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)18 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)12 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)11 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 ChannelFuture (io.netty.channel.ChannelFuture)6 ChannelFutureListener (io.netty.channel.ChannelFutureListener)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ExecutionException (java.util.concurrent.ExecutionException)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 IllegalReferenceCountException (io.netty.util.IllegalReferenceCountException)3 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)2 ChannelPromise (io.netty.channel.ChannelPromise)2