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;
}
}
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());
}
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();
}
}
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();
}
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));
}
Aggregations