aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestTools.java
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestTools.java')
-rw-r--r--framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestTools.java17
1 files changed, 17 insertions, 0 deletions
diff --git a/framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestTools.java b/framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestTools.java
index e2fcefce..40e8686c 100644
--- a/framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestTools.java
+++ b/framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestTools.java
@@ -20,6 +20,7 @@ import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
+import java.net.ServerSocket;
import java.util.List;
import java.util.Random;
@@ -207,4 +208,20 @@ public final class TestTools {
}
}
+ /*
+ * Finds an available port that a test can bind to.
+ */
+ public static int findAvailablePort(int defaultPort) {
+ try {
+ ServerSocket socket = new ServerSocket(0);
+ socket.setReuseAddress(true);
+ int port = socket.getLocalPort();
+ socket.close();
+ return port;
+ } catch (IOException ex) {
+ return defaultPort;
+ }
+ }
+
+
}