Search in sources :

Example 41 with Timeout

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

the class SocketChannelNotYetConnectedTest method readMustBePendingUntilChannelIsActive.

@Test
@Timeout(30)
public void readMustBePendingUntilChannelIsActive(TestInfo info) throws Throwable {
    run(info, new Runner<Bootstrap>() {

        @Override
        public void run(Bootstrap bootstrap) throws Throwable {
            NioEventLoopGroup group = new NioEventLoopGroup(1);
            ServerBootstrap sb = new ServerBootstrap().group(group);
            Channel serverChannel = sb.childHandler(new ChannelInboundHandlerAdapter() {

                @Override
                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                    ctx.writeAndFlush(Unpooled.copyInt(42));
                }
            }).channel(NioServerSocketChannel.class).bind(0).sync().channel();
            final CountDownLatch readLatch = new CountDownLatch(1);
            bootstrap.handler(new ByteToMessageDecoder() {

                @Override
                public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
                    assertFalse(ctx.channel().isActive());
                    ctx.read();
                }

                @Override
                protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
                    assertThat(in.readableBytes()).isLessThanOrEqualTo(Integer.BYTES);
                    if (in.readableBytes() == Integer.BYTES) {
                        assertThat(in.readInt()).isEqualTo(42);
                        readLatch.countDown();
                    }
                }
            });
            bootstrap.connect(serverChannel.localAddress()).sync();
            readLatch.await();
            group.shutdownGracefully().await();
        }
    });
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ByteToMessageDecoder(io.netty.handler.codec.ByteToMessageDecoder) ByteBuf(io.netty.buffer.ByteBuf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) List(java.util.List) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 42 with Timeout

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

the class NettyBlockHoundIntegrationTest method testUnixResolverDnsServerAddressStreamProvider_ParseEtcResolverSearchDomainsAndOptions.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testUnixResolverDnsServerAddressStreamProvider_ParseEtcResolverSearchDomainsAndOptions() throws InterruptedException {
    NioEventLoopGroup group = new NioEventLoopGroup();
    try {
        DnsNameResolverBuilder builder = new DnsNameResolverBuilder(group.next()).channelFactory(NioDatagramChannel::new);
        doTestParseResolverFilesAllowsBlockingCalls(builder::build);
    } finally {
        group.shutdownGracefully();
    }
}
Also used : DnsNameResolverBuilder(io.netty.resolver.dns.DnsNameResolverBuilder) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 43 with Timeout

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

the class NioUdtMessageRendezvousChannelTest method basicEcho.

/**
 * verify basic echo message rendezvous
 *
 * FIXME: Re-enable after making it pass on Windows without unncessary tight loop.
 *        https://github.com/netty/netty/issues/2853
 */
@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
@Disabled
public void basicEcho() throws Exception {
    final int messageSize = 64 * 1024;
    final int transferLimit = messageSize * 16;
    final Meter rate1 = Metrics.newMeter(NioUdtMessageRendezvousChannelTest.class, "send rate", "bytes", TimeUnit.SECONDS);
    final Meter rate2 = Metrics.newMeter(NioUdtMessageRendezvousChannelTest.class, "send rate", "bytes", TimeUnit.SECONDS);
    final InetSocketAddress addr1 = UnitHelp.localSocketAddress();
    final InetSocketAddress addr2 = UnitHelp.localSocketAddress();
    final EchoMessageHandler handler1 = new EchoMessageHandler(rate1, messageSize);
    final EchoMessageHandler handler2 = new EchoMessageHandler(rate2, messageSize);
    final NioEventLoopGroup group1 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
    final NioEventLoopGroup group2 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
    final Bootstrap boot1 = new Bootstrap();
    boot1.group(group1).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr1).remoteAddress(addr2).handler(handler1);
    final Bootstrap boot2 = new Bootstrap();
    boot2.group(group2).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr2).remoteAddress(addr1).handler(handler2);
    final ChannelFuture connectFuture1 = boot1.connect();
    final ChannelFuture connectFuture2 = boot2.connect();
    while (handler1.meter().count() < transferLimit && handler2.meter().count() < transferLimit) {
        log.info("progress : {} {}", handler1.meter().count(), handler2.meter().count());
        Thread.sleep(1000);
    }
    connectFuture1.channel().close().sync();
    connectFuture2.channel().close().sync();
    log.info("handler1 : {}", handler1.meter().count());
    log.info("handler2 : {}", handler2.meter().count());
    assertTrue(handler1.meter().count() >= transferLimit);
    assertTrue(handler2.meter().count() >= transferLimit);
    assertEquals(handler1.meter().count(), handler2.meter().count());
    group1.shutdownGracefully();
    group2.shutdownGracefully();
    group1.terminationFuture().sync();
    group2.terminationFuture().sync();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Meter(com.yammer.metrics.core.Meter) InetSocketAddress(java.net.InetSocketAddress) EchoMessageHandler(io.netty.test.udt.util.EchoMessageHandler) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout) Disabled(org.junit.jupiter.api.Disabled)

