Search in sources :

Example 6 with DefaultEventExecutorGroup

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

the class DefaultChannelPipelineTest method testHandlerRemovedExceptionFromChildHandlerIsPropegated.

@Test(timeout = 3000)
public void testHandlerRemovedExceptionFromChildHandlerIsPropegated() {
    final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
    try {
        final Promise<Void> promise = group1.next().newPromise();
        String handlerName = "foo";
        final Exception exception = new RuntimeException();
        ChannelPipeline pipeline = new LocalChannel().pipeline();
        pipeline.addLast(handlerName, new ChannelHandlerAdapter() {

            @Override
            public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
                throw exception;
            }
        });
        pipeline.addLast(group1, new CheckExceptionHandler(exception, promise));
        group.register(pipeline.channel()).syncUninterruptibly();
        pipeline.remove(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) Test(org.junit.Test)

Example 7 with DefaultEventExecutorGroup

use of io.netty.util.concurrent.DefaultEventExecutorGroup 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 8 with DefaultEventExecutorGroup

use of io.netty.util.concurrent.DefaultEventExecutorGroup 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 9 with DefaultEventExecutorGroup

use of io.netty.util.concurrent.DefaultEventExecutorGroup 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 10 with DefaultEventExecutorGroup

use of io.netty.util.concurrent.DefaultEventExecutorGroup in project rest.li by linkedin.

the class HttpNettyServer method start.

@Override
public void start() {
    _eventExecutors = new DefaultEventExecutorGroup(_threadPoolSize);
    _bossGroup = new NioEventLoopGroup(1, new NamedThreadFactory("R2 Nio Boss"));
    _workerGroup = new NioEventLoopGroup(0, new NamedThreadFactory("R2 Nio Worker"));
    ServerBootstrap bootstrap = new ServerBootstrap().group(_bossGroup, _workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<NioSocketChannel>() {

        @Override
        protected void initChannel(NioSocketChannel ch) throws Exception {
            ch.pipeline().addLast("decoder", new HttpRequestDecoder());
            ch.pipeline().addLast("aggregator", new HttpObjectAggregator(1048576));
            ch.pipeline().addLast("encoder", new HttpResponseEncoder());
            ch.pipeline().addLast("rapi", new RAPServerCodec());
            ch.pipeline().addLast(_eventExecutors, "handler", _restOverStream ? new StreamHandler() : new RestHandler());
        }
    });
    bootstrap.bind(new InetSocketAddress(_port));
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)16 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)11 Test (org.junit.Test)9 LocalChannel (io.netty.channel.local.LocalChannel)6 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)4 EventLoopGroup (io.netty.channel.EventLoopGroup)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 Channel (io.netty.channel.Channel)3 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)3 SocketChannel (io.netty.channel.socket.SocketChannel)3 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)3 EventExecutor (io.netty.util.concurrent.EventExecutor)3 ThreadFactory (java.util.concurrent.ThreadFactory)3 ChannelHandler (io.netty.channel.ChannelHandler)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)2 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)2 AbstractEventExecutor (io.netty.util.concurrent.AbstractEventExecutor)2 ImmediateEventExecutor (io.netty.util.concurrent.ImmediateEventExecutor)2