8195703: BasicJDWPConnectionTest.java: 'App exited unexpectedly with 2'
authoramenkov
Fri, 12 Oct 2018 10:08:11 -0700
changeset 52110 65efb9c57fef
parent 52109 101c2b6eacbe
child 52111 367b2cd49ec5
8195703: BasicJDWPConnectionTest.java: 'App exited unexpectedly with 2' Reviewed-by: sspitsyn, jcbeyler
test/jdk/ProblemList.txt
test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java
test/jdk/com/sun/jdi/DoubleAgentTest.java
test/lib/jdk/test/lib/apps/LingeredApp.java
--- a/test/jdk/ProblemList.txt	Tue Oct 09 16:08:07 2018 +0530
+++ b/test/jdk/ProblemList.txt	Fri Oct 12 10:08:11 2018 -0700
@@ -835,8 +835,6 @@
 
 # jdk_jdi
 
-com/sun/jdi/BasicJDWPConnectionTest.java                        8195703 generic-all
-
 com/sun/jdi/RepStep.java                                        8043571 generic-all
 
 com/sun/jdi/sde/SourceDebugExtensionTest.java                   8158066 windows-all
--- a/test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java	Tue Oct 09 16:08:07 2018 +0530
+++ b/test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java	Fri Oct 12 10:08:11 2018 -0700
@@ -29,17 +29,15 @@
  */
 
 import java.io.IOException;
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
 
 import java.net.Socket;
 import java.net.SocketException;
 
 import jdk.test.lib.apps.LingeredApp;
-import jdk.test.lib.Utils;
 
 import java.util.ArrayList;
-import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 
 public class BasicJDWPConnectionTest {
@@ -64,25 +62,37 @@
         return res;
     }
 
