Search in sources :

Example 26 with EventLoopGroup

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

the class LocalTransportThreadModelTest method testStagedExecution.

@Test(timeout = 5000)
public void testStagedExecution() throws Throwable {
    EventLoopGroup l = new DefaultEventLoopGroup(4, new DefaultThreadFactory("l"));
    EventExecutorGroup e1 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e1"));
    EventExecutorGroup e2 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e2"));
    ThreadNameAuditor h1 = new ThreadNameAuditor();
    ThreadNameAuditor h2 = new ThreadNameAuditor();
    ThreadNameAuditor h3 = new ThreadNameAuditor(true);
    Channel ch = new LocalChannel();
    // With no EventExecutor specified, h1 will be always invoked by EventLoop 'l'.
    ch.pipeline().addLast(h1);
    // h2 will be always invoked by EventExecutor 'e1'.
    ch.pipeline().addLast(e1, h2);
    // h3 will be always invoked by EventExecutor 'e2'.
    ch.pipeline().addLast(e2, h3);
    l.register(ch).sync().channel().connect(localAddr).sync();
    // Fire inbound events from all possible starting points.
    ch.pipeline().fireChannelRead("1");
    ch.pipeline().context(h1).fireChannelRead("2");
    ch.pipeline().context(h2).fireChannelRead("3");
    ch.pipeline().context(h3).fireChannelRead("4");
    // Fire outbound events from all possible starting points.
    ch.pipeline().write("5");
    ch.pipeline().context(h3).write("6");
    ch.pipeline().context(h2).write("7");
    ch.pipeline().context(h1).writeAndFlush("8").sync();
    ch.close().sync();
    // Wait until all events are handled completely.
    while (h1.outboundThreadNames.size() < 3 || h3.inboundThreadNames.size() < 3 || h1.removalThreadNames.size() < 1) {
        if (h1.exception.get() != null) {
            throw h1.exception.get();
        }
        if (h2.exception.get() != null) {
            throw h2.exception.get();
        }
        if (h3.exception.get() != null) {
            throw h3.exception.get();
        }
        Thread.sleep(10);
    }
    String currentName = Thread.currentThread().getName();
    try {
        // Events should never be handled from the current thread.
        Assert.assertFalse(h1.inboundThreadNames.contains(currentName));
        Assert.assertFalse(h2.inboundThreadNames.contains(currentName));
        Assert.assertFalse(h3.inboundThreadNames.contains(currentName));
        Assert.assertFalse(h1.outboundThreadNames.contains(currentName));
        Assert.assertFalse(h2.outboundThreadNames.contains(currentName));
        Assert.assertFalse(h3.outboundThreadNames.contains(currentName));
        Assert.assertFalse(h1.removalThreadNames.contains(currentName));
        Assert.assertFalse(h2.removalThreadNames.contains(currentName));
        Assert.assertFalse(h3.removalThreadNames.contains(currentName));
        // Assert that events were handled by the correct executor.
        for (String name : h1.inboundThreadNames) {
            Assert.assertTrue(name.startsWith("l-"));
        }
        for (String name : h2.inboundThreadNames) {
            Assert.assertTrue(name.startsWith("e1-"));
        }
        for (String name : h3.inboundThreadNames) {
            Assert.assertTrue(name.startsWith("e2-"));
        }
        for (String name : h1.outboundThreadNames) {
            Assert.assertTrue(name.startsWith("l-"));
        }
        for (String name : h2.outboundThreadNames) {
            Assert.assertTrue(name.startsWith("e1-"));
        }
        for (String name : h3.outboundThreadNames) {
            Assert.assertTrue(name.startsWith("e2-"));
        }
        for (String name : h1.removalThreadNames) {
            Assert.assertTrue(name.startsWith("l-"));
        }
        for (String name : h2.removalThreadNames) {
            Assert.assertTrue(name.startsWith("e1-"));
        }
        for (String name : h3.removalThreadNames) {
            Assert.assertTrue(name.startsWith("e2-"));
        }
        // Assert that the events for the same handler were handled by the same thread.
        Set<String> names = new HashSet<String>();
        names.addAll(h1.inboundThreadNames);
        names.addAll(h1.outboundThreadNames);
        names.addAll(h1.removalThreadNames);
        Assert.assertEquals(1, names.size());
        names.clear();
        names.addAll(h2.inboundThreadNames);
        names.addAll(h2.outboundThreadNames);
        names.addAll(h2.removalThreadNames);
        Assert.assertEquals(1, names.size());
        names.clear();
        names.addAll(h3.inboundThreadNames);
        names.addAll(h3.outboundThreadNames);
        names.addAll(h3.removalThreadNames);
        Assert.assertEquals(1, names.size());
        // Count the number of events
        Assert.assertEquals(1, h1.inboundThreadNames.size());
        Assert.assertEquals(2, h2.inboundThreadNames.size());
        Assert.assertEquals(3, h3.inboundThreadNames.size());
        Assert.assertEquals(3, h1.outboundThreadNames.size());
        Assert.assertEquals(2, h2.outboundThreadNames.size());
        Assert.assertEquals(1, h3.outboundThreadNames.size());
        Assert.assertEquals(1, h1.removalThreadNames.size());
        Assert.assertEquals(1, h2.removalThreadNames.size());
        Assert.assertEquals(1, h3.removalThreadNames.size());
    } catch (AssertionError e) {
        System.out.println("H1I: " + h1.inboundThreadNames);
        System.out.println("H2I: " + h2.inboundThreadNames);
        System.out.println("H3I: " + h3.inboundThreadNames);
        System.out.println("H1O: " + h1.outboundThreadNames);
        System.out.println("H2O: " + h2.outboundThreadNames);
        System.out.println("H3O: " + h3.outboundThreadNames);
        System.out.println("H1R: " + h1.removalThreadNames);
        System.out.println("H2R: " + h2.removalThreadNames);
        System.out.println("H3R: " + h3.removalThreadNames);
        throw e;
    } finally {
        l.shutdownGracefully();
        e1.shutdownGracefully();
        e2.shutdownGracefully();
        l.terminationFuture().sync();
        e1.terminationFuture().sync();
        e2.terminationFuture().sync();
    }
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) Channel(io.netty.channel.Channel) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with EventLoopGroup

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

