Use TestTools.findAvailablePort

- Use TestTools.findAvailablePort when selecting unused port during unit tests

Change-Id: I155781db8830fc3d5238bda0b23cbbf065ba7b51
diff --git a/incubator/rpc-grpc/src/test/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceTest.java b/incubator/rpc-grpc/src/test/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceTest.java
index 243141b..e366da3 100644
--- a/incubator/rpc-grpc/src/test/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceTest.java
+++ b/incubator/rpc-grpc/src/test/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceTest.java
@@ -19,8 +19,6 @@
 import static org.onosproject.net.DeviceId.deviceId;
 import static org.onosproject.net.PortNumber.portNumber;
 
-import java.io.IOException;
-import java.net.ServerSocket;
 import java.net.URI;
 import java.util.Collection;
 import java.util.Collections;
@@ -29,10 +27,10 @@
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
-import org.apache.commons.lang3.RandomUtils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.onlab.junit.TestTools;
 import org.onlab.packet.ChassisId;
 import org.onosproject.incubator.rpc.RemoteServiceContext;
 import org.onosproject.incubator.rpc.RemoteServiceContextProvider;
@@ -114,28 +112,15 @@
 
     private URI uri;
 
-    public static int pickListenPort() {
-        try {
-            // pick unused port
-            ServerSocket socket = new ServerSocket(0);
-            int port = socket.getLocalPort();
-            socket.close();
-            return port;
-        } catch (IOException e) {
-            // something went wrong, try picking randomly
-            return RandomUtils.nextInt(49152, 0xFFFF + 1);
-        }
-    }
-
     @Before
     public void setUp() throws Exception {
         serverReady = new CountDownLatch(1);
         server = new GrpcRemoteServiceServer();
         server.deviceProviderRegistry = new MTestDeviceProviderRegistry();
         server.linkProviderRegistry = new ServerSideLinkProviderRegistry();
-        // todo: pass proper ComponentContext
-        server.listenPort = pickListenPort();
+        server.listenPort = TestTools.findAvailablePort(11984);
         uri = URI.create("grpc://localhost:" + server.listenPort);
+        // todo: pass proper ComponentContext
         server.activate(null);
 
         client = new GrpcRemoteServiceProvider();
diff --git a/utils/netty/src/test/java/org/onlab/netty/NettyMessagingTest.java b/utils/netty/src/test/java/org/onlab/netty/NettyMessagingTest.java
index 7086c1e..f7737d2 100644
--- a/utils/netty/src/test/java/org/onlab/netty/NettyMessagingTest.java
+++ b/utils/netty/src/test/java/org/onlab/netty/NettyMessagingTest.java
@@ -19,6 +19,7 @@
 import com.google.common.util.concurrent.Uninterruptibles;
 
 import static org.junit.Assert.*;
+import static org.onlab.junit.TestTools.findAvailablePort;
 
 /**
  * Unit tests for NettyMessaging.
@@ -34,10 +35,12 @@
 
     @Before
     public void setUp() throws Exception {
+        ep1 = new Endpoint(IpAddress.valueOf("127.0.0.1"), findAvailablePort(5001));
         netty1 = new NettyMessaging();
-        netty2 = new NettyMessaging();
-
         netty1.start(12, ep1);
+
+        ep2 = new Endpoint(IpAddress.valueOf("127.0.0.1"), findAvailablePort(5003));
+        netty2 = new NettyMessaging();
         netty2.start(12, ep2);
     }