markrmiller commented on a change in pull request #1659:
URL: https://github.com/apache/hbase/pull/1659#discussion_r420467753



##########
File path: 
hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseCommonTestingUtility.java
##########
@@ -264,4 +265,34 @@ boolean deleteDir(final File dir) {
       boolean failIfTimeout, Predicate<E> predicate) throws E {
     return Waiter.waitFor(this.conf, timeout, interval, failIfTimeout, 
predicate);
   }
+
+  private static final PortAllocator portAllocator = new PortAllocator();
+
+  public static int randomFreePort() {
+    try {
+      return portAllocator.randomFreePort();
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  static class PortAllocator {
+
+    public PortAllocator() {
+
+    }
+
+    public int randomFreePort() throws IOException {
+      ServerSocket s = new ServerSocket(0);
+      try {
+        s.setReuseAddress(true);
+        int port = s.getLocalPort();
+        return port;
+      } finally {
+        if (null != s) {
+          s.close();
+        }
+      }
+    }
+  }
 }

Review comment:
       I suppose it's hard to make robust universally though. It's a 
problematic way to get a free ephem port. Just trying to address at these 
common get free port methods in some better way since they are available and 
used in a few places.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to