diff --git a/test-framework/common/src/main/java/io/quarkus/test/common/LauncherUtil.java b/test-framework/common/src/main/java/io/quarkus/test/common/LauncherUtil.java index fe3b846263cc6e..9c16089aa67c44 100644 --- a/test-framework/common/src/main/java/io/quarkus/test/common/LauncherUtil.java +++ b/test-framework/common/src/main/java/io/quarkus/test/common/LauncherUtil.java @@ -29,6 +29,8 @@ public final class LauncherUtil { + public static final int LOG_CHECK_INTERVAL = 500; + private LauncherUtil() { } @@ -106,7 +108,7 @@ private static void destroyProcess(Process quarkusProcess) { int i = 0; while (i++ < 10) { try { - Thread.sleep(500); + Thread.sleep(LOG_CHECK_INTERVAL); } catch (InterruptedException ignored) { } @@ -177,11 +179,13 @@ static IntegrationTestStartedNotifier.Result waitForStartedFunction( * process is listening */ static void updateConfigForPort(Integer effectivePort) { - System.setProperty("quarkus.http.port", effectivePort.toString()); //set the port as a system property in order to have it applied to Config - System.setProperty("quarkus.http.test-port", effectivePort.toString()); // needed for RestAssuredManager - installAndGetSomeConfig(); // reinitialize the configuration to make sure the actual port is used - System.clearProperty("test.url"); // make sure the old value does not interfere with setting the new one - System.setProperty("test.url", TestHTTPResourceManager.getUri()); + if (effectivePort != null) { + System.setProperty("quarkus.http.port", effectivePort.toString()); //set the port as a system property in order to have it applied to Config + System.setProperty("quarkus.http.test-port", effectivePort.toString()); // needed for RestAssuredManager + installAndGetSomeConfig(); // reinitialize the configuration to make sure the actual port is used + System.clearProperty("test.url"); // make sure the old value does not interfere with setting the new one + System.setProperty("test.url", TestHTTPResourceManager.getUri()); + } } /** @@ -195,6 +199,7 @@ private static class CaptureListeningDataReader implements Runnable { private final CountDownLatch signal; private final AtomicReference resultReference; private final Pattern listeningRegex = Pattern.compile("Listening on:\\s+(https?)://\\S*:(\\d+)"); + private final Pattern startedRegex = Pattern.compile(".*\\(powered by Quarkus \\d.*\\) started in \\d+.*s.*"); public CaptureListeningDataReader(Path processOutput, Duration waitTime, CountDownLatch signal, AtomicReference resultReference) { @@ -213,9 +218,19 @@ public void run() { long bailoutTime = System.currentTimeMillis() + waitTime.toMillis(); try (BufferedReader reader = new BufferedReader(new FileReader(processOutput.toFile()))) { + long timeStarted = Long.MAX_VALUE; + boolean started = false; + // generally, we want to start as soon as info about Quarkus having started is printed + // but just in case the line with http host and port is printed later, let's wait a bit more while (true) { if (reader.ready()) { // avoid blocking as the input is a file which continually gets more data added String line = reader.readLine(); + + if (startedRegex.matcher(line).matches()) { + timeStarted = System.currentTimeMillis(); + started = true; + } + Matcher regexMatcher = listeningRegex.matcher(line); if (regexMatcher.find()) { dataDetermined(regexMatcher.group(1), Integer.valueOf(regexMatcher.group(2))); @@ -228,17 +243,26 @@ public void run() { } } else { //wait until there is more of the file for us to read + + long now = System.currentTimeMillis(); + // if we have seen info that the app is started in the log a while ago + // or waiting the the next check interval will exceed the bailout time, it's time to finish waiting: + if (now + LOG_CHECK_INTERVAL > bailoutTime || now > timeStarted + 2 * LOG_CHECK_INTERVAL) { + if (started) { + dataDetermined(null, null); // no http, all is null + } else { + unableToDetermineData("Waited " + waitTime.getSeconds() + " seconds for " + processOutput + + " to contain info about the listening port and protocol but no such info was found"); + } + return; + } + try { - Thread.sleep(500); + Thread.sleep(LOG_CHECK_INTERVAL); } catch (InterruptedException e) { unableToDetermineData( - "Thread interrupted while waiting for more data to become available in proccess output file: " - + processOutput.toAbsolutePath().toString()); - return; - } - if (System.currentTimeMillis() > bailoutTime) { - unableToDetermineData("Waited " + waitTime.getSeconds() + "seconds for " + processOutput - + " to contain info about the listening port and protocol but no such info was found"); + "Thread interrupted while waiting for more data to become available in process output file: " + + processOutput.toAbsolutePath()); return; } }