Search in sources :

Example 61 with ChannelInboundHandlerAdapter

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

the class Http2MultiplexTest method readUnkownFrame.

@Test
public void readUnkownFrame() {
    LastInboundHandler handler = new LastInboundHandler();
    Http2StreamChannel channel = newInboundStream(3, true, handler);
    frameInboundWriter.writeInboundFrame((byte) 99, channel.stream().id(), new Http2Flags(), Unpooled.EMPTY_BUFFER);
    // header frame and unknown frame
    verifyFramesMultiplexedToCorrectChannel(channel, handler, 2);
    Channel childChannel = newOutboundStream(new ChannelInboundHandlerAdapter());
    assertTrue(childChannel.isActive());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 62 with ChannelInboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter 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 63 with ChannelInboundHandlerAdapter

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

the class Http2MultiplexTest method newInboundStream.

private Http2StreamChannel newInboundStream(int streamId, boolean endStream, AtomicInteger maxReads, final ChannelHandler childHandler) {
    final AtomicReference<Http2StreamChannel> streamChannelRef = new AtomicReference<Http2StreamChannel>();
    childChannelInitializer.maxReads = maxReads;
    childChannelInitializer.handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) {
            assertNull(streamChannelRef.get());
            streamChannelRef.set((Http2StreamChannel) ctx.channel());
            ctx.pipeline().addLast(childHandler);
            ctx.fireChannelRegistered();
        }
    };
    frameInboundWriter.writeInboundHeaders(streamId, request, 0, endStream);
    parentChannel.runPendingTasks();
    Http2StreamChannel channel = streamChannelRef.get();
    assertEquals(streamId, channel.stream().id());
    return channel;
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 64 with ChannelInboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter 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 65 with ChannelInboundHandlerAdapter

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

the class SocketConnectTest method testLocalAddressAfterConnect.

public void testLocalAddressAfterConnect(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    Channel serverChannel = null;
    Channel clientChannel = null;
    try {
        final Promise<InetSocketAddress> localAddressPromise = ImmediateEventExecutor.INSTANCE.newPromise();
        serverChannel = sb.childHandler(new ChannelInboundHandlerAdapter() {

            @Override
            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                localAddressPromise.setSuccess((InetSocketAddress) ctx.channel().localAddress());
            }
        }).bind().syncUninterruptibly().channel();
        clientChannel = cb.handler(new ChannelInboundHandlerAdapter()).register().syncUninterruptibly().channel();
        assertNull(clientChannel.localAddress());
        assertNull(clientChannel.remoteAddress());
        clientChannel.connect(serverChannel.localAddress()).syncUninterruptibly().channel();
        assertLocalAddress((InetSocketAddress) clientChannel.localAddress());
        assertNotNull(clientChannel.remoteAddress());
        assertLocalAddress(localAddressPromise.get());
    } finally {
        if (clientChannel != null) {
            clientChannel.close().syncUninterruptibly();
        }
        if (serverChannel != null) {
            serverChannel.close().syncUninterruptibly();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Aggregations

ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)248 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)192 Channel (io.netty.channel.Channel)132 Bootstrap (io.netty.bootstrap.Bootstrap)109 Test (org.junit.jupiter.api.Test)102 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)99 ChannelFuture (io.netty.channel.ChannelFuture)71 CountDownLatch (java.util.concurrent.CountDownLatch)70 InetSocketAddress (java.net.InetSocketAddress)66 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)54 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)53 EventLoopGroup (io.netty.channel.EventLoopGroup)52 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)51 ByteBuf (io.netty.buffer.ByteBuf)47 AtomicReference (java.util.concurrent.atomic.AtomicReference)47 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)46 ClosedChannelException (java.nio.channels.ClosedChannelException)46 LocalServerChannel (io.netty.channel.local.LocalServerChannel)44 LocalChannel (io.netty.channel.local.LocalChannel)42 SocketChannel (io.netty.channel.socket.SocketChannel)39