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