Rework BmpMockDispatcherTest 06/74306/2
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Fri, 20 Jul 2018 06:51:43 +0000 (08:51 +0200)
committerClaudio David Gasparini <claudio.gasparini@pantheon.tech>
Sun, 22 Jul 2018 09:14:13 +0000 (09:14 +0000)
- Use test timeout instead of waitFutureSuccess utility
- use blocking futures

JIRA: BGPCEP-814
Change-Id: I1471226c703779222ed27f11154d8557b0ab7a65
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
(cherry picked from commit 3bab01236ef38a758a96bdaf6a73482bb1840891)

bmp/bmp-mock/src/test/java/org/opendaylight/protocol/bmp/mock/BmpMockDispatcherTest.java
testtool-util/src/main/java/org/opendaylight/protocol/util/InetSocketAddressUtil.java

index cc2c37583273cd85af731f62774b88ce67a727cd..0627f3e8e2de650adfb66daffeb0cc8320bf618c 100644 (file)
@@ -48,7 +48,7 @@ public class BmpMockDispatcherTest {
         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);
@@ -61,7 +61,6 @@ public class BmpMockDispatcherTest {
 
         final ChannelFuture channelFuture = this.bmpMockDispatcher.createClient(InetSocketAddressUtil
                 .getRandomLoopbackInetSocketAddress(0), serverAddr);
-        waitFutureSuccess(channelFuture);
         final Channel channel = channelFuture.sync().channel();
 
         assertTrue(channel.isActive());
@@ -74,7 +73,7 @@ public class BmpMockDispatcherTest {
                 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();
@@ -82,17 +81,16 @@ public class BmpMockDispatcherTest {
         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());
index 3248dc4f18df5736e92595649f38b53e58420f99..912306bacee9e2179051a8b51592c8fa7eba2a64 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.protocol.util;
 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;
 
@@ -19,6 +20,8 @@ import java.util.List;
  */
 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();
@@ -45,7 +48,12 @@ public final class InetSocketAddressUtil {
         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);
     }
 
@@ -58,8 +66,13 @@ public final class InetSocketAddressUtil {
      *
      * @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;
     }
 
     /**