the class LocalTransportThreadModelTest3 method testConcurrentAddRemove.

private static void testConcurrentAddRemove(boolean inbound) throws Exception {
    EventLoopGroup l = new DefaultEventLoopGroup(4, new DefaultThreadFactory("l"));
    EventExecutorGroup e1 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e1"));
    EventExecutorGroup e2 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e2"));
    EventExecutorGroup e3 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e3"));
    EventExecutorGroup e4 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e4"));
    EventExecutorGroup e5 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e5"));
    final EventExecutorGroup[] groups = { e1, e2, e3, e4, e5 };
    try {
        Deque<EventType> events = new ConcurrentLinkedDeque<EventType>();
        final EventForwarder h1 = new EventForwarder();
        final EventForwarder h2 = new EventForwarder();
        final EventForwarder h3 = new EventForwarder();
        final EventForwarder h4 = new EventForwarder();
        final EventForwarder h5 = new EventForwarder();
        final EventRecorder h6 = new EventRecorder(events, inbound);
        final Channel ch = new LocalChannel();
        if (!inbound) {
            ch.config().setAutoRead(false);
        }
        ch.pipeline().addLast(e1, h1).addLast(e1, h2).addLast(e1, h3).addLast(e1, h4).addLast(e1, h5).addLast(e1, "recorder", h6);
        l.register(ch).sync().channel().connect(localAddr).sync();
        final LinkedList<EventType> expectedEvents = events(inbound, 8192);
        Throwable cause = new Throwable();
        Thread pipelineModifier = new Thread(new Runnable() {

            @Override
            public void run() {
                Random random = new Random();
                while (true) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        return;
                    }
                    if (!ch.isRegistered()) {
                        continue;
                    }
                    //EventForwardHandler forwardHandler = forwarders[random.nextInt(forwarders.length)];
                    ChannelHandler handler = ch.pipeline().removeFirst();
                    ch.pipeline().addBefore(groups[random.nextInt(groups.length)], "recorder", UUID.randomUUID().toString(), handler);
                }
            }
        });
        pipelineModifier.setDaemon(true);
        pipelineModifier.start();
        for (EventType event : expectedEvents) {
            switch(event) {
                case EXCEPTION_CAUGHT:
                    ch.pipeline().fireExceptionCaught(cause);
                    break;
                case MESSAGE_RECEIVED:
                    ch.pipeline().fireChannelRead("");
                    break;
                case MESSAGE_RECEIVED_LAST:
                    ch.pipeline().fireChannelReadComplete();
                    break;
                case USER_EVENT:
                    ch.pipeline().fireUserEventTriggered("");
                    break;
                case WRITE:
                    ch.pipeline().write("");
                    break;
                case READ:
                    ch.pipeline().read();
                    break;
            }
        }
        ch.close().sync();
        while (events.peekLast() != EventType.UNREGISTERED) {
            Thread.sleep(10);
        }
        expectedEvents.addFirst(EventType.ACTIVE);
        expectedEvents.addFirst(EventType.REGISTERED);
        expectedEvents.addLast(EventType.INACTIVE);
        expectedEvents.addLast(EventType.UNREGISTERED);
        for (; ; ) {
            EventType event = events.poll();
            if (event == null) {
                Assert.assertTrue("Missing events:" + expectedEvents, expectedEvents.isEmpty());
                break;
            }
            Assert.assertEquals(event, expectedEvents.poll());
        }
    } finally {
        l.shutdownGracefully();
        e1.shutdownGracefully();
        e2.shutdownGracefully();
        e3.shutdownGracefully();
        e4.shutdownGracefully();
        e5.shutdownGracefully();
        l.terminationFuture().sync();
        e1.terminationFuture().sync();
        e2.terminationFuture().sync();
        e3.terminationFuture().sync();
        e4.terminationFuture().sync();
        e5.terminationFuture().sync();
    }
}
Also used : DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) Channel(io.netty.channel.Channel) ChannelHandler(io.netty.channel.ChannelHandler) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Random(java.util.Random)

