Search in sources :

Example 46 with Timeout

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

the class EmbeddedChannelTest method testHandlerAddedExecutedInEventLoop.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testHandlerAddedExecutedInEventLoop() throws Throwable {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final ChannelHandler handler = new ChannelHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            try {
                assertTrue(ctx.executor().inEventLoop());
            } catch (Throwable cause) {
                error.set(cause);
            } finally {
                latch.countDown();
            }
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    assertFalse(channel.finish());
    latch.await();
    Throwable cause = error.get();
    if (cause != null) {
        throw cause;
    }
}
Also used : ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 47 with Timeout

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

the class EmbeddedChannelTest method testChannelInactiveFired.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testChannelInactiveFired() throws InterruptedException {
    final AtomicBoolean inactive = new AtomicBoolean();
    EmbeddedChannel channel = new EmbeddedChannel(new ChannelInboundHandlerAdapter() {

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            ctx.close();
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            inactive.set(true);
        }
    });
    channel.pipeline().fireExceptionCaught(new IllegalStateException());
    assertTrue(inactive.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 48 with Timeout

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

the class BootstrapTest method testLateRegistrationConnect.

@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testLateRegistrationConnect() throws Exception {
    EventLoopGroup group = new DelayedEventLoopGroup();
    try {
        final Bootstrap bootstrapA = new Bootstrap();
        bootstrapA.group(group);
        bootstrapA.channel(LocalChannel.class);
        bootstrapA.handler(dummyHandler);
        assertThrows(ConnectException.class, new Executable() {

            @Override
            public void execute() {
                bootstrapA.connect(LocalAddress.ANY).syncUninterruptibly();
            }
        });
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 49 with Timeout

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

the class CombinedChannelDuplexHandlerTest method testPromisesPassed.

@Test
@Timeout(value = 3000, unit = TimeUnit.MILLISECONDS)
public void testPromisesPassed() {
    OutboundEventHandler outboundHandler = new OutboundEventHandler();
    EmbeddedChannel ch = new EmbeddedChannel(outboundHandler, new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(new ChannelInboundHandlerAdapter(), new ChannelOutboundHandlerAdapter()));
    ChannelPipeline pipeline = ch.pipeline();
    ChannelPromise promise = ch.newPromise();
    pipeline.bind(LOCAL_ADDRESS, promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.connect(REMOTE_ADDRESS, LOCAL_ADDRESS, promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.close(promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.disconnect(promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.write(MSG, promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.deregister(promise);
    promise.syncUninterruptibly();
    ch.finish();
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 50 with Timeout

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

the class ChannelInitializerTest method testChannelInitializerEventExecutor.

@SuppressWarnings("deprecation")
@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testChannelInitializerEventExecutor() throws Throwable {
    final AtomicInteger invokeCount = new AtomicInteger();
    final AtomicInteger completeCount = new AtomicInteger();
    final AtomicReference<Throwable> errorRef = new AtomicReference<Throwable>();
    LocalAddress addr = new LocalAddress("test");
    final EventExecutor executor = new DefaultEventLoop() {

        private final ScheduledExecutorService execService = Executors.newSingleThreadScheduledExecutor();

        @Override
        public void shutdown() {
            execService.shutdown();
        }

        @Override
        public boolean inEventLoop(Thread thread) {
            // Always return false which will ensure we always call execute(...)
            return false;
        }

        @Override
        public boolean isShuttingDown() {
            return false;
        }

        @Override
        public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
            throw new IllegalStateException();
        }

        @Override
        public Future<?> terminationFuture() {
            throw new IllegalStateException();
        }

        @Override
        public boolean isShutdown() {
            return execService.isShutdown();
        }

        @Override
        public boolean isTerminated() {
            return execService.isTerminated();
        }

        @Override
        public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
            return execService.awaitTermination(timeout, unit);
        }

        @Override
        public void execute(Runnable command) {
            execService.execute(command);
        }
    };
    final CountDownLatch latch = new CountDownLatch(1);
    ServerBootstrap serverBootstrap = new ServerBootstrap().channel(LocalServerChannel.class).group(group).localAddress(addr).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        protected void initChannel(LocalChannel ch) {
            ch.pipeline().addLast(executor, new ChannelInitializer<Channel>() {

                @Override
                protected void initChannel(Channel ch) {
                    invokeCount.incrementAndGet();
                    ChannelHandlerContext ctx = ch.pipeline().context(this);
                    assertNotNull(ctx);
                    ch.pipeline().addAfter(ctx.executor(), ctx.name(), null, new ChannelInboundHandlerAdapter() {

                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) {
                        // just drop on the floor.
                        }

                        @Override
                        public void handlerRemoved(ChannelHandlerContext ctx) {
                            latch.countDown();
                        }
                    });
                    completeCount.incrementAndGet();
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                    if (cause instanceof AssertionError) {
                        errorRef.set(cause);
                    }
                }
            });
        }
    });
    Channel server = serverBootstrap.bind().sync().channel();
    Bootstrap clientBootstrap = new Bootstrap().channel(LocalChannel.class).group(group).remoteAddress(addr).handler(new ChannelInboundHandlerAdapter());
    Channel client = clientBootstrap.connect().sync().channel();
    client.writeAndFlush("Hello World").sync();
    client.close().sync();
    server.close().sync();
    client.closeFuture().sync();
    server.closeFuture().sync();
    // Wait until the handler is removed from the pipeline and so no more events are handled by it.
    latch.await();
    assertEquals(1, invokeCount.get());
    assertEquals(invokeCount.get(), completeCount.get());
    Throwable cause = errorRef.get();
    if (cause != null) {
        throw cause;
    }
    executor.shutdown();
    assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS));
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LocalAddress(io.netty.channel.local.LocalAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventExecutor(io.netty.util.concurrent.EventExecutor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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