From f03805c4efbc17f62e29a6dc31ad39fa8bea99e1 Mon Sep 17 00:00:00 2001 From: Alex Menkov Date: Wed, 13 Feb 2019 11:08:51 -0800 Subject: [PATCH] 8214582: BasicJDWPConnectionTest.java: RuntimeException: Could not detect port from '' Reviewed-by: sspitsyn, dtitov --- .../com/sun/jdi/BasicJDWPConnectionTest.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java b/test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java index 8911f5bfbde..a26cfb1ca51 100644 --- a/test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java +++ b/test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java @@ -33,9 +33,11 @@ import java.io.IOException; 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 @@ public class BasicJDWPConnectionTest { } 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 @@ public class BasicJDWPConnectionTest { LingeredApp a = LingeredApp.startApp(cmd); int res; try { - res = handshake(detectPort(a.getProcessStdout())); + res = handshake(detectPort(a)); } finally { a.stopApp(); } @@ -107,7 +123,7 @@ public class BasicJDWPConnectionTest { LingeredApp a = LingeredApp.startApp(cmd); int res; try { - res = handshake(detectPort(a.getProcessStdout())); + res = handshake(detectPort(a)); } finally { a.stopApp(); }