--- old/test/jdk/com/sun/jdi/ExclusiveBind.java 2018-10-19 09:46:37.412728600 -0700 +++ new/test/jdk/com/sun/jdi/ExclusiveBind.java 2018-10-19 09:46:35.751746100 -0700 @@ -32,7 +32,6 @@ * @build VMConnection ExclusiveBind HelloWorld * @run driver ExclusiveBind */ -import java.net.ServerSocket; import com.sun.jdi.Bootstrap; import com.sun.jdi.VirtualMachine; import com.sun.jdi.connect.Connector; @@ -43,6 +42,8 @@ import java.util.List; import java.util.Iterator; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import jdk.test.lib.process.ProcessTools; import jdk.test.lib.Utils; @@ -89,31 +90,38 @@ } /* - * - pick a TCP port - * - Launch a debuggee in server=y,suspend=y,address=${port} - * - Launch a second debuggee in server=y,suspend=n with the same port + * - Launch a debuggee in server=y,suspend=y,address=0 + * - Parse listening port + * - Launch a second debuggee in server=y,suspend=n with the parsed port * - Second debuggee should fail with an error (address already in use) * - For clean-up we attach to the first debuggee and resume it. */ public static void main(String args[]) throws Exception { - // find a free port - ServerSocket ss = new ServerSocket(0); - int port = ss.getLocalPort(); - ss.close(); - - String address = String.valueOf(port); - // launch the first debuggee - ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld"); + ProcessBuilder process1 = prepareLauncher("0", true, "HelloWorld"); // start the debuggee and wait for the "ready" message + Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(\\d+)\\b"); + // debuggeeListen[0] - transport, debuggeeListen[1] - address + String[] debuggeeListen = new String[2]; Process p = ProcessTools.startProcess( "process1", process1, - line -> line.equals("Listening for transport dt_socket at address: " + address), - Utils.adjustTimeout(5000), - TimeUnit.MILLISECONDS + line -> { // warm-up predicate + Matcher m = listenRegexp.matcher(line); + if (!m.matches()) { + return false; + } + debuggeeListen[0] = m.group(1); + debuggeeListen[1] = m.group(2); + return true; + }, + + Utils.adjustTimeout(30), + TimeUnit.SECONDS ); + String address = debuggeeListen[1]; + int port = Integer.parseInt(address); // launch a second debuggee with the same address ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");