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.
This commit is contained in:
Norman Maurer 2017-09-07 13:51:36 +02:00
parent a739d89792
commit c0396818ca

View File

@ -146,6 +146,10 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
private void testSimpleSend0(Bootstrap sb, Bootstrap cb, ByteBuf buf, boolean bindClient, private void testSimpleSend0(Bootstrap sb, Bootstrap cb, ByteBuf buf, boolean bindClient,
final byte[] bytes, int count, WrapType wrapType) final byte[] bytes, int count, WrapType wrapType)
throws Throwable { throws Throwable {
Channel sc = null;
Channel cc = null;
try {
cb.handler(new SimpleChannelInboundHandler<Object>() { cb.handler(new SimpleChannelInboundHandler<Object>() {
@Override @Override
public void channelRead0(ChannelHandlerContext ctx, Object msgs) throws Exception { public void channelRead0(ChannelHandlerContext ctx, Object msgs) throws Exception {
@ -154,9 +158,7 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
}); });
final CountDownLatch latch = new CountDownLatch(count); final CountDownLatch latch = new CountDownLatch(count);
Channel sc = setupServerChannel(sb, bytes, latch); sc = setupServerChannel(sb, bytes, latch);
Channel cc;
if (bindClient) { if (bindClient) {
cc = cb.bind(newSocketAddress()).sync().channel(); cc = cb.bind(newSocketAddress()).sync().channel();
} else { } else {
@ -183,21 +185,27 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
} }
} }
// release as we used buf.retain() before // release as we used buf.retain() before
buf.release();
cc.flush(); cc.flush();
assertTrue(latch.await(10, TimeUnit.SECONDS)); assertTrue(latch.await(10, TimeUnit.SECONDS));
} finally {
// release as we used buf.retain() before
buf.release();
sc.close().sync(); closeChannel(cc);
cc.close().sync(); closeChannel(sc);
}
} }
private void testSimpleSendWithConnect(Bootstrap sb, Bootstrap cb, ByteBuf buf, final byte[] bytes, int count) private void testSimpleSendWithConnect(Bootstrap sb, Bootstrap cb, ByteBuf buf, final byte[] bytes, int count)
throws Throwable { throws Throwable {
for (WrapType type: WrapType.values()) { try {
for (WrapType type : WrapType.values()) {
testSimpleSendWithConnect0(sb, cb, buf.retain(), bytes, count, type); 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, private void testSimpleSendWithConnect0(Bootstrap sb, Bootstrap cb, ByteBuf buf, final byte[] bytes, int count,
WrapType wrapType) throws Throwable { WrapType wrapType) throws Throwable {
@ -208,10 +216,11 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
} }
}); });
final CountDownLatch latch = new CountDownLatch(count); Channel sc = null;
Channel sc = setupServerChannel(sb, bytes, latch);
DatagramChannel cc = null; DatagramChannel cc = null;
try { try {
final CountDownLatch latch = new CountDownLatch(count);
sc = setupServerChannel(sb, bytes, latch);
cc = (DatagramChannel) cb.connect(sc.localAddress()).sync().channel(); cc = (DatagramChannel) cb.connect(sc.localAddress()).sync().channel();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
@ -243,15 +252,14 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
ChannelFuture future = cc.writeAndFlush( ChannelFuture future = cc.writeAndFlush(
buf.retain().duplicate()).awaitUninterruptibly(); buf.retain().duplicate()).awaitUninterruptibly();
assertTrue(future.cause() instanceof NotYetConnectedException); assertTrue("NotYetConnectedException expected, got: " + future.cause(),
future.cause() instanceof NotYetConnectedException);
} finally { } finally {
// release as we used buf.retain() before // release as we used buf.retain() before
buf.release(); buf.release();
sc.close().sync(); closeChannel(cc);
if (cc != null) { closeChannel(sc);
cc.close().sync();
}
} }
} }
@ -276,4 +284,10 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
}); });
return sb.bind(newSocketAddress()).sync().channel(); return sb.bind(newSocketAddress()).sync().channel();
} }
private static void closeChannel(Channel channel) throws Exception {
if (channel != null) {
channel.close().sync();
}
}
} }