Search in sources :

Example 1 with UnorderedThreadPoolEventExecutor

use of io.netty.util.concurrent.UnorderedThreadPoolEventExecutor in project netty by netty.

the class DefaultChannelPipelineTest method testUnorderedEventExecutor.

@Test(timeout = 3000)
public void testUnorderedEventExecutor() throws Throwable {
    ChannelPipeline pipeline1 = new LocalChannel().pipeline();
    EventExecutorGroup eventExecutors = new UnorderedThreadPoolEventExecutor(2);
    EventLoopGroup defaultGroup = new DefaultEventLoopGroup(1);
    try {
        EventLoop eventLoop1 = defaultGroup.next();
        eventLoop1.register(pipeline1.channel()).syncUninterruptibly();
        final CountDownLatch latch = new CountDownLatch(1);
        pipeline1.addLast(eventExecutors, new ChannelInboundHandlerAdapter() {

            @Override
            public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
                // Just block one of the two threads.
                LockSupport.park();
            }

            @Override
            public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                latch.countDown();
            }
        });
        // Trigger an event, as we use UnorderedEventExecutor userEventTriggered should be called even when
        // handlerAdded(...) blocks.
        pipeline1.fireUserEventTriggered("");
        latch.await();
    } finally {
        defaultGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
        eventExecutors.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
    }
}
Also used : DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) LocalChannel(io.netty.channel.local.LocalChannel) UnorderedThreadPoolEventExecutor(io.netty.util.concurrent.UnorderedThreadPoolEventExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Aggregations

LocalChannel (io.netty.channel.local.LocalChannel)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 OioEventLoopGroup (io.netty.channel.oio.OioEventLoopGroup)1 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)1 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)1 UnorderedThreadPoolEventExecutor (io.netty.util.concurrent.UnorderedThreadPoolEventExecutor)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1