Search in sources :

Example 51 with Timeout

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

the class EmbeddedChannelTest method promiseDoesNotInfiniteLoop.

@Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void promiseDoesNotInfiniteLoop() throws InterruptedException {
    EmbeddedChannel channel = new EmbeddedChannel();
    channel.closeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            future.channel().close();
        }
    });
    channel.close().syncUninterruptibly();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 52 with Timeout

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

the class EpollReuseAddrTest method testMultipleBindSocketChannel.

@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testMultipleBindSocketChannel() throws Exception {
    assumeTrue(versionEqOrGt(3, 9, 0));
    ServerBootstrap bootstrap = createServerBootstrap();
    bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
    final AtomicBoolean accepted1 = new AtomicBoolean();
    bootstrap.childHandler(new ServerSocketTestHandler(accepted1));
    ChannelFuture future = bootstrap.bind().syncUninterruptibly();
    InetSocketAddress address1 = (InetSocketAddress) future.channel().localAddress();
    final AtomicBoolean accepted2 = new AtomicBoolean();
    bootstrap.childHandler(new ServerSocketTestHandler(accepted2));
    ChannelFuture future2 = bootstrap.bind(address1).syncUninterruptibly();
    InetSocketAddress address2 = (InetSocketAddress) future2.channel().localAddress();
    assertEquals(address1, address2);
    while (!accepted1.get() || !accepted2.get()) {
        Socket socket = new Socket(address1.getAddress(), address1.getPort());
        socket.setReuseAddress(true);
        socket.close();
    }
    future.channel().close().syncUninterruptibly();
    future2.channel().close().syncUninterruptibly();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 53 with Timeout

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

the class EpollReuseAddrTest method testMultipleBindDatagramChannel.

@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
// TODO: Unignore after making it pass on centos6-1 and debian7-1
@Disabled
public void testMultipleBindDatagramChannel() throws Exception {
    ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
    assumeTrue(versionEqOrGt(3, 9, 0));
    Bootstrap bootstrap = createBootstrap();
    bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
    final AtomicBoolean received1 = new AtomicBoolean();
    bootstrap.handler(new DatagramSocketTestHandler(received1));
    ChannelFuture future = bootstrap.bind().syncUninterruptibly();
    final InetSocketAddress address1 = (InetSocketAddress) future.channel().localAddress();
    final AtomicBoolean received2 = new AtomicBoolean();
    bootstrap.handler(new DatagramSocketTestHandler(received2));
    ChannelFuture future2 = bootstrap.bind(address1).syncUninterruptibly();
    final InetSocketAddress address2 = (InetSocketAddress) future2.channel().localAddress();
    assertEquals(address1, address2);
    final byte[] bytes = "data".getBytes();
    // fire up 16 Threads and send DatagramPackets to make sure we stress it enough to see DatagramPackets received
    // on both sockets.
    int count = 16;
    final CountDownLatch latch = new CountDownLatch(count);
    Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                DatagramSocket socket = new DatagramSocket();
                while (!received1.get() || !received2.get()) {
                    socket.send(new DatagramPacket(bytes, 0, bytes.length, address1.getAddress(), address1.getPort()));
                }
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            latch.countDown();
        }
    };
    ExecutorService executor = Executors.newFixedThreadPool(count);
    for (int i = 0; i < count; i++) {
        executor.execute(r);
    }
    latch.await();
    executor.shutdown();
    future.channel().close().syncUninterruptibly();
    future2.channel().close().syncUninterruptibly();
    assertTrue(received1.get());
    assertTrue(received2.get());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) ExecutorService(java.util.concurrent.ExecutorService) AbstractBootstrap(io.netty.bootstrap.AbstractBootstrap) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout) Disabled(org.junit.jupiter.api.Disabled)

Example 54 with Timeout

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

the class EpollSpliceTest method spliceToFile.