Example 44 with Timeout

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

the class LocalChannelTest method testConnectFutureBeforeChannelActive.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testConnectFutureBeforeChannelActive() throws Exception {
    Bootstrap cb = new Bootstrap();
    ServerBootstrap sb = new ServerBootstrap();
    cb.group(group1).channel(LocalChannel.class).handler(new ChannelInboundHandlerAdapter());
    sb.group(group2).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new TestHandler());
        }
    });
    Channel sc = null;
    Channel cc = null;
    try {
        // Start server
        sc = sb.bind(TEST_ADDRESS).sync().channel();
        cc = cb.register().sync().channel();
        final ChannelPromise promise = cc.newPromise();
        final Promise<Void> assertPromise = cc.eventLoop().newPromise();
        cc.pipeline().addLast(new TestHandler() {

            @Override
            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                // Ensure the promise was done before the handler method is triggered.
                if (promise.isDone()) {
                    assertPromise.setSuccess(null);
                } else {
                    assertPromise.setFailure(new AssertionError("connect promise should be done"));
                }
            }
        });
        // Connect to the server
        cc.connect(sc.localAddress(), promise).sync();
        assertPromise.syncUninterruptibly();
        assertTrue(promise.isSuccess());
    } finally {
        closeChannel(cc);
        closeChannel(sc);
    }
}
Also used : AbstractChannel(io.netty.channel.AbstractChannel) Channel(io.netty.channel.Channel) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 45 with Timeout

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

the class DefaultChannelPipelineTest method testThrowInOtherHandlerAfterInvokedFromExceptionCaught.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testThrowInOtherHandlerAfterInvokedFromExceptionCaught() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger counter = new AtomicInteger();
    Channel channel = new LocalChannel();
    try {
        group.register(channel).syncUninterruptibly();
        channel.pipeline().addLast(new ChannelInboundHandlerAdapter() {

            @Override
            public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                ctx.fireChannelReadComplete();
            }
        }, new ChannelInboundHandlerAdapter() {

            class TestException extends Exception {
            }

            @Override
            public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
                throw new TestException();
            }

            @Override
            public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                if (cause instanceof TestException) {
                    ctx.executor().execute(new Runnable() {

                        @Override
                        public void run() {
                            latch.countDown();
                        }
                    });
                }
                counter.incrementAndGet();
                throw new Exception();
            }
        });
        channel.pipeline().fireExceptionCaught(new Exception());
        latch.await();
        assertEquals(1, counter.get());
    } finally {
        channel.close().syncUninterruptibly();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) OioSocketChannel(io.netty.channel.socket.oio.OioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) CountDownLatch(java.util.concurrent.CountDownLatch) NoSuchElementException(java.util.NoSuchElementException) 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