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
This commit is contained in:
parent
add630c957
commit
3adc6566d4
@ -48,4 +48,9 @@ public abstract class AbstractDatagramTest extends AbstractComboTestsuiteTest<Bo
|
|||||||
bootstrap2.localAddress(0).remoteAddress(addr);
|
bootstrap2.localAddress(0).remoteAddress(addr);
|
||||||
bootstrap2.option(ChannelOption.ALLOCATOR, allocator);
|
bootstrap2.option(ChannelOption.ALLOCATOR, allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void refreshLocalAddress(Bootstrap bootstrap) {
|
||||||
|
addr = new InetSocketAddress(NetUtil.LOCALHOST4, TestUtils.getFreePort());
|
||||||
|
bootstrap.localAddress(addr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,11 +21,13 @@ import io.netty.buffer.CompositeByteBuf;
|
|||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.ChannelOption;
|
import io.netty.channel.ChannelOption;
|
||||||
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
import io.netty.channel.socket.DatagramPacket;
|
import io.netty.channel.socket.DatagramPacket;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.net.BindException;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@ -127,15 +129,20 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
|
|||||||
throws Throwable {
|
throws Throwable {
|
||||||
final CountDownLatch latch = new CountDownLatch(count);
|
final CountDownLatch latch = new CountDownLatch(count);
|
||||||
|
|
||||||
sb.handler(new SimpleChannelInboundHandler<DatagramPacket>() {
|
sb.handler(new ChannelInitializer<Channel>() {
|
||||||
@Override
|
@Override
|
||||||
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
|
protected void initChannel(Channel ch) throws Exception {
|
||||||
ByteBuf buf = msg.content();
|
ch.pipeline().addLast(new SimpleChannelInboundHandler<DatagramPacket>() {
|
||||||
assertEquals(bytes.length, buf.readableBytes());
|
@Override
|
||||||
for (int i = 0; i < bytes.length; i++) {
|
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
|
||||||
assertEquals(bytes[i], buf.readByte());
|
ByteBuf buf = msg.content();
|
||||||
}
|
assertEquals(bytes.length, buf.readableBytes());
|
||||||
latch.countDown();
|
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;
|
Channel cc;
|
||||||
if (bindClient) {
|
if (bindClient) {
|
||||||
cc = cb.bind().sync().channel();
|
cc = cb.bind().sync().channel();
|
||||||
|
Loading…
Reference in New Issue
Block a user