Search in sources :

Example 91 with ChannelInboundHandlerAdapter

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

the class FlowControlHandlerTest method testRemoveFlowControl.

@Test
public void testRemoveFlowControl() throws Exception {
    final CountDownLatch latch = new CountDownLatch(3);
    ChannelInboundHandlerAdapter handler = new ChannelDuplexHandler() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            // do the first read
            ctx.read();
            super.channelActive(ctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            latch.countDown();
            super.channelRead(ctx, msg);
        }
    };
    FlowControlHandler flow = new FlowControlHandler() {

        private int num;

        @Override
        public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
            super.channelRead(ctx, msg);
            ++num;
            if (num >= 3) {
                // We have received 3 messages. Remove myself later
                final ChannelHandler handler = this;
                ctx.channel().eventLoop().execute(new Runnable() {

                    @Override
                    public void run() {
                        ctx.pipeline().remove(handler);
                    }
                });
            }
        }
    };
    ChannelInboundHandlerAdapter tail = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            // consume this msg
            ReferenceCountUtil.release(msg);
        }
    };
    Channel server = newServer(false, /* no auto read */
    flow, handler, tail);
    Channel client = newClient(server.localAddress());
    try {
        // Write one message
        client.writeAndFlush(newOneMessage()).sync();
        // We should receive 3 messages
        assertTrue(latch.await(1L, SECONDS));
        assertTrue(flow.isQueueEmpty());
    } finally {
        client.close();
        server.close();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 92 with ChannelInboundHandlerAdapter

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

the class FlowControlHandlerTest method testSwallowedReadComplete.

@Test
public void testSwallowedReadComplete() throws Exception {
    final long delayMillis = 100;
    final Queue<IdleStateEvent> userEvents = new LinkedBlockingQueue<IdleStateEvent>();
    final EmbeddedChannel channel = new EmbeddedChannel(false, false, new FlowControlHandler(), new IdleStateHandler(delayMillis, 0, 0, MILLISECONDS), new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            ctx.fireChannelActive();
            ctx.read();
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            ctx.fireChannelRead(msg);
            ctx.read();
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) {
            ctx.fireChannelReadComplete();
            ctx.read();
        }

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
            if (evt instanceof IdleStateEvent) {
                userEvents.add((IdleStateEvent) evt);
            }
            ctx.fireUserEventTriggered(evt);
        }
    });
    channel.config().setAutoRead(false);
    assertFalse(channel.config().isAutoRead());
    channel.register();
    // Reset read timeout by some message
    assertTrue(channel.writeInbound(Unpooled.EMPTY_BUFFER));
    channel.flushInbound();
    assertEquals(Unpooled.EMPTY_BUFFER, channel.readInbound());
    // Emulate 'no more messages in NIO channel' on the next read attempt.
    channel.flushInbound();
    assertNull(channel.readInbound());
    Thread.sleep(delayMillis + 20L);
    channel.runPendingTasks();
    assertEquals(IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT, userEvents.poll());
    assertFalse(channel.finish());
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 93 with ChannelInboundHandlerAdapter

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

the class FlowControlHandlerTest method testFlowAutoReadOff.

/**
 * The {@link FlowControlHandler} will pass down messages one by one
 * if auto reading is off and the user is calling {@code read()} on
 * their own.
 */
@Test
public void testFlowAutoReadOff() throws Exception {
    final Exchanger<Channel> peerRef = new Exchanger<Channel>();
    final CountDownLatch msgRcvLatch1 = new CountDownLatch(1);
    final CountDownLatch msgRcvLatch2 = new CountDownLatch(2);
    final CountDownLatch msgRcvLatch3 = new CountDownLatch(3);
    ChannelInboundHandlerAdapter handler = new ChannelDuplexHandler() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            ctx.fireChannelActive();
            peerRef.exchange(ctx.channel(), 1L, SECONDS);
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            msgRcvLatch1.countDown();
            msgRcvLatch2.countDown();
            msgRcvLatch3.countDown();
        }
    };
    FlowControlHandler flow = new FlowControlHandler();
    Channel server = newServer(false, flow, handler);
    Channel client = newClient(server.localAddress());
    try {
        // The client connection on the server side
        Channel peer = peerRef.exchange(null, 1L, SECONDS);
        // Write the message
        client.writeAndFlush(newOneMessage()).syncUninterruptibly();
        // channelRead(1)
        peer.read();
        assertTrue(msgRcvLatch1.await(1L, SECONDS));
        // channelRead(2)
        peer.read();
        assertTrue(msgRcvLatch2.await(1L, SECONDS));
        // channelRead(3)
        peer.read();
        assertTrue(msgRcvLatch3.await(1L, SECONDS));
        assertTrue(flow.isQueueEmpty());
    } finally {
        client.close();
        server.close();
    }
}
Also used : Exchanger(java.util.concurrent.Exchanger) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 94 with ChannelInboundHandlerAdapter

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

the class FlowControlHandlerTest method newClient.

private static Channel newClient(SocketAddress server) {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(GROUP).channel(NioSocketChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000).handler(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            fail("In this test the client is never receiving a message from the server.");
        }
    });
    return bootstrap.connect(server).syncUninterruptibly().channel();
}
Also used : Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 95 with ChannelInboundHandlerAdapter

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

the class FlowControlHandlerTest method testAutoReadingOff.

/**
 * This test demonstrates the default behavior if auto reading
 * is turned off from the get-go and you're calling read() in
 * the hope that only one message will be returned.
 *
 * NOTE: This test waits for the client to disconnect which is
 * interpreted as the signal that all {@code byte}s have been
 * transferred to the server.
 */
@Test
public void testAutoReadingOff() throws Exception {
    final Exchanger<Channel> peerRef = new Exchanger<Channel>();
    final CountDownLatch latch = new CountDownLatch(3);
    ChannelInboundHandlerAdapter handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            peerRef.exchange(ctx.channel(), 1L, SECONDS);
            ctx.fireChannelActive();
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            ReferenceCountUtil.release(msg);
            latch.countDown();
        }
    };
    Channel server = newServer(false, handler);
    Channel client = newClient(server.localAddress());
    try {
        // The client connection on the server side
        Channel peer = peerRef.exchange(null, 1L, SECONDS);
        // Write the message
        client.writeAndFlush(newOneMessage()).syncUninterruptibly();
        // Read the message
        peer.read();
        // We received all three messages but hoped that only one
        // message was read because auto reading was off and we
        // invoked the read() method only once.
        assertTrue(latch.await(1L, SECONDS));
    } finally {
        client.close();
        server.close();
    }
}
Also used : Exchanger(java.util.concurrent.Exchanger) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

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