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,57 +146,65 @@ 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 {
cb.handler(new SimpleChannelInboundHandler<Object>() { Channel sc = null;
@Override Channel cc = null;
public void channelRead0(ChannelHandlerContext ctx, Object msgs) throws Exception {
// Nothing will be sent. try {
cb.handler(new SimpleChannelInboundHandler<Object>() {
@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();
} }
}); InetSocketAddress addr = (InetSocketAddress) sc.localAddress();
for (int i = 0; i < count; i++) {
final CountDownLatch latch = new CountDownLatch(count); switch (wrapType) {
Channel sc = setupServerChannel(sb, bytes, latch); case DUP:
cc.write(new DatagramPacket(buf.retainedDuplicate(), addr));
Channel cc; break;
if (bindClient) { case SLICE:
cc = cb.bind(newSocketAddress()).sync().channel(); cc.write(new DatagramPacket(buf.retainedSlice(), addr));
} else { break;
cb.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true); case READ_ONLY:
cc = cb.register().sync().channel(); cc.write(new DatagramPacket(buf.retain().asReadOnly(), addr));
} break;
InetSocketAddress addr = (InetSocketAddress) sc.localAddress(); case NONE:
for (int i = 0; i < count; i++) { cc.write(new DatagramPacket(buf.retain(), addr));
switch (wrapType) { break;
case DUP: default:
cc.write(new DatagramPacket(buf.retainedDuplicate(), addr)); throw new Error("unknown wrap type: " + wrapType);
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
// release as we used buf.retain() before cc.flush();
buf.release(); assertTrue(latch.await(10, TimeUnit.SECONDS));
cc.flush(); } finally {
assertTrue(latch.await(10, TimeUnit.SECONDS)); // 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 {
testSimpleSendWithConnect0(sb, cb, buf.retain(), bytes, count, type); 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, 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 = 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();
}
}
} }