this.bmpMockDispatcher = new BmpMockDispatcher(this.registry, this.sessionFactory);
}
- @Test
+ @Test(timeout = 20000)
public void testCreateClient() throws Exception {
final int port = InetSocketAddressUtil.getRandomPort();
final InetSocketAddress serverAddr = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
final ChannelFuture channelFuture = this.bmpMockDispatcher.createClient(InetSocketAddressUtil
.getRandomLoopbackInetSocketAddress(0), serverAddr);
- waitFutureSuccess(channelFuture);
final Channel channel = channelFuture.sync().channel();
assertTrue(channel.isActive());
new NioEventLoopGroup(), new NioEventLoopGroup(), this.registry, this.sessionFactory);
final ChannelFuture futureServer2 = bmpDispatcher2
.createServer(serverAddr, this.slf, KeyMapping.getKeyMapping());
- waitFutureSuccess(futureServer2);
+ futureServer2.sync();
checkEquals(() -> assertTrue(this.sl.getStatus()));
bmpDispatcher2.close();
checkEquals(() -> assertFalse(this.sl.getStatus()));
}
- @Test
+ @Test(timeout = 20000)
public void testCreateServer() throws Exception {
final int port = InetSocketAddressUtil.getRandomPort();
final BmpDispatcherImpl bmpDispatcher = new BmpDispatcherImpl(
new NioEventLoopGroup(), new NioEventLoopGroup(), this.registry, this.sessionFactory);
final ChannelFuture futureServer = this.bmpMockDispatcher.createServer(
new InetSocketAddress(InetAddresses.forString("0.0.0.0"), port));
- waitFutureSuccess(futureServer);
+ futureServer.sync();
final ChannelFuture channelFuture = bmpDispatcher.createClient(
InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port), this.slf, KeyMapping.getKeyMapping());
- waitFutureSuccess(channelFuture);
final Channel channel = channelFuture.sync().channel();
assertTrue(channel.isActive());
import com.google.common.collect.Lists;
import com.google.common.net.HostAndPort;
import java.net.InetSocketAddress;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
*/
public final class InetSocketAddressUtil {
private static final String SEPARATOR = ",";
+ private static final List<String> ASSIGNED_IPS = new ArrayList<>();
+ private static final List<Integer> ASSIGNED_PORTS = new ArrayList<>();
private InetSocketAddressUtil() {
throw new UnsupportedOperationException();
return new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort());
}
- public static InetSocketAddress getRandomLoopbackInetSocketAddress(final int port) {
+ public static synchronized InetSocketAddress getRandomLoopbackInetSocketAddress(final int port) {
+ String newIp;
+ do {
+ newIp = getRandomLoopbackIpAddress();
+ } while (ASSIGNED_IPS.contains(newIp));
+ ASSIGNED_IPS.add(newIp);
return new InetSocketAddress(getRandomLoopbackIpAddress(), port);
}
*
* @return A port number range from 20000 to 60000
*/
- public static int getRandomPort() {
- return 20000 + (int) Math.round(40000 * Math.random());
+ public static synchronized int getRandomPort() {
+ int port;
+ do {
+ port = 20000 + (int) Math.round(40000 * Math.random());
+ } while (ASSIGNED_PORTS.contains(port));
+ ASSIGNED_PORTS.add(port);
+ return port;
}
/**