@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void spliceToFile() throws Throwable {
    EventLoopGroup group = new EpollEventLoopGroup(1);
    File file = PlatformDependent.createTempFile("netty-splice", null, null);
    file.deleteOnExit();
    SpliceHandler sh = new SpliceHandler(file);
    ServerBootstrap bs = new ServerBootstrap();
    bs.channel(EpollServerSocketChannel.class);
    bs.group(group).childHandler(sh);
    bs.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    Channel sc = bs.bind(NetUtil.LOCALHOST, 0).syncUninterruptibly().channel();
    Bootstrap cb = new Bootstrap();
    cb.group(group);
    cb.channel(EpollSocketChannel.class);
    cb.handler(new ChannelInboundHandlerAdapter());
    Channel cc = cb.connect(sc.localAddress()).syncUninterruptibly().channel();
    for (int i = 0; i < data.length; ) {
        int length = Math.min(random.nextInt(1024 * 64), data.length - i);
        ByteBuf buf = Unpooled.wrappedBuffer(data, i, length);
        cc.writeAndFlush(buf);
        i += length;
    }
    while (sh.future2 == null || !sh.future2.isDone() || !sh.future.isDone()) {
        if (sh.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    sc.close().sync();
    cc.close().sync();
    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
        throw sh.exception.get();
    }
    byte[] written = new byte[data.length];
    FileInputStream in = new FileInputStream(file);
    try {
        assertEquals(written.length, in.read(written));
        assertArrayEquals(data, written);
    } finally {
        in.close();
        group.shutdownGracefully();
    }
}
Also used : Channel(io.netty.channel.Channel) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) FileInputStream(java.io.FileInputStream) EventLoopGroup(io.netty.channel.EventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) File(java.io.File) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 55 with Timeout

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

the class CloseNotifyTest method eventsOrder.

@ParameterizedTest(name = "{index}: provider={0}, protocol={1}")
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
@MethodSource("data")
public void eventsOrder(SslProvider provider, String protocol) throws Exception {
    assumeTrue(provider != SslProvider.OPENSSL || OpenSsl.isAvailable(), "OpenSSL is not available");
    if (SslProtocols.TLS_v1_3.equals(protocol)) {
        // Ensure we support TLSv1.3
        assumeTrue(SslProvider.isTlsv13Supported(provider));
    }
    BlockingQueue<Object> clientEventQueue = new LinkedBlockingQueue<Object>();
    BlockingQueue<Object> serverEventQueue = new LinkedBlockingQueue<Object>();
    EmbeddedChannel clientChannel = initChannel(provider, protocol, true, clientEventQueue);
    EmbeddedChannel serverChannel = initChannel(provider, protocol, false, serverEventQueue);
    try {
        // handshake:
        forwardData(clientChannel, serverChannel);
        forwardData(serverChannel, clientChannel);
        forwardData(clientChannel, serverChannel);
        forwardData(serverChannel, clientChannel);
        assertThat(clientEventQueue.poll(), instanceOf(SslHandshakeCompletionEvent.class));
        assertThat(serverEventQueue.poll(), instanceOf(SslHandshakeCompletionEvent.class));
        assertThat(handshakenProtocol(clientChannel), equalTo(protocol));
        // send data:
        clientChannel.writeOutbound(writeAscii(ALLOC, "request_msg"));
        forwardData(clientChannel, serverChannel);
        assertThat(serverEventQueue.poll(), equalTo((Object) "request_msg"));
        // respond with data and close_notify:
        serverChannel.writeOutbound(writeAscii(ALLOC, "response_msg"));
        assertThat(serverChannel.finish(), is(true));
        assertThat(serverEventQueue.poll(), instanceOf(SslCloseCompletionEvent.class));
        assertThat(clientEventQueue, empty());
        // consume server response with close_notify:
        forwardAllWithCloseNotify(serverChannel, clientChannel);
        assertThat(clientEventQueue.poll(), equalTo((Object) "response_msg"));
        assertThat(clientEventQueue.poll(), instanceOf(SslCloseCompletionEvent.class));
        // make sure client automatically responds with close_notify:
        if (!jdkTls13(provider, protocol)) {
            // JDK impl of TLSv1.3 does not automatically generate "close_notify" in response to the received
            // "close_notify" alert. This is a legit behavior according to the spec:
            // https://tools.ietf.org/html/rfc8446#section-6.1. Handle it differently:
            assertCloseNotify((ByteBuf) clientChannel.readOutbound());
        }
    } finally {
        try {
            clientChannel.finish();
        } finally {
            serverChannel.finish();
        }
    }
    if (jdkTls13(provider, protocol)) {
        assertCloseNotify((ByteBuf) clientChannel.readOutbound());
    } else {
        discardEmptyOutboundBuffers(clientChannel);
    }
    assertThat(clientEventQueue.poll(), is(INACTIVE));
    assertThat(clientEventQueue, empty());
    assertThat(serverEventQueue.poll(), is(INACTIVE));
    assertThat(serverEventQueue, empty());
    assertThat(clientChannel.releaseInbound(), is(false));
    assertThat(clientChannel.releaseOutbound(), is(false));
    assertThat(serverChannel.releaseInbound(), is(false));
    assertThat(serverChannel.releaseOutbound(), is(false));
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Timeout(org.junit.jupiter.api.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

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