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



##########
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:
       Lets get rid of all impls except this one that you and I moved back into 
common testing utils.
   
   We get the port number but it could be taken by another by the time we get 
to use it so handling bindexception seems needed everywhere. Catching has been 
generalized a bunch but still a few places where we don't catch the bind 
exception.




----------------------------------------------------------------
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