8042581: Intermittent failure in java/net/DatagramSocket/InheritHandle.java

Reviewed-by: alanb, chegar
This commit is contained in:
Chris Hegarty 2015-01-15 17:05:06 +00:00
parent cd582fa380
commit 00b2f7005d

View File

@ -22,27 +22,51 @@
*/ */
/* @test /* @test
* @bug 4945514 * @bug 4945514 8042581
* @summary DatagramSocket should make handle not inherited * @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 { public class InheritHandle {
private static final long SLEEPTIME_MS = 1000L;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
DatagramSocket sock = new DatagramSocket (0); int port;
sock.setReuseAddress(true); try (DatagramSocket sock = new DatagramSocket(0);) {
int port = sock.getLocalPort(); sock.setReuseAddress(true);
port = sock.getLocalPort();
/** /**
* spawn a child to check whether handle passed to it or not; * spawn a child to check whether handle passed to it or not; it
* it shouldn't * shouldn't
*/ */
Runtime.getRuntime().exec ("sleep 10"); Runtime.getRuntime().exec("sleep 10");
}
sock.close(); try (DatagramSocket sock = new DatagramSocket(null);) {
sock = new DatagramSocket (null); sock.setReuseAddress(true);
sock.setReuseAddress(true); int retries = 0;
sock.bind(new InetSocketAddress(port)); 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;
}
}
}
}
} }
} }