Search in sources :

Example 36 with Timeout

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

the class SslHandlerTest method testHandshakeFailedByWriteBeforeChannelActive.

@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testHandshakeFailedByWriteBeforeChannelActive() throws Exception {
    final SslContext sslClientCtx = SslContextBuilder.forClient().protocols(SslProtocols.SSL_v3).trustManager(InsecureTrustManagerFactory.INSTANCE).sslProvider(SslProvider.JDK).build();
    EventLoopGroup group = new NioEventLoopGroup();
    Channel sc = null;
    Channel cc = null;
    final CountDownLatch activeLatch = new CountDownLatch(1);
    final AtomicReference<AssertionError> errorRef = new AtomicReference<AssertionError>();
    final SslHandler sslHandler = sslClientCtx.newHandler(UnpooledByteBufAllocator.DEFAULT);
    try {
        sc = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInboundHandlerAdapter()).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        cc = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {

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

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                        if (cause instanceof AssertionError) {
                            errorRef.set((AssertionError) cause);
                        }
                    }

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        activeLatch.countDown();
                    }
                });
            }
        }).connect(sc.localAddress()).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                // Write something to trigger the handshake before fireChannelActive is called.
                future.channel().writeAndFlush(wrappedBuffer(new byte[] { 1, 2, 3, 4 }));
            }
        }).syncUninterruptibly().channel();
        // Ensure there is no AssertionError thrown by having the handshake failed by the writeAndFlush(...) before
        // channelActive(...) was called. Let's first wait for the activeLatch countdown to happen and after this
        // check if we saw and AssertionError (even if we timed out waiting).
        activeLatch.await(5, TimeUnit.SECONDS);
        AssertionError error = errorRef.get();
        if (error != null) {
            throw error;
        }
        assertThat(sslHandler.handshakeFuture().await().cause(), CoreMatchers.<Throwable>instanceOf(SSLException.class));
    } finally {
        if (cc != null) {
            cc.close().syncUninterruptibly();
        }
        if (sc != null) {
            sc.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
        ReferenceCountUtil.release(sslClientCtx);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelFutureListener(io.netty.channel.ChannelFutureListener) SSLException(javax.net.ssl.SSLException) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) CodecException(io.netty.handler.codec.CodecException) DecoderException(io.netty.handler.codec.DecoderException) SSLProtocolException(javax.net.ssl.SSLProtocolException) SSLException(javax.net.ssl.SSLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClosedChannelException(java.nio.channels.ClosedChannelException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 37 with Timeout

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

the class SniClientTest method testSniSNIMatcherDoesNotMatchClient.

@ParameterizedTest(name = PARAMETERIZED_NAME)
@Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
@MethodSource("parameters")
public void testSniSNIMatcherDoesNotMatchClient(final SslProvider serverProvider, final SslProvider clientProvider) {
    assumeTrue(PlatformDependent.javaVersion() >= 8);
    assertThrows(SSLException.class, new Executable() {

        @Override
        public void execute() throws Throwable {
            SniClientJava8TestUtil.testSniClient(serverProvider, clientProvider, false);
        }
    });
}
Also used : Executable(org.junit.jupiter.api.function.Executable) Timeout(org.junit.jupiter.api.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 38 with Timeout

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

the class Http2FrameCodecTest method newOutboundStream.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void newOutboundStream() {
    final Http2FrameStream stream = frameCodec.newStream();
    assertNotNull(stream);
    assertFalse(isStreamIdValid(stream.id()));
    final Promise<Void> listenerExecuted = new DefaultPromise<Void>(GlobalEventExecutor.INSTANCE);
    channel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers(), false).stream(stream)).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            assertTrue(future.isSuccess());
            assertTrue(isStreamIdValid(stream.id()));
            listenerExecuted.setSuccess(null);
        }
    });
    ByteBuf data = Unpooled.buffer().writeZero(100);
    ChannelFuture f = channel.writeAndFlush(new DefaultHttp2DataFrame(data).stream(stream));
    assertTrue(f.isSuccess());
    listenerExecuted.syncUninterruptibly();
    assertTrue(listenerExecuted.isSuccess());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultPromise(io.netty.util.concurrent.DefaultPromise) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 39 with Timeout

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

the class NettyBlockHoundIntegrationTest method testHashedWheelTimerStartStop.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testHashedWheelTimerStartStop() throws Exception {
    HashedWheelTimer timer = new HashedWheelTimer();
    Future<?> futureStart = GlobalEventExecutor.INSTANCE.submit(timer::start);
    futureStart.get(5, TimeUnit.SECONDS);
    Future<?> futureStop = GlobalEventExecutor.INSTANCE.submit(timer::stop);
    futureStop.get(5, TimeUnit.SECONDS);
}
Also used : HashedWheelTimer(io.netty.util.HashedWheelTimer) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 40 with Timeout

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

the class NettyBlockHoundIntegrationTest method testSingleThreadEventExecutorAddTask.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testSingleThreadEventExecutorAddTask() throws Exception {
    TestLinkedBlockingQueue<Runnable> taskQueue = new TestLinkedBlockingQueue<>();
    SingleThreadEventExecutor executor = new SingleThreadEventExecutor(null, new DefaultThreadFactory("test"), true) {

        @Override
        protected Queue<Runnable> newTaskQueue(int maxPendingTasks) {
            return taskQueue;
        }

        @Override
        protected void run() {
            while (!confirmShutdown()) {
                Runnable task = takeTask();
                if (task != null) {
                    task.run();
                }
            }
        }
    };
    taskQueue.emulateContention();
    CountDownLatch latch = new CountDownLatch(1);
    executor.submit(() -> {
        // calls addTask
        executor.execute(() -> {
        });
        latch.countDown();
    });
    taskQueue.waitUntilContented();
    taskQueue.removeContention();
    latch.await();
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) SingleThreadEventExecutor(io.netty.util.concurrent.SingleThreadEventExecutor) 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