Search in sources :

Example 71 with EventLoop

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

the class DefaultDnsCnameCacheTest method testClear.

@Test
public void testClear() throws Exception {
    EventLoopGroup group = new DefaultEventLoopGroup(1);
    try {
        EventLoop loop = group.next();
        final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
        cache.cache("x.netty.io", "mapping.netty.io", 100000, loop);
        cache.cache("y.netty.io", "mapping.netty.io", 100000, loop);
        assertEquals("mapping.netty.io", cache.get("x.netty.io"));
        assertEquals("mapping.netty.io", cache.get("y.netty.io"));
        assertTrue(cache.clear("x.netty.io"));
        assertNull(cache.get("x.netty.io"));
        assertEquals("mapping.netty.io", cache.get("y.netty.io"));
        cache.clear();
        assertNull(cache.get("y.netty.io"));
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) EventLoop(io.netty.channel.EventLoop) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test)

Example 72 with EventLoop

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

the class AbstractSingleThreadEventLoopTest method gracefulShutdownAfterStart.

@Test
public void gracefulShutdownAfterStart() throws Exception {
    EventLoop loop = newEventLoopGroup().next();
    final CountDownLatch latch = new CountDownLatch(1);
    loop.execute(new Runnable() {

        @Override
        public void run() {
            latch.countDown();
        }
    });
    // Wait for the event loop thread to start.
    latch.await();
    // Request the event loop thread to stop.
    loop.shutdownGracefully(200L, 3000L, TimeUnit.MILLISECONDS);
    // Wait until the event loop is terminated.
    assertTrue(loop.awaitTermination(500L, TimeUnit.MILLISECONDS));
    assertRejection(loop);
}
Also used : EventLoop(io.netty.channel.EventLoop) SingleThreadEventLoop(io.netty.channel.SingleThreadEventLoop) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 73 with EventLoop

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

the class NioEventLoop method processSelectedKey.

private void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
    final AbstractNioChannel.NioUnsafe unsafe = ch.unsafe();
    if (!k.isValid()) {
        final EventLoop eventLoop;
        try {
            eventLoop = ch.eventLoop();
        } catch (Throwable ignored) {
            // to close ch.
            return;
        }
        // See https://github.com/netty/netty/issues/5125
        if (eventLoop == this) {
            // close the channel if the key is not valid anymore
            unsafe.close(unsafe.voidPromise());
        }
        return;
    }
    try {
        int readyOps = k.readyOps();
        // the NIO JDK channel implementation may throw a NotYetConnectedException.
        if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
            // remove OP_CONNECT as otherwise Selector.select(..) will always return without blocking
            // See https://github.com/netty/netty/issues/924
            int ops = k.interestOps();
            ops &= ~SelectionKey.OP_CONNECT;
            k.interestOps(ops);
            unsafe.finishConnect();
        }
        // Process OP_WRITE first as we may be able to write some queued buffers and so free memory.
        if ((readyOps & SelectionKey.OP_WRITE) != 0) {
            // Call forceFlush which will also take care of clear the OP_WRITE once there is nothing left to write
            ch.unsafe().forceFlush();
        }
        // to a spin loop
        if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
            unsafe.read();
        }
    } catch (CancelledKeyException ignored) {
        unsafe.close(unsafe.voidPromise());
    }
}
Also used : EventLoop(io.netty.channel.EventLoop) SingleThreadEventLoop(io.netty.channel.SingleThreadEventLoop) CancelledKeyException(java.nio.channels.CancelledKeyException)

Example 74 with EventLoop

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

the class NioEventLoopTest method testScheduleBigDelayNotOverflow.

@Test
public void testScheduleBigDelayNotOverflow() {
    EventLoopGroup group = new NioEventLoopGroup(1);
    final EventLoop el = group.next();
    Future<?> future = el.schedule(new Runnable() {

        @Override
        public void run() {
        // NOOP
        }
    }, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    assertFalse(future.awaitUninterruptibly(1000));
    assertTrue(future.cancel(true));
    group.shutdownGracefully();
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) EventLoop(io.netty.channel.EventLoop) SingleThreadEventLoop(io.netty.channel.SingleThreadEventLoop) AbstractEventLoopTest(io.netty.channel.AbstractEventLoopTest) Test(org.junit.jupiter.api.Test)

Example 75 with EventLoop

use of io.netty.channel.EventLoop 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) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) NetworkChannel(java.nio.channels.NetworkChannel) 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) IOException(java.io.IOException) ChannelPipeline(io.netty.channel.ChannelPipeline) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoop(io.netty.channel.EventLoop) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Aggregations

EventLoop (io.netty.channel.EventLoop)77 EventLoopGroup (io.netty.channel.EventLoopGroup)27 Test (org.junit.jupiter.api.Test)27 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)18 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)17 Channel (io.netty.channel.Channel)10 InetSocketAddress (java.net.InetSocketAddress)10 Bootstrap (io.netty.bootstrap.Bootstrap)9 InetAddress (java.net.InetAddress)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ChannelFuture (io.netty.channel.ChannelFuture)6 ChannelPromise (io.netty.channel.ChannelPromise)6 LocalAddress (io.netty.channel.local.LocalAddress)5 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)4 ChannelPipeline (io.netty.channel.ChannelPipeline)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 SingleThreadEventLoop (io.netty.channel.SingleThreadEventLoop)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Callable (java.util.concurrent.Callable)3