Search in sources :

Example 61 with Timeout

use of org.junit.jupiter.api.Timeout in project netty by netty.

the class DefaultChannelPipelineTest method testAddBefore.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testAddBefore() throws Throwable {
    ChannelPipeline pipeline1 = new LocalChannel().pipeline();
    ChannelPipeline pipeline2 = new LocalChannel().pipeline();
    EventLoopGroup defaultGroup = new DefaultEventLoopGroup(2);
    try {
        EventLoop eventLoop1 = defaultGroup.next();
        EventLoop eventLoop2 = defaultGroup.next();
        eventLoop1.register(pipeline1.channel()).syncUninterruptibly();
        eventLoop2.register(pipeline2.channel()).syncUninterruptibly();
        CountDownLatch latch = new CountDownLatch(2 * 10);
        for (int i = 0; i < 10; i++) {
            eventLoop1.execute(new TestTask(pipeline2, latch));
            eventLoop2.execute(new TestTask(pipeline1, latch));
        }
        latch.await();
    } finally {
        defaultGroup.shutdownGracefully();
    }
}
Also used : NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) LocalChannel(io.netty.channel.local.LocalChannel) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 62 with Timeout

use of org.junit.jupiter.api.Timeout in project netty by netty.

the class DefaultChannelPipelineTest method testHandlerAddedThrowsAndRemovedThrowsException.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
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) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 63 with Timeout

use of org.junit.jupiter.api.Timeout in project netty by netty.

the class DefaultChannelPipelineTest method testHandlerAddedAndRemovedCalledInCorrectOrder.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
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.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 64 with Timeout

use of org.junit.jupiter.api.Timeout in project netty by netty.

the class DefaultChannelPipelineTest method testHandlerAddedExceptionFromChildHandlerIsPropagated.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHandlerAddedExceptionFromChildHandlerIsPropagated() {
    final EventExecutorGroup group1 = new DefaultEventExecutorGroup(1);
    try {
        final Promise<Void> promise = group1.next().newPromise();
        final AtomicBoolean handlerAdded = new AtomicBoolean();
        final Exception exception = new RuntimeException();
        ChannelPipeline pipeline = new LocalChannel().pipeline();
        pipeline.addLast(group1, new CheckExceptionHandler(exception, promise));
        pipeline.addFirst(new ChannelHandlerAdapter() {

            @Override
            public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
                handlerAdded.set(true);
                throw exception;
            }
        });
        assertFalse(handlerAdded.get());
        group.register(pipeline.channel());
        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) NoSuchElementException(java.util.NoSuchElementException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 65 with Timeout

use of org.junit.jupiter.api.Timeout in project netty by netty.

the class NioSocketChannelTest method testShutdownOutputAndClose.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testShutdownOutputAndClose() throws IOException {
    NioEventLoopGroup group = new NioEventLoopGroup(1);
    ServerSocket socket = new ServerSocket();
    socket.bind(new InetSocketAddress(0));
    Socket accepted = null;
    try {
        Bootstrap sb = new Bootstrap();
        sb.group(group).channel(NioSocketChannel.class);
        sb.handler(new ChannelInboundHandlerAdapter());
        SocketChannel channel = (SocketChannel) sb.connect(socket.getLocalSocketAddress()).syncUninterruptibly().channel();
        accepted = socket.accept();
        channel.shutdownOutput().syncUninterruptibly();
        channel.close().syncUninterruptibly();
    } finally {
        if (accepted != null) {
            try {
                accepted.close();
            } catch (IOException ignore) {
            // ignore
            }
        }
        try {
            socket.close();
        } catch (IOException ignore) {
        // ignore
        }
        group.shutdownGracefully();
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

Timeout (org.junit.jupiter.api.Timeout)291 Test (org.junit.jupiter.api.Test)235 CountDownLatch (java.util.concurrent.CountDownLatch)71 ZooKeeper (org.apache.zookeeper.ZooKeeper)33 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 ArrayList (java.util.ArrayList)31 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29 RepeatedTest (org.junit.jupiter.api.RepeatedTest)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 LocalChannel (io.netty.channel.local.LocalChannel)27 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)26 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)25 IOException (java.io.IOException)25 Bootstrap (io.netty.bootstrap.Bootstrap)24 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 Channel (io.netty.channel.Channel)23 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)21 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)19 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)19