-    public static ArrayList<String> prepareCmd(int port, String allowOpt) {
-         String address = "*:" + String.valueOf(port);
+    public static ArrayList<String> prepareCmd(String allowOpt) {
          ArrayList<String> cmd = new ArrayList<>();
 
          String jdwpArgs = "-agentlib:jdwp=transport=dt_socket,server=y," +
-                           "suspend=n,address=" + address + allowOpt;
+                           "suspend=n,address=*:0" + allowOpt;
          cmd.add(jdwpArgs);
          return cmd;
     }
 
+    private static Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(\\d+)\\b");
+    private static int detectPort(String s) {
+        Matcher m = listenRegexp.matcher(s);
+        if (!m.find()) {
+            throw new RuntimeException("Could not detect port from '" + s + "'");
+        }
+        // m.group(1) is transport, m.group(2) is port
+        return Integer.parseInt(m.group(2));
+    }
+
     public static void positiveTest(String testName, String allowOpt)
         throws InterruptedException, IOException {
         System.err.println("\nStarting " + testName);
-        int port = Utils.getFreePort();
-        ArrayList<String> cmd = prepareCmd(port, allowOpt);
+        ArrayList<String> cmd = prepareCmd(allowOpt);
 
         LingeredApp a = LingeredApp.startApp(cmd);
-        int res = handshake(port);
-        a.stopApp();
+        int res;
+        try {
+            res = handshake(detectPort(a.getProcessStdout()));
+        } finally {
+            a.stopApp();
+        }
         if (res < 0) {
             throw new RuntimeException(testName + " FAILED");
         }
@@ -92,12 +102,15 @@
     public static void negativeTest(String testName, String allowOpt)
         throws InterruptedException, IOException {
         System.err.println("\nStarting " + testName);
-        int port = Utils.getFreePort();
-        ArrayList<String> cmd = prepareCmd(port, allowOpt);
+        ArrayList<String> cmd = prepareCmd(allowOpt);
 
         LingeredApp a = LingeredApp.startApp(cmd);
-        int res = handshake(port);
-        a.stopApp();
+        int res;
+        try {
+            res = handshake(detectPort(a.getProcessStdout()));
+        } finally {
+            a.stopApp();
+        }
         if (res > 0) {
             System.err.println(testName + ": res=" + res);
             throw new RuntimeException(testName + " FAILED");
@@ -109,16 +122,18 @@
     public static void badAllowOptionTest(String testName, String allowOpt)
         throws InterruptedException, IOException {
         System.err.println("\nStarting " + testName);
-        int port = Utils.getFreePort();
-        ArrayList<String> cmd = prepareCmd(port, allowOpt);
+        ArrayList<String> cmd = prepareCmd(allowOpt);
 
+        LingeredApp a;
         try {
-            LingeredApp a = LingeredApp.startApp(cmd);
+            a = LingeredApp.startApp(cmd);
         } catch (IOException ex) {
             System.err.println(testName + ": caught expected IOException");
             System.err.println(testName + " PASSED");
             return;
         }
+        // LingeredApp.startApp is expected to fail, but if not, terminate the app
+        a.stopApp();
         throw new RuntimeException(testName + " FAILED");
     }
 
@@ -174,26 +189,16 @@
         badAllowOptionTest("ExplicitMultiDefault2Test", allowOpt);
     }
 
-    public static void main(String[] args) {
-        try {
-            DefaultTest();
-            ExplicitDefaultTest();
-            AllowTest();
-            MultiAllowTest();
-            DenyTest();
-            MultiDenyTest();
-            EmptyAllowOptionTest();
-            ExplicitMultiDefault1Test();
-            ExplicitMultiDefault2Test();
-            System.err.println("\nTest PASSED");
-        } catch (InterruptedException ex) {
-            System.err.println("\nTest ERROR, getFreePort");
-            ex.printStackTrace();
-            System.exit(3);
-        } catch (IOException ex) {
-            System.err.println("\nTest ERROR");
-            ex.printStackTrace();
-            System.exit(3);
-        }
+    public static void main(String[] args) throws Exception {
+        DefaultTest();
+        ExplicitDefaultTest();
+        AllowTest();
+        MultiAllowTest();
+        DenyTest();
+        MultiDenyTest();
+        EmptyAllowOptionTest();
+        ExplicitMultiDefault1Test();
+        ExplicitMultiDefault2Test();
+        System.err.println("\nTest PASSED");
     }
 }
--- a/test/jdk/com/sun/jdi/DoubleAgentTest.java	Tue Oct 09 16:08:07 2018 +0530
+++ b/test/jdk/com/sun/jdi/DoubleAgentTest.java	Fri Oct 12 10:08:11 2018 -0700
@@ -41,10 +41,8 @@
             "test.classes", ".");
 
     public static void main(String[] args) throws Throwable {
-        int port = Utils.getFreePort();
-
         String jdwpOption = "-agentlib:jdwp=transport=dt_socket"
-                         + ",server=y" + ",suspend=n" + ",address=*:" + String.valueOf(port);
+                         + ",server=y" + ",suspend=n" + ",address=*:0";
 
         OutputAnalyzer output = ProcessTools.executeTestJvm("-classpath",
                 TEST_CLASSES,
--- a/test/lib/jdk/test/lib/apps/LingeredApp.java	Tue Oct 09 16:08:07 2018 +0530
+++ b/test/lib/jdk/test/lib/apps/LingeredApp.java	Fri Oct 12 10:08:11 2018 -0700
@@ -26,9 +26,6 @@
 import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
 import java.io.StringReader;
 import java.nio.file.Files;
 import java.nio.file.NoSuchFileException;
@@ -43,7 +40,6 @@
 import java.util.stream.Collectors;
 import java.util.UUID;
 import jdk.test.lib.process.OutputBuffer;
-import jdk.test.lib.process.ProcessTools;
 import jdk.test.lib.process.StreamPumper;
 
 /**
@@ -137,6 +133,14 @@
     }
 
     /**
+     * @return the LingeredApp's output.
+     * Can be called after the app is run.
+     */
+    public String getProcessStdout() {
+        return stdoutBuffer.toString();
+    }
+
+    /**
      *
      * @return OutputBuffer object for the LingeredApp's output. Can only be called
      * after LingeredApp has exited.