Search in sources :

Example 56 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class EmbeddedChannelTest method testFlushInbound.

@Test
public void testFlushInbound() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            latch.countDown();
        }
    });
    channel.flushInbound();
    if (!latch.await(1L, TimeUnit.SECONDS)) {
        fail("Nobody called #channelReadComplete() in time.");
    }
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 57 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class ServerBootstrapTest method testParentHandler.

private static void testParentHandler(boolean channelInitializer) throws Exception {
    final LocalAddress addr = new LocalAddress(UUID.randomUUID().toString());
    final CountDownLatch readLatch = new CountDownLatch(1);
    final CountDownLatch initLatch = new CountDownLatch(1);
    final ChannelHandler handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            initLatch.countDown();
            super.handlerAdded(ctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            readLatch.countDown();
            super.channelRead(ctx, msg);
        }
    };
    EventLoopGroup group = new DefaultEventLoopGroup(1);
    Channel sch = null;
    Channel cch = null;
    try {
        ServerBootstrap sb = new ServerBootstrap();
        sb.channel(LocalServerChannel.class).group(group).childHandler(new ChannelInboundHandlerAdapter());
        if (channelInitializer) {
            sb.handler(new ChannelInitializer<Channel>() {

                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ch.pipeline().addLast(handler);
                }
            });
        } else {
            sb.handler(handler);
        }
        Bootstrap cb = new Bootstrap();
        cb.group(group).channel(LocalChannel.class).handler(new ChannelInboundHandlerAdapter());
        sch = sb.bind(addr).syncUninterruptibly().channel();
        cch = cb.connect(addr).syncUninterruptibly().channel();
        initLatch.await();
        readLatch.await();
    } finally {
        if (sch != null) {
            sch.close().syncUninterruptibly();
        }
        if (cch != null) {
            cch.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 58 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class ServerBootstrapTest method testHandlerRegister.

@Test(timeout = 5000)
public void testHandlerRegister() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    LocalEventLoopGroup group = new LocalEventLoopGroup(1);
    try {
        ServerBootstrap sb = new ServerBootstrap();
        sb.channel(LocalServerChannel.class).group(group).childHandler(new ChannelInboundHandlerAdapter()).handler(new ChannelHandlerAdapter() {

            @Override
            public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
                try {
                    assertTrue(ctx.executor().inEventLoop());
                } catch (Throwable cause) {
                    error.set(cause);
                } finally {
                    latch.countDown();
                }
            }
        });
        sb.register().syncUninterruptibly();
        latch.await();
        assertNull(error.get());
    } finally {
        group.shutdownGracefully();
    }
}
Also used : ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 59 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SocketRstTest method testNoRstIfSoLingerOnClose.

public void testNoRstIfSoLingerOnClose(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    final AtomicReference<Channel> serverChannelRef = new AtomicReference<Channel>();
    final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            serverChannelRef.compareAndSet(null, ch);
            latch.countDown();
        }
    });
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                    throwableRef.compareAndSet(null, cause);
                }

                @Override
                public void channelInactive(ChannelHandlerContext ctx) {
                    latch2.countDown();
                }
            });
        }
    });
    Channel sc = sb.bind().sync().channel();
    cb.connect(sc.localAddress()).syncUninterruptibly();
    // Wait for the server to get setup.
    latch.await();
    // The server has SO_LINGER=0 and so it must send a RST when close is called.
    serverChannelRef.get().close();
    // Wait for the client to get channelInactive.
    latch2.await();
    // Verify the client did not received a RST.
    assertNull(throwableRef.get());
}
Also used : Channel(io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 60 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SocketChannelNotYetConnectedTest method testShutdownNotYetConnected.

public void testShutdownNotYetConnected(Bootstrap cb) throws Throwable {
    SocketChannel ch = (SocketChannel) cb.handler(new ChannelInboundHandlerAdapter()).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
    try {
        try {
            ch.shutdownInput().syncUninterruptibly();
            fail();
        } catch (Throwable cause) {
            checkThrowable(cause);
        }
        try {
            ch.shutdownOutput().syncUninterruptibly();
            fail();
        } catch (Throwable cause) {
            checkThrowable(cause);
        }
    } finally {
        ch.close().syncUninterruptibly();
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Aggregations

ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)103 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)75 Channel (io.netty.channel.Channel)65 Test (org.junit.Test)62 Bootstrap (io.netty.bootstrap.Bootstrap)51 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)48 ChannelFuture (io.netty.channel.ChannelFuture)33 CountDownLatch (java.util.concurrent.CountDownLatch)32 EventLoopGroup (io.netty.channel.EventLoopGroup)28 InetSocketAddress (java.net.InetSocketAddress)26 ClosedChannelException (java.nio.channels.ClosedChannelException)26 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)23 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)23 ByteBuf (io.netty.buffer.ByteBuf)22 ChannelFutureListener (io.netty.channel.ChannelFutureListener)17 LocalServerChannel (io.netty.channel.local.LocalServerChannel)17 SocketChannel (io.netty.channel.socket.SocketChannel)17 LocalAddress (io.netty.channel.local.LocalAddress)16 LocalChannel (io.netty.channel.local.LocalChannel)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)15