Search in sources :

Example 1 with SingleThreadEventLoop

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

the class LocalChannelTest method localChannelRaceCondition.

@Test
public void localChannelRaceCondition() throws Exception {
    final CountDownLatch closeLatch = new CountDownLatch(1);
    final EventLoopGroup clientGroup = new DefaultEventLoopGroup(1) {

        @Override
        protected EventLoop newChild(Executor threadFactory, Object... args) throws Exception {
            return new SingleThreadEventLoop(this, threadFactory, true) {

                @Override
                protected void run() {
                    for (; ; ) {
                        Runnable task = takeTask();
                        if (task != null) {
                            /* Only slow down the anonymous class in LocalChannel#doRegister() */
                            if (task.getClass().getEnclosingClass() == LocalChannel.class) {
                                try {
                                    closeLatch.await();
                                } catch (InterruptedException e) {
                                    throw new Error(e);
                                }
                            }
                            task.run();
                            updateLastExecutionTime();
                        }
                        if (confirmShutdown()) {
                            break;
                        }
                    }
                }
            };
        }
    };
    Channel sc = null;
    Channel cc = null;
    try {
        ServerBootstrap sb = new ServerBootstrap();
        sc = sb.group(group2).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.close();
                closeLatch.countDown();
            }
        }).bind(TEST_ADDRESS).sync().channel();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(clientGroup).channel(LocalChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
            /* Do nothing */
            }
        });
        ChannelFuture future = bootstrap.connect(sc.localAddress());
        assertTrue("Connection should finish, not time out", future.await(200));
        cc = future.channel();
    } finally {
        closeChannel(cc);
        closeChannel(sc);
        clientGroup.shutdownGracefully(0, 0, SECONDS).await();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AbstractChannel(io.netty.channel.AbstractChannel) Channel(io.netty.channel.Channel) SingleThreadEventLoop(io.netty.channel.SingleThreadEventLoop) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Executor(java.util.concurrent.Executor) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Test(org.junit.Test)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 AbstractChannel (io.netty.channel.AbstractChannel)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 SingleThreadEventLoop (io.netty.channel.SingleThreadEventLoop)1 ConnectException (java.net.ConnectException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executor (java.util.concurrent.Executor)1 Test (org.junit.Test)1