Search in sources :

Example 6 with Http2StreamChannel

use of io.netty.handler.codec.http2.Http2StreamChannel 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 7 with Http2StreamChannel

use of io.netty.handler.codec.http2.Http2StreamChannel 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 8 with Http2StreamChannel

use of io.netty.handler.codec.http2.Http2StreamChannel in project netty by netty.

the class Http2MultiplexTest method channelClosedWhenInactiveFired.

@Test
public void channelClosedWhenInactiveFired() {
    LastInboundHandler inboundHandler = new LastInboundHandler();
    Http2StreamChannel childChannel = newInboundStream(3, false, inboundHandler);
    final AtomicBoolean channelOpen = new AtomicBoolean(false);
    final AtomicBoolean channelActive = new AtomicBoolean(false);
    assertTrue(childChannel.isOpen());
    assertTrue(childChannel.isActive());
    childChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            channelOpen.set(ctx.channel().isOpen());
            channelActive.set(ctx.channel().isActive());
            super.channelInactive(ctx);
        }
    });
    childChannel.close().syncUninterruptibly();
    assertFalse(channelOpen.get());
    assertFalse(channelActive.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 9 with Http2StreamChannel

use of io.netty.handler.codec.http2.Http2StreamChannel in project netty by netty.

the class Http2MultiplexTest method channelReadShouldRespectAutoReadAndNotProduceNPE.

@Test
public void channelReadShouldRespectAutoReadAndNotProduceNPE() throws Exception {
    LastInboundHandler inboundHandler = new LastInboundHandler();
    Http2StreamChannel childChannel = newInboundStream(3, false, inboundHandler);
    assertTrue(childChannel.config().isAutoRead());
    Http2HeadersFrame headersFrame = inboundHandler.readInbound();
    assertNotNull(headersFrame);
    childChannel.config().setAutoRead(false);
    childChannel.pipeline().addFirst(new ChannelInboundHandlerAdapter() {

        private int count;

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ctx.fireChannelRead(msg);
            // Close channel after 2 reads so there is still something in the inboundBuffer when the close happens.
            if (++count == 2) {
                ctx.close();
            }
        }
    });
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("hello world"), 0, false);
    Http2DataFrame dataFrame0 = inboundHandler.readInbound();
    assertNotNull(dataFrame0);
    release(dataFrame0);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("foo"), 0, false);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("bar"), 0, false);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("bar"), 0, false);
    assertNull(inboundHandler.readInbound());
    childChannel.config().setAutoRead(true);
    verifyFramesMultiplexedToCorrectChannel(childChannel, inboundHandler, 3);
    inboundHandler.checkException();
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 10 with Http2StreamChannel

use of io.netty.handler.codec.http2.Http2StreamChannel in project rest.li by linkedin.

the class Http2ChannelLifecycle method doBootstrapStreamChannel.

/**
 * Bootstraps the stream channel from the given parent channel. Returns the stream channel
 * through the success callback if bootstrap succeeds; Return the cause if an exception occurs.
 * @param channel Parent channel to bootstrap the stream channel from.
 * @param callback Callback of the stream channel bootstrap.
 */
private void doBootstrapStreamChannel(Channel channel, Callback<Channel> callback) {
    final Http2StreamChannelBootstrap bootstrap = new Http2StreamChannelBootstrap(channel).handler(new Http2StreamChannelInitializer(_ssl, _maxContentLength));
    bootstrap.open().addListener(future -> {
        if (future.isSuccess()) {
            synchronized (_lock) {
                _childChannelCount++;
            }
            callback.onSuccess((Http2StreamChannel) future.get());
        } else {
            channel.close();
            callback.onError(future.cause());
        }
    });
}
Also used : Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap)

Aggregations

Test (org.junit.jupiter.api.Test)14 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)9 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)9 StreamException (io.netty.handler.codec.http2.Http2Exception.StreamException)9 ChannelPromise (io.netty.channel.ChannelPromise)7 Http2TestUtil.anyChannelPromise (io.netty.handler.codec.http2.Http2TestUtil.anyChannelPromise)7 ClosedChannelException (java.nio.channels.ClosedChannelException)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Channel (io.netty.channel.Channel)5 ChannelFuture (io.netty.channel.ChannelFuture)5 Http2StreamChannel (io.netty.handler.codec.http2.Http2StreamChannel)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 ChannelHandler (io.netty.channel.ChannelHandler)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 Http2StreamChannelBootstrap (io.netty.handler.codec.http2.Http2StreamChannelBootstrap)3 Consumer (io.netty.handler.codec.http2.LastInboundHandler.Consumer)3 ArrayList (java.util.ArrayList)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 MultiplexedChannelRecordTest (com.github.ambry.network.http2.MultiplexedChannelRecordTest)2