Search in sources :

Example 56 with Timeout

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

the class ParameterizedSslHandlerTest method reentryOnHandshakeCompleteNioChannel.

@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("data")
@Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
public void reentryOnHandshakeCompleteNioChannel(SslProvider clientProvider, SslProvider serverProvider) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Class<? extends ServerChannel> serverClass = NioServerSocketChannel.class;
        Class<? extends Channel> clientClass = NioSocketChannel.class;
        SocketAddress bindAddress = new InetSocketAddress(0);
        reentryOnHandshakeComplete(clientProvider, serverProvider, group, bindAddress, serverClass, clientClass, false, false);
        reentryOnHandshakeComplete(clientProvider, serverProvider, group, bindAddress, serverClass, clientClass, false, true);
        reentryOnHandshakeComplete(clientProvider, serverProvider, group, bindAddress, serverClass, clientClass, true, false);
        reentryOnHandshakeComplete(clientProvider, serverProvider, group, bindAddress, serverClass, clientClass, true, true);
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Timeout(org.junit.jupiter.api.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 57 with Timeout

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

the class ParameterizedSslHandlerTest method reentryOnHandshakeCompleteLocalChannel.

@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("data")
@Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
public void reentryOnHandshakeCompleteLocalChannel(SslProvider clientProvider, SslProvider serverProvider) throws Exception {
    EventLoopGroup group = new DefaultEventLoopGroup();
    try {
        Class<? extends ServerChannel> serverClass = LocalServerChannel.class;
        Class<? extends Channel> clientClass = LocalChannel.class;
        SocketAddress bindAddress = new LocalAddress(String.valueOf(current().nextLong()));
        reentryOnHandshakeComplete(clientProvider, serverProvider, group, bindAddress, serverClass, clientClass, false, false);
        reentryOnHandshakeComplete(clientProvider, serverProvider, group, bindAddress, serverClass, clientClass, false, true);
        reentryOnHandshakeComplete(clientProvider, serverProvider, group, bindAddress, serverClass, clientClass, true, false);
        reentryOnHandshakeComplete(clientProvider, serverProvider, group, bindAddress, serverClass, clientClass, true, true);
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalAddress(io.netty.channel.local.LocalAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Timeout(org.junit.jupiter.api.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 58 with Timeout

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

the class LocalTransportThreadModelTest method testStagedExecution.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
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.
        assertFalse(h1.inboundThreadNames.contains(currentName));
        assertFalse(h2.inboundThreadNames.contains(currentName));
        assertFalse(h3.inboundThreadNames.contains(currentName));
        assertFalse(h1.outboundThreadNames.contains(currentName));
        assertFalse(h2.outboundThreadNames.contains(currentName));
        assertFalse(h3.outboundThreadNames.contains(currentName));
        assertFalse(h1.removalThreadNames.contains(currentName));
        assertFalse(h2.removalThreadNames.contains(currentName));
        assertFalse(h3.removalThreadNames.contains(currentName));
        // Assert that events were handled by the correct executor.
        for (String name : h1.inboundThreadNames) {
            assertTrue(name.startsWith("l-"));
        }
        for (String name : h2.inboundThreadNames) {
            assertTrue(name.startsWith("e1-"));
        }
        for (String name : h3.inboundThreadNames) {
            assertTrue(name.startsWith("e2-"));
        }
        for (String name : h1.outboundThreadNames) {
            assertTrue(name.startsWith("l-"));
        }
        for (String name : h2.outboundThreadNames) {
            assertTrue(name.startsWith("e1-"));
        }
        for (String name : h3.outboundThreadNames) {
            assertTrue(name.startsWith("e2-"));
        }
        for (String name : h1.removalThreadNames) {
            assertTrue(name.startsWith("l-"));
        }
        for (String name : h2.removalThreadNames) {
            assertTrue(name.startsWith("e1-"));
        }
        for (String name : h3.removalThreadNames) {
            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);
        assertEquals(1, names.size());
        names.clear();
        names.addAll(h2.inboundThreadNames);
        names.addAll(h2.outboundThreadNames);
        names.addAll(h2.removalThreadNames);
        assertEquals(1, names.size());
        names.clear();
        names.addAll(h3.inboundThreadNames);
        names.addAll(h3.outboundThreadNames);
        names.addAll(h3.removalThreadNames);
        assertEquals(1, names.size());
        // Count the number of events
        assertEquals(1, h1.inboundThreadNames.size());
        assertEquals(2, h2.inboundThreadNames.size());
        assertEquals(3, h3.inboundThreadNames.size());
        assertEquals(3, h1.outboundThreadNames.size());
        assertEquals(2, h2.outboundThreadNames.size());
        assertEquals(1, h3.outboundThreadNames.size());
        assertEquals(1, h1.removalThreadNames.size());
        assertEquals(1, h2.removalThreadNames.size());
        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.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 59 with Timeout

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

the class DefaultChannelPipelineTest method testAddRemoveHandlerCalledOnceRegistered.

@Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testAddRemoveHandlerCalledOnceRegistered() throws Throwable {
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    CallbackCheckHandler handler = new CallbackCheckHandler();
    pipeline.addFirst(handler);
    pipeline.remove(handler);
    assertNull(handler.addedHandler.getNow());
    assertNull(handler.removedHandler.getNow());
    group.register(pipeline.channel()).syncUninterruptibly();
    Throwable cause = handler.error.get();
    if (cause != null) {
        throw cause;
    }
    assertTrue(handler.addedHandler.get());
    assertTrue(handler.removedHandler.get());
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 60 with Timeout

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

the class DefaultChannelPipelineTest method testAddHandlerBeforeRegisteredThenReplace.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testAddHandlerBeforeRegisteredThenReplace() throws Exception {
    final EventLoop loop = group.next();
    final CountDownLatch latch = new CountDownLatch(1);
    CheckEventExecutorHandler handler = new CheckEventExecutorHandler(loop);
    ChannelPipeline pipeline = new LocalChannel().pipeline();
    pipeline.addFirst(handler);
    assertFalse(handler.addedPromise.isDone());
    group.register(pipeline.channel());
    handler.addedPromise.syncUninterruptibly();
    pipeline.replace(handler, null, new ChannelHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) {
            latch.countDown();
        }
    });
    handler.removedPromise.syncUninterruptibly();
    latch.await();
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) CountDownLatch(java.util.concurrent.CountDownLatch) 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