use of org.junit.jupiter.api.Timeout in project netty by netty.
the class NonStickyEventExecutorGroupTest method testOrdering.
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("data")
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testOrdering(int maxTaskExecutePerRun) throws Throwable {
final int threads = NettyRuntime.availableProcessors() * 2;
final EventExecutorGroup group = new UnorderedThreadPoolEventExecutor(threads);
final NonStickyEventExecutorGroup nonStickyGroup = new NonStickyEventExecutorGroup(group, maxTaskExecutePerRun);
try {
final CountDownLatch startLatch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
List<Thread> threadList = new ArrayList<Thread>(threads);
for (int i = 0; i < threads; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
execute(nonStickyGroup, startLatch);
} catch (Throwable cause) {
error.compareAndSet(null, cause);
}
}
});
threadList.add(thread);
thread.start();
}
startLatch.countDown();
for (Thread t : threadList) {
t.join();
}
Throwable cause = error.get();
if (cause != null) {
throw cause;
}
} finally {
nonStickyGroup.shutdownGracefully();
}
}
use of org.junit.jupiter.api.Timeout in project netty by netty.
the class AbstractReferenceCountedTest method testReleaseFromMultipleThreadsThrowsReferenceCountException.
@Test
@Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
public void testReleaseFromMultipleThreadsThrowsReferenceCountException() throws Exception {
int threads = 4;
Queue<Future<?>> futures = new ArrayDeque<Future<?>>(threads);
ExecutorService service = Executors.newFixedThreadPool(threads);
final AtomicInteger refCountExceptions = new AtomicInteger();
try {
for (int i = 0; i < 10000; i++) {
final AbstractReferenceCounted referenceCounted = newReferenceCounted();
final CountDownLatch releaseLatch = new CountDownLatch(1);
final AtomicInteger releasedCount = new AtomicInteger();
for (int a = 0; a < threads; a++) {
final AtomicInteger releaseCnt = new AtomicInteger(0);
futures.add(service.submit(new Runnable() {
@Override
public void run() {
try {
releaseLatch.await();
try {
if (referenceCounted.release(releaseCnt.incrementAndGet())) {
releasedCount.incrementAndGet();
}
} catch (IllegalReferenceCountException e) {
refCountExceptions.incrementAndGet();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}));
}
releaseLatch.countDown();
for (; ; ) {
Future<?> f = futures.poll();
if (f == null) {
break;
}
f.get();
}
assertEquals(3, refCountExceptions.get());
assertEquals(1, releasedCount.get());
refCountExceptions.set(0);
}
} finally {
service.shutdown();
}
}
use of org.junit.jupiter.api.Timeout in project netty by netty.
the class UnorderedThreadPoolEventExecutorTest method scheduledAtFixedRateMustRunTaskRepeatedly.
@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void scheduledAtFixedRateMustRunTaskRepeatedly() throws InterruptedException {
UnorderedThreadPoolEventExecutor executor = new UnorderedThreadPoolEventExecutor(1);
final CountDownLatch latch = new CountDownLatch(3);
Future<?> future = executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
latch.countDown();
}
}, 1, 1, TimeUnit.MILLISECONDS);
try {
latch.await();
} finally {
future.cancel(true);
executor.shutdownGracefully();
}
}
use of org.junit.jupiter.api.Timeout in project netty by netty.
the class SslHandlerTest method testHandshakeFailureOnlyFireExceptionOnce.
@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testHandshakeFailureOnlyFireExceptionOnce() throws Exception {
final SslContext sslClientCtx = SslContextBuilder.forClient().trustManager(new X509ExtendedTrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
failVerification();
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
failVerification();
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
failVerification();
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
failVerification();
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
failVerification();
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
failVerification();
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return EmptyArrays.EMPTY_X509_CERTIFICATES;
}
private void failVerification() throws CertificateException {
throw new CertificateException();
}
}).sslProvider(SslProvider.JDK).build();
final SelfSignedCertificate cert = new SelfSignedCertificate();
final SslContext sslServerCtx = SslContextBuilder.forServer(cert.key(), cert.cert()).sslProvider(SslProvider.JDK).build();
EventLoopGroup group = new NioEventLoopGroup();
Channel sc = null;
final SslHandler clientSslHandler = sslClientCtx.newHandler(UnpooledByteBufAllocator.DEFAULT);
final SslHandler serverSslHandler = sslServerCtx.newHandler(UnpooledByteBufAllocator.DEFAULT);
try {
final Object terminalEvent = new Object();
final BlockingQueue<Object> errorQueue = new LinkedBlockingQueue<Object>();
sc = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline().addLast(serverSslHandler);
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void exceptionCaught(final ChannelHandlerContext ctx, Throwable cause) {
errorQueue.add(cause);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
errorQueue.add(terminalEvent);
}
});
}
}).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
final ChannelFuture future = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline().addLast(clientSslHandler);
}
}).connect(sc.localAddress());
future.syncUninterruptibly();
clientSslHandler.handshakeFuture().addListener(new FutureListener<Channel>() {
@Override
public void operationComplete(Future<Channel> f) {
future.channel().close();
}
});
assertFalse(clientSslHandler.handshakeFuture().await().isSuccess());
assertFalse(serverSslHandler.handshakeFuture().await().isSuccess());
Object error = errorQueue.take();
assertThat(error, Matchers.instanceOf(DecoderException.class));
assertThat(((Throwable) error).getCause(), Matchers.<Throwable>instanceOf(SSLException.class));
Object terminal = errorQueue.take();
assertSame(terminalEvent, terminal);
assertNull(errorQueue.poll(1, TimeUnit.MILLISECONDS));
} finally {
if (sc != null) {
sc.close().syncUninterruptibly();
}
group.shutdownGracefully();
}
}
use of org.junit.jupiter.api.Timeout in project netty by netty.
the class SslHandlerTest method testRemoval.
@Test
@Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
public void testRemoval() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup();
Channel sc = null;
Channel cc = null;
try {
final Promise<Void> clientPromise = group.next().newPromise();
Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(newHandler(SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(), clientPromise));
SelfSignedCertificate ssc = new SelfSignedCertificate();
final Promise<Void> serverPromise = group.next().newPromise();
ServerBootstrap serverBootstrap = new ServerBootstrap().group(group, group).channel(NioServerSocketChannel.class).childHandler(newHandler(SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(), serverPromise));
sc = serverBootstrap.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
cc = bootstrap.connect(sc.localAddress()).syncUninterruptibly().channel();
serverPromise.syncUninterruptibly();
clientPromise.syncUninterruptibly();
} finally {
if (cc != null) {
cc.close().syncUninterruptibly();
}
if (sc != null) {
sc.close().syncUninterruptibly();
}
group.shutdownGracefully();
}
}
Aggregations