|
17 | 17 |
|
18 | 18 | import io.netty.bootstrap.Bootstrap; |
19 | 19 | import io.netty.buffer.Unpooled; |
| 20 | +import io.netty.channel.ChannelFuture; |
20 | 21 | import io.netty.channel.ChannelHandlerContext; |
21 | 22 | import io.netty.channel.ChannelOption; |
22 | 23 | import io.netty.channel.SimpleChannelInboundHandler; |
@@ -81,25 +82,34 @@ public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception |
81 | 82 | cb.option(ChannelOption.IP_MULTICAST_IF, iface); |
82 | 83 | cb.option(ChannelOption.SO_REUSEADDR, true); |
83 | 84 |
|
84 | | - DatagramChannel sc = (DatagramChannel) sb.bind(newSocketAddress(iface)).sync().channel(); |
85 | | - assertEquals(iface, sc.config().getNetworkInterface()); |
86 | | - assertInterfaceAddress(iface, sc.config().getInterface()); |
| 85 | + DatagramChannel sc = null; |
87 | 86 |
|
88 | | - InetSocketAddress addr = sc.localAddress(); |
89 | | - cb.localAddress(addr.getPort()); |
90 | | - |
91 | | - if (sc instanceof OioDatagramChannel) { |
92 | | - // skip the test for OIO, as it fails because of |
93 | | - // No route to host which makes no sense. |
94 | | - // Maybe a JDK bug ? |
95 | | - sc.close().awaitUninterruptibly(); |
96 | | - return; |
97 | | - } |
98 | | - DatagramChannel cc = (DatagramChannel) cb.bind().sync().channel(); |
| 87 | + int attempts = 5; |
| 88 | + ChannelFuture clientFuture; |
| 89 | + do { |
| 90 | + if (sc != null) { |
| 91 | + sc.close().sync(); |
| 92 | + } |
| 93 | + sc = (DatagramChannel) sb.bind(newSocketAddress(iface)).sync().channel(); |
| 94 | + if (sc instanceof OioDatagramChannel) { |
| 95 | + // skip the test for OIO, as it fails because of |
| 96 | + // No route to host which makes no sense. |
| 97 | + // Maybe a JDK bug ? |
| 98 | + sc.close().awaitUninterruptibly(); |
| 99 | + return; |
| 100 | + } |
| 101 | + assertEquals(iface, sc.config().getNetworkInterface()); |
| 102 | + assertInterfaceAddress(iface, sc.config().getInterface()); |
| 103 | + |
| 104 | + InetSocketAddress addr = sc.localAddress(); |
| 105 | + cb.localAddress(addr.getPort()); |
| 106 | + clientFuture = cb.bind().await(); |
| 107 | + } while (!clientFuture.isSuccess() && --attempts > 0); |
| 108 | + DatagramChannel cc = (DatagramChannel) clientFuture.sync().channel(); |
99 | 109 | assertEquals(iface, cc.config().getNetworkInterface()); |
100 | 110 | assertInterfaceAddress(iface, cc.config().getInterface()); |
101 | 111 |
|
102 | | - InetSocketAddress groupAddress = SocketUtils.socketAddress(groupAddress(), addr.getPort()); |
| 112 | + InetSocketAddress groupAddress = SocketUtils.socketAddress(groupAddress(), sc.localAddress().getPort()); |
103 | 113 |
|
104 | 114 | cc.joinGroup(groupAddress, iface).sync(); |
105 | 115 |
|
|