Search in sources :

Example 76 with ChannelInboundHandlerAdapter

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

the class LocalChannelTest method testWriteInWritePromiseCompletePreservesOrder.

@Test
public void testWriteInWritePromiseCompletePreservesOrder() throws InterruptedException {
    Bootstrap cb = new Bootstrap();
    ServerBootstrap sb = new ServerBootstrap();
    final CountDownLatch messageLatch = new CountDownLatch(2);
    final ByteBuf data = Unpooled.wrappedBuffer(new byte[1024]);
    final ByteBuf data2 = Unpooled.wrappedBuffer(new byte[512]);
    try {
        cb.group(group1).channel(LocalChannel.class).handler(new TestHandler());
        sb.group(group2).channel(LocalServerChannel.class).childHandler(new ChannelInboundHandlerAdapter() {

            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                final long count = messageLatch.getCount();
                if ((data.equals(msg) && count == 2) || (data2.equals(msg) && count == 1)) {
                    ReferenceCountUtil.safeRelease(msg);
                    messageLatch.countDown();
                } else {
                    super.channelRead(ctx, msg);
                }
            }
        });
        Channel sc = null;
        Channel cc = null;
        try {
            // Start server
            sc = sb.bind(TEST_ADDRESS).syncUninterruptibly().channel();
            // Connect to the server
            cc = cb.connect(sc.localAddress()).syncUninterruptibly().channel();
            final Channel ccCpy = cc;
            // Make sure a write operation is executed in the eventloop
            cc.pipeline().lastContext().executor().execute(new Runnable() {

                @Override
                public void run() {
                    ChannelPromise promise = ccCpy.newPromise();
                    promise.addListener(new ChannelFutureListener() {

                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            ccCpy.writeAndFlush(data2.retainedDuplicate(), ccCpy.newPromise());
                        }
                    });
                    ccCpy.writeAndFlush(data.retainedDuplicate(), promise);
                }
            });
            assertTrue(messageLatch.await(5, SECONDS));
        } finally {
            closeChannel(cc);
            closeChannel(sc);
        }
    } finally {
        data.release();
        data2.release();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AbstractChannel(io.netty.channel.AbstractChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 77 with ChannelInboundHandlerAdapter

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

the class LocalChannelTest method testConnectFutureBeforeChannelActive.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testConnectFutureBeforeChannelActive() throws Exception {
    Bootstrap cb = new Bootstrap();
    ServerBootstrap sb = new ServerBootstrap();
    cb.group(group1).channel(LocalChannel.class).handler(new ChannelInboundHandlerAdapter());
    sb.group(group2).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new TestHandler());
        }
    });
    Channel sc = null;
    Channel cc = null;
    try {
        // Start server
        sc = sb.bind(TEST_ADDRESS).sync().channel();
        cc = cb.register().sync().channel();
        final ChannelPromise promise = cc.newPromise();
        final Promise<Void> assertPromise = cc.eventLoop().newPromise();
        cc.pipeline().addLast(new TestHandler() {

            @Override
            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                // Ensure the promise was done before the handler method is triggered.
                if (promise.isDone()) {
                    assertPromise.setSuccess(null);
                } else {
                    assertPromise.setFailure(new AssertionError("connect promise should be done"));
                }
            }
        });
        // Connect to the server
        cc.connect(sc.localAddress(), promise).sync();
        assertPromise.syncUninterruptibly();
        assertTrue(promise.isSuccess());
    } finally {
        closeChannel(cc);
        closeChannel(sc);
    }
}
Also used : AbstractChannel(io.netty.channel.AbstractChannel) Channel(io.netty.channel.Channel) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 78 with ChannelInboundHandlerAdapter

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

the class EmbeddedChannelTest method testFireChannelInactiveAndUnregistered.

private static void testFireChannelInactiveAndUnregistered(Action action) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(3);
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            latch.countDown();
            ctx.executor().execute(new Runnable() {

                @Override
                public void run() {
                    // Should be executed.
                    latch.countDown();
                }
            });
        }

        @Override
        public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
            latch.countDown();
        }
    });
    action.doRun(channel).syncUninterruptibly();
    latch.await();
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 79 with ChannelInboundHandlerAdapter

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

the class EmbeddedChannelTest method testChannelInactiveFired.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testChannelInactiveFired() throws InterruptedException {
    final AtomicBoolean inactive = new AtomicBoolean();
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            ctx.close();
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            inactive.set(true);
        }
    });
    channel.pipeline().fireExceptionCaught(new IllegalStateException());
    assertTrue(inactive.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 80 with ChannelInboundHandlerAdapter

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

the class EmbeddedChannelTest method testConstructWithChannelInitializer.

@Test
public void testConstructWithChannelInitializer() {
    final Integer first = 1;
    final Integer second = 2;
    final ChannelHandler handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ctx.fireChannelRead(first);
            ctx.fireChannelRead(second);
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(handler);
        }
    });
    ChannelPipeline pipeline = channel.pipeline();
    assertSame(handler, pipeline.firstContext().handler());
    assertTrue(channel.writeInbound(3));
    assertTrue(channel.finish());
    assertSame(first, channel.readInbound());
    assertSame(second, channel.readInbound());
    assertNull(channel.readInbound());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelPipeline(io.netty.channel.ChannelPipeline) 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