Example 28 with EventLoopGroup

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

the class NioSocketChannelTest method testChannelReRegisterRead.

private static void testChannelReRegisterRead(final boolean sameEventLoop) throws Exception {
    final EventLoopGroup group = new NioEventLoopGroup(2);
    final CountDownLatch latch = new CountDownLatch(1);
    // Just some random bytes
    byte[] bytes = new byte[1024];
    PlatformDependent.threadLocalRandom().nextBytes(bytes);
    Channel sc = null;
    Channel cc = null;
    ServerBootstrap b = new ServerBootstrap();
    try {
        b.group(group).channel(NioServerSocketChannel.class).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new SimpleChannelInboundHandler<ByteBuf>() {

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) {
                        // We was able to read something from the Channel after reregister.
                        latch.countDown();
                    }

                    @Override
                    public void channelActive(final ChannelHandlerContext ctx) throws Exception {
                        final EventLoop loop = group.next();
                        if (sameEventLoop) {
                            deregister(ctx, loop);
                        } else {
                            loop.execute(new Runnable() {

                                @Override
                                public void run() {
                                    deregister(ctx, loop);
                                }
                            });
                        }
                    }

                    private void deregister(ChannelHandlerContext ctx, final EventLoop loop) {
                        // As soon as the channel becomes active re-register it to another
                        // EventLoop. After this is done we should still receive the data that
                        // was written to the channel.
                        ctx.deregister().addListener(new ChannelFutureListener() {

                            @Override
                            public void operationComplete(ChannelFuture cf) {
                                Channel channel = cf.channel();
                                assertNotSame(loop, channel.eventLoop());
                                group.next().register(channel);
                            }
                        });
                    }
                });
            }
        });
        sc = b.bind(0).syncUninterruptibly().channel();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class);
        bootstrap.handler(new ChannelInboundHandlerAdapter());
        cc = bootstrap.connect(sc.localAddress()).syncUninterruptibly().channel();
        cc.writeAndFlush(Unpooled.wrappedBuffer(bytes)).syncUninterruptibly();
        latch.await();
    } finally {
        if (cc != null) {
            cc.close();
        }
        if (sc != null) {
            sc.close();
        }
        group.shutdownGracefully();
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelPipeline(io.netty.channel.ChannelPipeline) EventLoopGroup(io.netty.channel.EventLoopGroup) EventLoop(io.netty.channel.EventLoop) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 29 with EventLoopGroup

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

the class OioEventLoopTest method testTooManyServerChannels.

@Test
public void testTooManyServerChannels() throws Exception {
    EventLoopGroup g = new OioEventLoopGroup(1);
    ServerBootstrap b = new ServerBootstrap();
    b.channel(OioServerSocketChannel.class);
    b.group(g);
    b.childHandler(new ChannelInboundHandlerAdapter());
    ChannelFuture f1 = b.bind(0);
    f1.sync();
    ChannelFuture f2 = b.bind(0);
    f2.await();
    assertThat(f2.cause(), is(instanceOf(ChannelException.class)));
    assertThat(f2.cause().getMessage().toLowerCase(), containsString("too many channels"));
    final CountDownLatch notified = new CountDownLatch(1);
    f2.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            notified.countDown();
        }
    });
    notified.await();
    g.shutdownGracefully();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EventLoopGroup(io.netty.channel.EventLoopGroup) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelException(io.netty.channel.ChannelException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 30 with EventLoopGroup

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

the class FixedChannelPoolTest method testAcquireNewConnection.

@Test
public void testAcquireNewConnection() throws Exception {
    EventLoopGroup group = new LocalEventLoopGroup();
    LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);
    Bootstrap cb = new Bootstrap();
    cb.remoteAddress(addr);
    cb.group(group).channel(LocalChannel.class);
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
        }
    });
    // Start server
    Channel sc = sb.bind(addr).syncUninterruptibly().channel();
    ChannelPoolHandler handler = new TestChannelPoolHandler();
    ChannelPool pool = new FixedChannelPool(cb, handler, ChannelHealthChecker.ACTIVE, AcquireTimeoutAction.NEW, 500, 1, Integer.MAX_VALUE);
    Channel channel = pool.acquire().syncUninterruptibly().getNow();
    Channel channel2 = pool.acquire().syncUninterruptibly().getNow();
    assertNotSame(channel, channel2);
    sc.close().syncUninterruptibly();
    channel.close().syncUninterruptibly();
    channel2.close().syncUninterruptibly();
    group.shutdownGracefully();
}
Also used : LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) 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) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) TimeoutException(java.util.concurrent.TimeoutException) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalEventLoopGroup(io.netty.channel.local.LocalEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Aggregations

EventLoopGroup (io.netty.channel.EventLoopGroup)134 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)92 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)64 Channel (io.netty.channel.Channel)64 Bootstrap (io.netty.bootstrap.Bootstrap)61 Test (org.junit.Test)36 ChannelFuture (io.netty.channel.ChannelFuture)35 SslContext (io.netty.handler.ssl.SslContext)35 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)31 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)28 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)27 LoggingHandler (io.netty.handler.logging.LoggingHandler)26 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)25 SocketChannel (io.netty.channel.socket.SocketChannel)22 ChannelPipeline (io.netty.channel.ChannelPipeline)20 LocalAddress (io.netty.channel.local.LocalAddress)18 LocalChannel (io.netty.channel.local.LocalChannel)18 LocalServerChannel (io.netty.channel.local.LocalServerChannel)18 InetSocketAddress (java.net.InetSocketAddress)18 ByteBuf (io.netty.buffer.ByteBuf)17