8042581: Intermittent failure in java/net/DatagramSocket/InheritHandle.java
authorchegar
Thu, 15 Jan 2015 17:05:06 +0000
changeset 28428 55242c4e5b0a
parent 28427 208a556546d3
child 28429 be279feaeb8b
8042581: Intermittent failure in java/net/DatagramSocket/InheritHandle.java Reviewed-by: alanb, chegar
jdk/test/java/net/DatagramSocket/InheritHandle.java
--- a/jdk/test/java/net/DatagramSocket/InheritHandle.java	Thu Jan 15 19:16:17 2015 +0400
+++ b/jdk/test/java/net/DatagramSocket/InheritHandle.java	Thu Jan 15 17:05:06 2015 +0000
@@ -22,27 +22,51 @@
  */
 
 /* @test
- * @bug 4945514
+ * @bug 4945514 8042581
  * @summary DatagramSocket should make handle not inherited
  */
 
-import java.net.*;
+import java.net.BindException;
+import java.net.DatagramSocket;
+import java.net.InetSocketAddress;
 
 public class InheritHandle {
+    private static final long SLEEPTIME_MS = 1000L;
+
     public static void main(String[] args) throws Exception {
-        DatagramSocket sock = new DatagramSocket (0);
-        sock.setReuseAddress(true);
-        int port = sock.getLocalPort();
+        int port;
+        try (DatagramSocket sock = new DatagramSocket(0);) {
+            sock.setReuseAddress(true);
+            port = sock.getLocalPort();
+
+            /**
+             * spawn a child to check whether handle passed to it or not; it
+             * shouldn't
+             */
+            Runtime.getRuntime().exec("sleep 10");
+        }
 
-        /**
-         * spawn a child to check whether handle passed to it or not;
-         * it shouldn't
-         */
-        Runtime.getRuntime().exec ("sleep 10");
+        try (DatagramSocket sock = new DatagramSocket(null);) {
+            sock.setReuseAddress(true);
+            int retries = 0;
+            boolean isWindows = System.getProperty("os.name").startsWith("Windows");
+            InetSocketAddress addr = new InetSocketAddress(port);
+            while (true) {
+                try {
+                    sock.bind(addr);
+                    break;
+                } catch (BindException e) {
+                    if (isWindows && retries++ < 5) {
+                        Thread.sleep(SLEEPTIME_MS);
+                        System.out.println("BindException \"" + e.getMessage() + "\", retrying...");
+                        continue;
+                    } else {
+                        throw e;
+                    }
+                }
+            }
 
-        sock.close();
-        sock = new DatagramSocket (null);
-        sock.setReuseAddress(true);
-        sock.bind(new InetSocketAddress(port));
+        }
     }
 }
+