From 3adc6566d4a47a467d98ce76ea473beaa4e28515 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Mon, 1 Jun 2015 17:31:14 +0900 Subject: [PATCH] Fix sporadic failures in DatagramUnicastTest Motivation: DatagramUnitcastTest sometimes fails with BindException for an unknown reason. Modifications: Retry up to 3 times with a new free port when bind() fails with BindException Result: More build stability --- .../socket/AbstractDatagramTest.java | 5 +++ .../transport/socket/DatagramUnicastTest.java | 45 +++++++++++++++---- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/testsuite/src/main/java/io/netty/testsuite/transport/socket/AbstractDatagramTest.java b/testsuite/src/main/java/io/netty/testsuite/transport/socket/AbstractDatagramTest.java index 353f614f2b..3d951162ff 100644 --- a/testsuite/src/main/java/io/netty/testsuite/transport/socket/AbstractDatagramTest.java +++ b/testsuite/src/main/java/io/netty/testsuite/transport/socket/AbstractDatagramTest.java @@ -48,4 +48,9 @@ public abstract class AbstractDatagramTest extends AbstractComboTestsuiteTest() { + sb.handler(new ChannelInitializer() { @Override - public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { - ByteBuf buf = msg.content(); - assertEquals(bytes.length, buf.readableBytes()); - for (int i = 0; i < bytes.length; i++) { - assertEquals(bytes[i], buf.readByte()); - } - latch.countDown(); + protected void initChannel(Channel ch) throws Exception { + ch.pipeline().addLast(new SimpleChannelInboundHandler() { + @Override + public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { + ByteBuf buf = msg.content(); + assertEquals(bytes.length, buf.readableBytes()); + for (byte b: bytes) { + assertEquals(b, buf.readByte()); + } + latch.countDown(); + } + }); } }); @@ -146,7 +153,27 @@ public class DatagramUnicastTest extends AbstractDatagramTest { } }); - Channel sc = sb.bind().sync().channel(); + Channel sc = null; + BindException bindFailureCause = null; + for (int i = 0; i < 3; i ++) { + try { + sc = sb.bind().sync().channel(); + break; + } catch (Exception e) { + if (e instanceof BindException) { + logger.warn("Failed to bind to a free port; trying again", e); + bindFailureCause = (BindException) e; + refreshLocalAddress(sb); + } else { + throw e; + } + } + } + + if (sc == null) { + throw bindFailureCause; + } + Channel cc; if (bindClient) { cc = cb.bind().sync().channel();