From c0396818cafc4d3b6a5c8230b9f91b180e813645 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 7 Sep 2017 13:51:36 +0200 Subject: [PATCH] Print out the actual cause when an assertion failure happens during DatagramUnicastTest.testSimpleSendWithConnect Motivation: We recently saw an assertion failure when running DatagramUnicastTest.testSimpleSendWithConnect. Modifications: - Adding more debug infos - Ensure we always correctly release the buffers. Result: More informations when tests fail. --- .../transport/socket/DatagramUnicastTest.java | 114 ++++++++++-------- 1 file changed, 64 insertions(+), 50 deletions(-) diff --git a/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java b/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java index 26491b56a4..26743ba8c7 100644 --- a/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java +++ b/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java @@ -146,57 +146,65 @@ public class DatagramUnicastTest extends AbstractDatagramTest { private void testSimpleSend0(Bootstrap sb, Bootstrap cb, ByteBuf buf, boolean bindClient, final byte[] bytes, int count, WrapType wrapType) throws Throwable { - cb.handler(new SimpleChannelInboundHandler() { - @Override - public void channelRead0(ChannelHandlerContext ctx, Object msgs) throws Exception { - // Nothing will be sent. + Channel sc = null; + Channel cc = null; + + try { + cb.handler(new SimpleChannelInboundHandler() { + @Override + public void channelRead0(ChannelHandlerContext ctx, Object msgs) throws Exception { + // Nothing will be sent. + } + }); + + final CountDownLatch latch = new CountDownLatch(count); + sc = setupServerChannel(sb, bytes, latch); + if (bindClient) { + cc = cb.bind(newSocketAddress()).sync().channel(); + } else { + cb.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true); + cc = cb.register().sync().channel(); } - }); - - final CountDownLatch latch = new CountDownLatch(count); - Channel sc = setupServerChannel(sb, bytes, latch); - - Channel cc; - if (bindClient) { - cc = cb.bind(newSocketAddress()).sync().channel(); - } else { - cb.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true); - cc = cb.register().sync().channel(); - } - InetSocketAddress addr = (InetSocketAddress) sc.localAddress(); - for (int i = 0; i < count; i++) { - switch (wrapType) { - case DUP: - cc.write(new DatagramPacket(buf.retainedDuplicate(), addr)); - break; - case SLICE: - cc.write(new DatagramPacket(buf.retainedSlice(), addr)); - break; - case READ_ONLY: - cc.write(new DatagramPacket(buf.retain().asReadOnly(), addr)); - break; - case NONE: - cc.write(new DatagramPacket(buf.retain(), addr)); - break; - default: - throw new Error("unknown wrap type: " + wrapType); + InetSocketAddress addr = (InetSocketAddress) sc.localAddress(); + for (int i = 0; i < count; i++) { + switch (wrapType) { + case DUP: + cc.write(new DatagramPacket(buf.retainedDuplicate(), addr)); + break; + case SLICE: + cc.write(new DatagramPacket(buf.retainedSlice(), addr)); + break; + case READ_ONLY: + cc.write(new DatagramPacket(buf.retain().asReadOnly(), addr)); + break; + case NONE: + cc.write(new DatagramPacket(buf.retain(), addr)); + break; + default: + throw new Error("unknown wrap type: " + wrapType); + } } - } - // release as we used buf.retain() before - buf.release(); - cc.flush(); - assertTrue(latch.await(10, TimeUnit.SECONDS)); + // release as we used buf.retain() before + cc.flush(); + assertTrue(latch.await(10, TimeUnit.SECONDS)); + } finally { + // release as we used buf.retain() before + buf.release(); - sc.close().sync(); - cc.close().sync(); + closeChannel(cc); + closeChannel(sc); + } } private void testSimpleSendWithConnect(Bootstrap sb, Bootstrap cb, ByteBuf buf, final byte[] bytes, int count) throws Throwable { - for (WrapType type: WrapType.values()) { - testSimpleSendWithConnect0(sb, cb, buf.retain(), bytes, count, type); + try { + for (WrapType type : WrapType.values()) { + testSimpleSendWithConnect0(sb, cb, buf.retain(), bytes, count, type); + } + } finally { + assertTrue(buf.release()); } - assertTrue(buf.release()); } private void testSimpleSendWithConnect0(Bootstrap sb, Bootstrap cb, ByteBuf buf, final byte[] bytes, int count, @@ -208,10 +216,11 @@ public class DatagramUnicastTest extends AbstractDatagramTest { } }); - final CountDownLatch latch = new CountDownLatch(count); - Channel sc = setupServerChannel(sb, bytes, latch); + Channel sc = null; DatagramChannel cc = null; try { + final CountDownLatch latch = new CountDownLatch(count); + sc = setupServerChannel(sb, bytes, latch); cc = (DatagramChannel) cb.connect(sc.localAddress()).sync().channel(); for (int i = 0; i < count; i++) { @@ -243,15 +252,14 @@ public class DatagramUnicastTest extends AbstractDatagramTest { ChannelFuture future = cc.writeAndFlush( buf.retain().duplicate()).awaitUninterruptibly(); - assertTrue(future.cause() instanceof NotYetConnectedException); + assertTrue("NotYetConnectedException expected, got: " + future.cause(), + future.cause() instanceof NotYetConnectedException); } finally { // release as we used buf.retain() before buf.release(); - sc.close().sync(); - if (cc != null) { - cc.close().sync(); - } + closeChannel(cc); + closeChannel(sc); } } @@ -276,4 +284,10 @@ public class DatagramUnicastTest extends AbstractDatagramTest { }); return sb.bind(newSocketAddress()).sync().channel(); } + + private static void closeChannel(Channel channel) throws Exception { + if (channel != null) { + channel.close().sync(); + } + } }