diff --git a/examples/picocli/src/test/java/io/quarkus/qe/picocli/HelloWorldIT.java b/examples/picocli/src/test/java/io/quarkus/qe/picocli/HelloWorldIT.java index 03a95560b..4820557ac 100644 --- a/examples/picocli/src/test/java/io/quarkus/qe/picocli/HelloWorldIT.java +++ b/examples/picocli/src/test/java/io/quarkus/qe/picocli/HelloWorldIT.java @@ -1,7 +1,5 @@ package io.quarkus.qe.picocli; -import static java.util.concurrent.CompletableFuture.runAsync; - import org.junit.jupiter.api.Test; import io.quarkus.test.bootstrap.RestService; @@ -15,12 +13,10 @@ public class HelloWorldIT { @QuarkusApplication static final RestService app = new RestService() - .withProperty("quarkus.args", "helloWorld -n " + NAME) - .setAutoStart(false); + .withProperty("quarkus.args", "helloWorld -n " + NAME); @Test public void verifyHelloWorldFormatted() { - runAsync(app::start); String expectedOutput = String.format("Hello %s!", NAME); app.logs().assertContains(expectedOutput); } diff --git a/quarkus-test-core/src/main/java/io/quarkus/test/bootstrap/BaseService.java b/quarkus-test-core/src/main/java/io/quarkus/test/bootstrap/BaseService.java index 25e35778f..040926dc5 100644 --- a/quarkus-test-core/src/main/java/io/quarkus/test/bootstrap/BaseService.java +++ b/quarkus-test-core/src/main/java/io/quarkus/test/bootstrap/BaseService.java @@ -3,6 +3,10 @@ import static io.quarkus.runtime.util.StringUtil.hyphenate; import static io.quarkus.test.utils.AwaitilityUtils.AwaitilitySettings; import static io.quarkus.test.utils.AwaitilityUtils.untilIsTrue; +import static io.quarkus.test.utils.TestExecutionProperties.isThisCliApp; +import static io.quarkus.test.utils.TestExecutionProperties.isThisStartedCliApp; +import static io.quarkus.test.utils.TestExecutionProperties.rememberThisAppStarted; +import static io.quarkus.test.utils.TestExecutionProperties.rememberThisCliAppStopped; import static org.junit.jupiter.api.Assertions.fail; import java.nio.file.Path; @@ -221,6 +225,22 @@ public void start() { return; } + // our FW tries to start each auto-started service twice + // I won't dare to change it until I have time to fix issues that I caused, but + // TODO: we should figure out why BaseService:start is called more than once + // once from the io.quarkus.test.bootstrap.QuarkusScenarioBootstrap.launchService + // and once from the io.quarkus.test.bootstrap.QuarkusScenarioBootstrap.beforeEach + // it doesn't matter for normal apps, but CLI app can launch and stop + // so it won't be running on the next "start()" + // so here, I am making sure that we remember the first start + if (isThisStartedCliApp(context)) { + return; + } else { + // we always need to remember this in case during the build we + // recognize this is a CLI app, which happens later + rememberThisAppStarted(context); + } + Log.debug(this, "Starting service (%s)", getDisplayName()); onPreStartActions.forEach(a -> a.handle(this)); doStart(); @@ -307,6 +327,11 @@ public void init(ManagedResourceBuilder managedResourceBuilder) { } public void restart() { + if (isThisCliApp(context)) { + // don't do that on the call to 'stop()' + // it could result in repeated start of the application + rememberThisCliAppStopped(context); + } managedResource.restart(); } @@ -352,6 +377,9 @@ private boolean isRunningOrFailed() { } private void waitUntilServiceIsStarted() { + if (isThisCliApp(this.context)) { + return; + } try { Duration startupCheckInterval = getConfiguration() .getAsDuration(SERVICE_STARTUP_CHECK_POLL_INTERVAL, SERVICE_STARTUP_CHECK_POLL_INTERVAL_DEFAULT); diff --git a/quarkus-test-core/src/main/java/io/quarkus/test/services/quarkus/ProdLocalhostQuarkusApplicationManagedResource.java b/quarkus-test-core/src/main/java/io/quarkus/test/services/quarkus/ProdLocalhostQuarkusApplicationManagedResource.java index 2971aefa8..63c741875 100644 --- a/quarkus-test-core/src/main/java/io/quarkus/test/services/quarkus/ProdLocalhostQuarkusApplicationManagedResource.java +++ b/quarkus-test-core/src/main/java/io/quarkus/test/services/quarkus/ProdLocalhostQuarkusApplicationManagedResource.java @@ -8,6 +8,7 @@ import org.apache.commons.lang3.ArrayUtils; import io.quarkus.test.configuration.PropertyLookup; +import io.quarkus.test.utils.TestExecutionProperties; public class ProdLocalhostQuarkusApplicationManagedResource extends LocalhostQuarkusApplicationManagedResource { @@ -57,6 +58,7 @@ private String[] extractQuarkusArgs(List systemProperties) { if (property.contains(QUARKUS_ARGS_PROPERTY_NAME)) { propertiesIt.remove(); args = property.replace("-D" + QUARKUS_ARGS_PROPERTY_NAME + "=", "").split(" "); + TestExecutionProperties.rememberThisIsCliApp(this.getContext()); break; } } diff --git a/quarkus-test-core/src/main/java/io/quarkus/test/utils/TestExecutionProperties.java b/quarkus-test-core/src/main/java/io/quarkus/test/utils/TestExecutionProperties.java index 35a81c053..3bbaad68f 100644 --- a/quarkus-test-core/src/main/java/io/quarkus/test/utils/TestExecutionProperties.java +++ b/quarkus-test-core/src/main/java/io/quarkus/test/utils/TestExecutionProperties.java @@ -1,6 +1,7 @@ package io.quarkus.test.utils; import io.quarkus.test.bootstrap.Service; +import io.quarkus.test.bootstrap.ServiceContext; import io.quarkus.test.configuration.PropertyLookup; import io.quarkus.test.services.quarkus.model.QuarkusProperties; @@ -14,6 +15,8 @@ public final class TestExecutionProperties { private static final String DEFAULT_SERVICE_NAME = "quarkus_test_framework"; private static final String DEFAULT_BUILD_NUMBER = "777-default"; private static final TestExecutionProperties INSTANCE = new TestExecutionProperties(); + private static final String CLI_APP_PROPERTY_KEY = "ts-internal.is-cli-app"; + private static final String APP_STARTED_KEY = "ts-internal.app-started"; private final String serviceName; private final String buildNumber; @@ -56,4 +59,24 @@ public static String getBuildNumber() { public static boolean useManagementSsl(Service service) { return service.getProperty(MANAGEMENT_INTERFACE_ENABLED).map(Boolean::parseBoolean).orElse(false); } + + public static void rememberThisIsCliApp(ServiceContext context) { + context.put(CLI_APP_PROPERTY_KEY, Boolean.TRUE.toString()); + } + + public static boolean isThisCliApp(ServiceContext context) { + return Boolean.parseBoolean(context.get(CLI_APP_PROPERTY_KEY)); + } + + public static boolean isThisStartedCliApp(ServiceContext context) { + return isThisCliApp(context) && Boolean.parseBoolean(context.get(APP_STARTED_KEY)); + } + + public static void rememberThisAppStarted(ServiceContext context) { + context.put(APP_STARTED_KEY, Boolean.TRUE.toString()); + } + + public static void rememberThisCliAppStopped(ServiceContext context) { + context.put(APP_STARTED_KEY, Boolean.FALSE.toString()); + } }