Search in sources :

Example 26 with Timeout

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

the class NonStickyEventExecutorGroupTest method testOrdering.

@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("data")
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testOrdering(int maxTaskExecutePerRun) throws Throwable {
    final int threads = NettyRuntime.availableProcessors() * 2;
    final EventExecutorGroup group = new UnorderedThreadPoolEventExecutor(threads);
    final NonStickyEventExecutorGroup nonStickyGroup = new NonStickyEventExecutorGroup(group, maxTaskExecutePerRun);
    try {
        final CountDownLatch startLatch = new CountDownLatch(1);
        final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
        List<Thread> threadList = new ArrayList<Thread>(threads);
        for (int i = 0; i < threads; i++) {
            Thread thread = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        execute(nonStickyGroup, startLatch);
                    } catch (Throwable cause) {
                        error.compareAndSet(null, cause);
                    }
                }
            });
            threadList.add(thread);
            thread.start();
        }
        startLatch.countDown();
        for (Thread t : threadList) {
            t.join();
        }
        Throwable cause = error.get();
        if (cause != null) {
            throw cause;
        }
    } finally {
        nonStickyGroup.shutdownGracefully();
    }
}
Also used : ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Timeout(org.junit.jupiter.api.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 27 with Timeout

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

the class AbstractReferenceCountedTest method testReleaseFromMultipleThreadsThrowsReferenceCountException.

@Test
@Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
public void testReleaseFromMultipleThreadsThrowsReferenceCountException() throws Exception {
    int threads = 4;
    Queue<Future<?>> futures = new ArrayDeque<Future<?>>(threads);
    ExecutorService service = Executors.newFixedThreadPool(threads);
    final AtomicInteger refCountExceptions = new AtomicInteger();
    try {
        for (int i = 0; i < 10000; i++) {
            final AbstractReferenceCounted referenceCounted = newReferenceCounted();
            final CountDownLatch releaseLatch = new CountDownLatch(1);
            final AtomicInteger releasedCount = new AtomicInteger();
            for (int a = 0; a < threads; a++) {
                final AtomicInteger releaseCnt = new AtomicInteger(0);
                futures.add(service.submit(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            releaseLatch.await();
                            try {
                                if (referenceCounted.release(releaseCnt.incrementAndGet())) {
                                    releasedCount.incrementAndGet();
                                }
                            } catch (IllegalReferenceCountException e) {
                                refCountExceptions.incrementAndGet();
                            }
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                }));
            }
            releaseLatch.countDown();
            for (; ; ) {
                Future<?> f = futures.poll();
                if (f == null) {
                    break;
                }
                f.get();
            }
            assertEquals(3, refCountExceptions.get());
            assertEquals(1, releasedCount.get());
            refCountExceptions.set(0);
        }
    } finally {
        service.shutdown();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch) ArrayDeque(java.util.ArrayDeque) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 28 with Timeout

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

the class UnorderedThreadPoolEventExecutorTest method scheduledAtFixedRateMustRunTaskRepeatedly.

@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void scheduledAtFixedRateMustRunTaskRepeatedly() throws InterruptedException {
    UnorderedThreadPoolEventExecutor executor = new UnorderedThreadPoolEventExecutor(1);
    final CountDownLatch latch = new CountDownLatch(3);
    Future<?> future = executor.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            latch.countDown();
        }
    }, 1, 1, TimeUnit.MILLISECONDS);
    try {
        latch.await();
    } finally {
        future.cancel(true);
        executor.shutdownGracefully();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 29 with Timeout

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

the class SslHandlerTest method testHandshakeFailureOnlyFireExceptionOnce.

@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testHandshakeFailureOnlyFireExceptionOnce() throws Exception {
    final SslContext sslClientCtx = SslContextBuilder.forClient().trustManager(new X509ExtendedTrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
            failVerification();
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
            failVerification();
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
            failVerification();
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
            failVerification();
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            failVerification();
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            failVerification();
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return EmptyArrays.EMPTY_X509_CERTIFICATES;
        }

        private void failVerification() throws CertificateException {
            throw new CertificateException();
        }
    }).sslProvider(SslProvider.JDK).build();
    final SelfSignedCertificate cert = new SelfSignedCertificate();
    final SslContext sslServerCtx = SslContextBuilder.forServer(cert.key(), cert.cert()).sslProvider(SslProvider.JDK).build();
    EventLoopGroup group = new NioEventLoopGroup();
    Channel sc = null;
    final SslHandler clientSslHandler = sslClientCtx.newHandler(UnpooledByteBufAllocator.DEFAULT);
    final SslHandler serverSslHandler = sslServerCtx.newHandler(UnpooledByteBufAllocator.DEFAULT);
    try {
        final Object terminalEvent = new Object();
        final BlockingQueue<Object> errorQueue = new LinkedBlockingQueue<Object>();
        sc = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(serverSslHandler);
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void exceptionCaught(final ChannelHandlerContext ctx, Throwable cause) {
                        errorQueue.add(cause);
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) {
                        errorQueue.add(terminalEvent);
                    }
                });
            }
        }).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        final ChannelFuture future = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(clientSslHandler);
            }
        }).connect(sc.localAddress());
        future.syncUninterruptibly();
        clientSslHandler.handshakeFuture().addListener(new FutureListener<Channel>() {

            @Override
            public void operationComplete(Future<Channel> f) {
                future.channel().close();
            }
        });
        assertFalse(clientSslHandler.handshakeFuture().await().isSuccess());
        assertFalse(serverSslHandler.handshakeFuture().await().isSuccess());
        Object error = errorQueue.take();
        assertThat(error, Matchers.instanceOf(DecoderException.class));
        assertThat(((Throwable) error).getCause(), Matchers.<Throwable>instanceOf(SSLException.class));
        Object terminal = errorQueue.take();
        assertSame(terminalEvent, terminal);
        assertNull(errorQueue.poll(1, TimeUnit.MILLISECONDS));
    } finally {
        if (sc != null) {
            sc.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
    }
}
Also used : X509ExtendedTrustManager(javax.net.ssl.X509ExtendedTrustManager) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) SSLEngine(javax.net.ssl.SSLEngine) InetSocketAddress(java.net.InetSocketAddress) CertificateException(java.security.cert.CertificateException) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) SSLException(javax.net.ssl.SSLException) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) X509Certificate(java.security.cert.X509Certificate) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) DecoderException(io.netty.handler.codec.DecoderException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Socket(java.net.Socket) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 30 with Timeout

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

the class SslHandlerTest method testRemoval.

@Test
@Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
public void testRemoval() throws Exception {
    NioEventLoopGroup group = new NioEventLoopGroup();
    Channel sc = null;
    Channel cc = null;
    try {
        final Promise<Void> clientPromise = group.next().newPromise();
        Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(newHandler(SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(), clientPromise));
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        final Promise<Void> serverPromise = group.next().newPromise();
        ServerBootstrap serverBootstrap = new ServerBootstrap().group(group, group).channel(NioServerSocketChannel.class).childHandler(newHandler(SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(), serverPromise));
        sc = serverBootstrap.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        cc = bootstrap.connect(sc.localAddress()).syncUninterruptibly().channel();
        serverPromise.syncUninterruptibly();
        clientPromise.syncUninterruptibly();
    } finally {
        if (cc != null) {
            cc.close().syncUninterruptibly();
        }
        if (sc != null) {
            sc.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) 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