Search in sources :

Example 6 with EventExecutorGroup

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

the class DefaultChannelPipelineTest method testHandlerAddedAndRemovedCalledInCorrectOrder.

@Test(timeout = 3000)
public void testHandlerAddedAndRemovedCalledInCorrectOrder() throws Throwable {
    final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
    final EventExecutorGroup group2 = new DefaultEventExecutorGroup(1);
    try {
        BlockingQueue<CheckOrderHandler> addedQueue = new LinkedBlockingQueue<CheckOrderHandler>();
        BlockingQueue<CheckOrderHandler> removedQueue = new LinkedBlockingQueue<CheckOrderHandler>();
        CheckOrderHandler handler1 = new CheckOrderHandler(addedQueue, removedQueue);
        CheckOrderHandler handler2 = new CheckOrderHandler(addedQueue, removedQueue);
        CheckOrderHandler handler3 = new CheckOrderHandler(addedQueue, removedQueue);
        CheckOrderHandler handler4 = new CheckOrderHandler(addedQueue, removedQueue);
        ChannelPipeline pipeline = new LocalChannel().pipeline();
        pipeline.addLast(handler1);
        group.register(pipeline.channel()).syncUninterruptibly();
        pipeline.addLast(group1, handler2);
        pipeline.addLast(group2, handler3);
        pipeline.addLast(handler4);
        assertTrue(removedQueue.isEmpty());
        pipeline.channel().close().syncUninterruptibly();
        assertHandler(addedQueue.take(), handler1);
        // Depending on timing this can be handler2 or handler3 as these use different EventExecutorGroups.
        assertHandler(addedQueue.take(), handler2, handler3, handler4);
        assertHandler(addedQueue.take(), handler2, handler3, handler4);
        assertHandler(addedQueue.take(), handler2, handler3, handler4);
        assertTrue(addedQueue.isEmpty());
        assertHandler(removedQueue.take(), handler4);
        assertHandler(removedQueue.take(), handler3);
        assertHandler(removedQueue.take(), handler2);
        assertHandler(removedQueue.take(), handler1);
        assertTrue(removedQueue.isEmpty());
    } finally {
        group1.shutdownGracefully();
        group2.shutdownGracefully();
    }
}
Also used : DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) LocalChannel(io.netty.channel.local.LocalChannel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 7 with EventExecutorGroup

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

the class DefaultChannelPipelineTest method testHandlerAddedThrowsAndRemovedThrowsException.

@Test(timeout = 3000)
public void testHandlerAddedThrowsAndRemovedThrowsException() throws InterruptedException {
    final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        final Promise<Void> promise = group1.next().newPromise();
        final Exception exceptionAdded = new RuntimeException();
        final Exception exceptionRemoved = new RuntimeException();
        String handlerName = "foo";
        ChannelPipeline pipeline = new LocalChannel().pipeline();
        pipeline.addLast(group1, new CheckExceptionHandler(exceptionAdded, promise));
        pipeline.addFirst(handlerName, new ChannelHandlerAdapter() {

            @Override
            public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
                throw exceptionAdded;
            }

            @Override
            public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
                // Execute this later so we are sure the exception is handled first.
                ctx.executor().execute(new Runnable() {

                    @Override
                    public void run() {
                        latch.countDown();
                    }
                });
                throw exceptionRemoved;
            }
        });
        group.register(pipeline.channel()).syncUninterruptibly();
        latch.await();
        assertNull(pipeline.context(handlerName));
        promise.syncUninterruptibly();
    } finally {
        group1.shutdownGracefully();
    }
}
Also used : DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) LocalChannel(io.netty.channel.local.LocalChannel) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 8 with EventExecutorGroup

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

the class AbstractEventLoopTest method testReregister.

/**
     * Test for https://github.com/netty/netty/issues/803
     */
