This is an automated email from the ASF dual-hosted git repository.

sijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new f6af755  [MERGE YAHOO REPO] YBK-154: Add sleep after finding free port 
given OS time to release it
f6af755 is described below

commit f6af755fd7f5f53b3b5b743bf4f314bcfa8cc89a
Author: Robert Evans <[email protected]>
AuthorDate: Tue Jan 30 11:48:31 2018 -0800

    [MERGE YAHOO REPO] YBK-154: Add sleep after finding free port given OS time 
to release it
    
    Descriptions of the changes in this PR:
    This is cherry-pick from yahoo repo of branch yahoo-4.3.
    
    original change is:
    https://github.com/yahoo/bookkeeper/commit/40196007
    YBK-154: Addign sleep after finding free port so the OS has time to release 
it
    
    Author: Robert Evans <[email protected]>
    
    Reviewers: Enrico Olivelli <[email protected]>, Sijie Guo 
<[email protected]>
    
    This closes #1055 from jiazhai/cherry_picks/ii_205
---
 .../org/apache/bookkeeper/test/PortManager.java    | 23 ++++++++++------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/PortManager.java 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/PortManager.java
index d52e9c7..0a4b8ab 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/PortManager.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/PortManager.java
@@ -20,9 +20,7 @@
  */
 package org.apache.bookkeeper.test;
 
-import java.io.IOException;
 import java.net.ServerSocket;
-
 /**
  * Port manager allows a base port to be specified on the commandline.
  * Tests will then use ports, counting up from this base port.
@@ -32,19 +30,18 @@ public class PortManager {
     private static int nextPort = getBasePort();
 
     public static synchronized int nextFreePort() {
+        int exceptionCount = 0;
         while (true) {
-            ServerSocket ss = null;
-            try {
-                int port = nextPort++;
-                ss = new ServerSocket(port);
-                ss.setReuseAddress(true);
+            int port = nextPort++;
+            try (ServerSocket ss = new ServerSocket(port)) {
+                ss.close();
+                //Give it some time to truly close the connection
+                Thread.sleep(100);
                 return port;
-            } catch (IOException ioe) {
-            } finally {
-                if (ss != null) {
-                    try {
-                        ss.close();
-                    } catch (IOException ioe) {}
+            } catch (Exception e) {
+                exceptionCount++;
+                if (exceptionCount > 5) {
+                    throw new RuntimeException(e);
                 }
             }
         }

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to