changeset 54893:0076e23f333f

8214582: BasicJDWPConnectionTest.java: RuntimeException: Could not detect port from '' Reviewed-by: sspitsyn, dtitov
author amenkov
date Wed, 13 Feb 2019 11:08:51 -0800
parents 011c7262f5fe
children 5b78f051912b
files test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java
diffstat 1 files changed, 24 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java	Wed Feb 13 11:04:03 2019 -0800
+++ b/test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java	Wed Feb 13 11:08:51 2019 -0800
@@ -33,9 +33,11 @@
 import java.net.Socket;
 import java.net.SocketException;
 
+import jdk.test.lib.Utils;
 import jdk.test.lib.apps.LingeredApp;
 
 import java.util.ArrayList;
+import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -72,13 +74,27 @@
     }
 
     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 + "'");
+    private static int detectPort(LingeredApp app) {
+        long maxWaitTime = System.currentTimeMillis()
+                + Utils.adjustTimeout(10000);  // 10 seconds adjusted for TIMEOUT_FACTOR
+        while (true) {
+            String s = app.getProcessStdout();
+            Matcher m = listenRegexp.matcher(s);
+            if (m.find()) {
+                // m.group(1) is transport, m.group(2) is port
+                return Integer.parseInt(m.group(2));
+            }
+            if (System.currentTimeMillis() > maxWaitTime) {
+                throw new RuntimeException("Could not detect port from '" + s + "' (timeout)");
+            }
+            try {
+                if (app.getProcess().waitFor(500, TimeUnit.MILLISECONDS)) {
+                    throw new RuntimeException("Could not detect port from '" + s + "' (debuggee is terminated)");
+                }
+            } catch (InterruptedException e) {
+                // ignore
+            }
         }
-        // m.group(1) is transport, m.group(2) is port
-        return Integer.parseInt(m.group(2));
     }
 
     public static void positiveTest(String testName, String allowOpt)
@@ -89,7 +105,7 @@
         LingeredApp a = LingeredApp.startApp(cmd);
         int res;
         try {
-            res = handshake(detectPort(a.getProcessStdout()));
+            res = handshake(detectPort(a));
         } finally {
             a.stopApp();
         }
@@ -107,7 +123,7 @@
         LingeredApp a = LingeredApp.startApp(cmd);
         int res;
         try {
-            res = handshake(detectPort(a.getProcessStdout()));
+            res = handshake(detectPort(a));
         } finally {
             a.stopApp();
         }