@Test
public void testReregister() {
    EventLoopGroup group = newEventLoopGroup();
    EventLoopGroup group2 = newEventLoopGroup();
    final EventExecutorGroup eventExecutorGroup = new DefaultEventExecutorGroup(2);
    ServerBootstrap bootstrap = new ServerBootstrap();
    ChannelFuture future = bootstrap.channel(newChannel()).group(group).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
        }
    }).handler(new ChannelInitializer<ServerSocketChannel>() {

        @Override
        public void initChannel(ServerSocketChannel ch) throws Exception {
            ch.pipeline().addLast(new TestChannelHandler());
            ch.pipeline().addLast(eventExecutorGroup, new TestChannelHandler2());
        }
    }).bind(0).awaitUninterruptibly();
    EventExecutor executor = future.channel().pipeline().context(TestChannelHandler2.class).executor();
    EventExecutor executor1 = future.channel().pipeline().context(TestChannelHandler.class).executor();
    future.channel().deregister().awaitUninterruptibly();
    Channel channel = group2.register(future.channel()).awaitUninterruptibly().channel();
    EventExecutor executorNew = channel.pipeline().context(TestChannelHandler.class).executor();
    assertNotSame(executor1, executorNew);
    assertSame(executor, future.channel().pipeline().context(TestChannelHandler2.class).executor());
}
Also used : DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventExecutor(io.netty.util.concurrent.EventExecutor) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) Test(org.junit.Test)

Example 9 with EventExecutorGroup

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

the class DefaultChannelPipeline method childExecutor.

private EventExecutor childExecutor(EventExecutorGroup group) {
    if (group == null) {
        return null;
    }
    Boolean pinEventExecutor = channel.config().getOption(ChannelOption.SINGLE_EVENTEXECUTOR_PER_GROUP);
    if (pinEventExecutor != null && !pinEventExecutor) {
        return group.next();
    }
    Map<EventExecutorGroup, EventExecutor> childExecutors = this.childExecutors;
    if (childExecutors == null) {
        // Use size of 4 as most people only use one extra EventExecutor.
        childExecutors = this.childExecutors = new IdentityHashMap<EventExecutorGroup, EventExecutor>(4);
    }
    // Pin one of the child executors once and remember it so that the same child executor
    // is used to fire events for the same channel.
    EventExecutor childExecutor = childExecutors.get(group);
    if (childExecutor == null) {
        childExecutor = group.next();
        childExecutors.put(group, childExecutor);
    }
    return childExecutor;
}
Also used : EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) EventExecutor(io.netty.util.concurrent.EventExecutor) IdentityHashMap(java.util.IdentityHashMap)

Example 10 with EventExecutorGroup

use of io.netty.util.concurrent.EventExecutorGroup in project riposte by Nike-Inc.

the class Server method shutdown.

public void shutdown() throws InterruptedException {
    try {
        logger.info("Shutting down Riposte...");
        List<ChannelFuture> channelCloseFutures = new ArrayList<>();
        for (Channel ch : channels) {
            // execute shutdown hooks
            if (serverConfig.serverShutdownHooks() != null) {
                for (ServerShutdownHook hook : serverConfig.serverShutdownHooks()) {
                    hook.executeServerShutdownHook(serverConfig, ch);
                }
            }
            channelCloseFutures.add(ch.close());
        }
        for (ChannelFuture chf : channelCloseFutures) {
            chf.sync();
        }
    } finally {
        eventLoopGroups.forEach(EventExecutorGroup::shutdownGracefully);
        logger.info("...Riposte shutdown complete");
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) ServerShutdownHook(com.nike.riposte.server.hooks.ServerShutdownHook)

Aggregations

EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)17 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)13 Test (org.junit.Test)10 LocalChannel (io.netty.channel.local.LocalChannel)7 Channel (io.netty.channel.Channel)5 EventLoopGroup (io.netty.channel.EventLoopGroup)4 EventExecutor (io.netty.util.concurrent.EventExecutor)4 ChannelHandler (io.netty.channel.ChannelHandler)3 ChannelPipeline (io.netty.channel.ChannelPipeline)3 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)3 SocketChannel (io.netty.channel.socket.SocketChannel)3 SslHandler (io.netty.handler.ssl.SslHandler)3 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)3 UnorderedThreadPoolEventExecutor (io.netty.util.concurrent.UnorderedThreadPoolEventExecutor)3 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2 ChannelFuture (io.netty.channel.ChannelFuture)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 AbstractEventExecutor (io.netty.util.concurrent.AbstractEventExecutor)2 ImmediateEventExecutor (io.netty.util.concurrent.ImmediateEventExecutor)2