Skip to content

Commit

Permalink
Don't fail native test start when http is not exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
michalszynkiewicz committed Nov 16, 2021
1 parent 68d99bd commit 5c85c6a
Showing 1 changed file with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

public final class LauncherUtil {

public static final int LOG_CHECK_INTERVAL = 500;

private LauncherUtil() {
}

Expand Down Expand Up @@ -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) {

}
Expand Down Expand Up @@ -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());
}
}

/**
Expand All @@ -195,6 +199,7 @@ private static class CaptureListeningDataReader implements Runnable {
private final CountDownLatch signal;
private final AtomicReference<ListeningAddress> 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<ListeningAddress> resultReference) {
Expand All @@ -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)));
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 5c85c6a

Please sign in to comment.