8181383: com/sun/jdi/OptionTest.java fails intermittently with bind failed: Address already in use
authoramenkov
Tue, 26 Feb 2019 15:29:08 -0800
changeset 53929 ca23d3475af0
parent 53928 20ee41e8740d
child 53930 df3d253aaf81
8181383: com/sun/jdi/OptionTest.java fails intermittently with bind failed: Address already in use Reviewed-by: sspitsyn, cjplummer
test/jdk/com/sun/jdi/OptionTest.java
test/jdk/com/sun/jdi/RunToExit.java
--- a/test/jdk/com/sun/jdi/OptionTest.java	Mon Feb 25 18:50:29 2019 +0800
+++ b/test/jdk/com/sun/jdi/OptionTest.java	Tue Feb 26 15:29:08 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,8 +33,6 @@
  * @run driver OptionTest
  */
 
-import java.net.ServerSocket;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public class OptionTest extends Object {
@@ -127,18 +125,12 @@
     }
 
     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);
-
         String javaExe = System.getProperty("java.home") +
             java.io.File.separator + "bin" +
             java.io.File.separator + "java";
         String targetClass = "HelloWorld";
         String baseOptions = "transport=dt_socket" +
-                              ",address=" + address +
+                              ",address=0" +
                               ",server=y" +
                               ",suspend=n";
 
--- a/test/jdk/com/sun/jdi/RunToExit.java	Mon Feb 25 18:50:29 2019 +0800
+++ b/test/jdk/com/sun/jdi/RunToExit.java	Tue Feb 26 15:29:08 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -30,7 +30,6 @@
  * @build VMConnection RunToExit Exit0
  * @run driver RunToExit
  */
-import java.net.ServerSocket;
 import com.sun.jdi.Bootstrap;
 import com.sun.jdi.VirtualMachine;
 import com.sun.jdi.event.*;
@@ -41,6 +40,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 java.util.stream.Collectors;
 import jdk.test.lib.process.ProcessTools;
 
@@ -50,6 +51,9 @@
     static volatile int error_seen = 0;
     static volatile boolean ready = false;
 
+    /* port the debuggee is listening on */
+    private static String address;
+
     /*
      * Find a connector by name
      */
@@ -66,12 +70,11 @@
     }
 
     /*
-     * Launch a server debuggee with the given address
+     * Launch a server debuggee, detect debuggee listening port
      */
-    private static Process launch(String address, String class_name) throws Exception {
+    private static Process launch(String class_name) throws Exception {
         String args[] = new String[]{
-            "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
-                + address,
+            "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0",
             class_name
         };
         args = VMConnection.insertDebuggeeVMOptions(args);
@@ -92,8 +95,17 @@
         return p;
     }
 
+    /* warm-up predicate for debuggee */
+    private static Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(.+)\\b");
+
     private static boolean isTransportListening(String line) {
-        return line.startsWith("Listening for transport dt_socket");
+        Matcher m = listenRegexp.matcher(line);
+        if (!m.matches()) {
+            return false;
+        }
+        // address is 2nd group
+        address = m.group(2);
+        return true;
     }
 
     private static void checkForError(String line) {
@@ -103,28 +115,21 @@
     }
 
     /*
-     * - pick a TCP port
-     * - Launch a server debuggee: server=y,suspend=y,address=${port}
+     * - Launch a server debuggee: server=y,suspend=y,address=0
+     * - detect the port debuggee is listening on
      * - run it to VM death
      * - verify we saw no error
      */
     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 server debuggee
-        Process process = launch(address, "Exit0");
+        Process process = launch("Exit0");
 
         // attach to server debuggee and resume it so it can exit
         AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
         Map conn_args = conn.defaultArguments();
         Connector.IntegerArgument port_arg =
             (Connector.IntegerArgument)conn_args.get("port");
-        port_arg.setValue(port);
+        port_arg.setValue(address);
 
         System.out.println("Connection arguments: " + conn_args);