diff --git a/src/test/java/io/floci/aws/lifecycle/EmulatorLifecycleTest.java b/src/test/java/io/floci/aws/lifecycle/EmulatorLifecycleTest.java index 6256998c97..207815e88a 100644 --- a/src/test/java/io/floci/aws/lifecycle/EmulatorLifecycleTest.java +++ b/src/test/java/io/floci/aws/lifecycle/EmulatorLifecycleTest.java @@ -50,6 +50,9 @@ import io.quarkus.runtime.ShutdownEvent; import io.quarkus.runtime.StartupEvent; import io.quarkus.vertx.http.HttpServerStart; import io.vertx.core.http.HttpServerOptions; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.enterprise.event.ObservesAsync; import jakarta.enterprise.inject.Instance; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -63,6 +66,7 @@ import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import java.io.IOException; +import java.lang.reflect.Method; import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -337,6 +341,19 @@ class EmulatorLifecycleTest { inOrder.verify(ecsService).restorePersistedRuntime(); } + @Test + void quarkusRunsTheBootRestoresWithoutWaitingForARequest() throws NoSuchMethodException { + // EcsService and EksService are created lazily, and creating them is what restores their + // persisted state; the tests above show onStart and onHttpStart do it. They only run at + // boot because the container fires them: onStart observes StartupEvent and onHttpStart + // observes HttpServerStart, on a bean the container manages. + assertTrue(EmulatorLifecycle.class.isAnnotationPresent(ApplicationScoped.class)); + Method onStart = EmulatorLifecycle.class.getDeclaredMethod("onStart", StartupEvent.class); + assertTrue(onStart.getParameters()[0].isAnnotationPresent(Observes.class)); + Method onHttpStart = EmulatorLifecycle.class.getDeclaredMethod("onHttpStart", HttpServerStart.class); + assertTrue(onHttpStart.getParameters()[0].isAnnotationPresent(ObservesAsync.class)); + } + @Test void restoresPersistedEksClustersOnceTheHttpPortListens() throws Exception { stubStorageConfig(); diff --git a/src/test/java/io/floci/aws/services/ecs/EcsBootReconcilerIntegrationTest.java b/src/test/java/io/floci/aws/services/ecs/EcsBootReconcilerIntegrationTest.java deleted file mode 100644 index c2e8e50011..0000000000 --- a/src/test/java/io/floci/aws/services/ecs/EcsBootReconcilerIntegrationTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package io.floci.aws.services.ecs; - -import io.quarkus.arc.Arc; -import io.quarkus.arc.InjectableBean; -import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.QuarkusTestProfile; -import io.quarkus.test.junit.TestProfile; -import jakarta.enterprise.context.ApplicationScoped; -import org.junit.jupiter.api.Test; - -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -/** - * EcsService is created lazily, and creating it is what loads the ECS stores and schedules the - * reconciler. Boot has to create it, or persisted services stay without tasks after a restart - * until the first ECS request. The profile of its own gives this test a fresh application that - * no other test has sent an ECS request to. - */ -@QuarkusTest -@TestProfile(EcsBootReconcilerIntegrationTest.FreshBootProfile.class) -class EcsBootReconcilerIntegrationTest { - - public static final class FreshBootProfile implements QuarkusTestProfile { - } - - @Test - void bootStartsTheReconcilerWithoutAnEcsRequest() { - Map, Object> created = Arc.container().getActiveContext(ApplicationScoped.class) - .getState().getContextualInstances(); - EcsService ecsService = created.entrySet().stream() - .filter(entry -> entry.getKey().getBeanClass() == EcsService.class) - .map(entry -> (EcsService) entry.getValue()) - .findFirst() - .orElse(null); - - assertNotNull(ecsService, "EcsService must be created at boot, not on the first ECS request"); - assertFalse(ecsService.isReconcilerShutdown()); - } -} diff --git a/src/test/java/io/floci/aws/services/eks/EksBootRestoreIntegrationTest.java b/src/test/java/io/floci/aws/services/eks/EksBootRestoreIntegrationTest.java deleted file mode 100644 index 79a7926bad..0000000000 --- a/src/test/java/io/floci/aws/services/eks/EksBootRestoreIntegrationTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package io.floci.aws.services.eks; - -import io.floci.aws.testutil.FreePorts; -import io.quarkus.arc.Arc; -import io.quarkus.arc.InjectableBean; -import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.QuarkusTestProfile; -import io.quarkus.test.junit.TestProfile; -import jakarta.enterprise.context.ApplicationScoped; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.time.Duration; -import java.util.Map; - -import static org.awaitility.Awaitility.await; -import static org.junit.jupiter.api.Assertions.assertTrue; - -/** - * EksService is created lazily, and creating it is what restarts each persisted cluster's k3s - * container. Boot has to create it, or persisted clusters stay down after a restart until the - * first EKS request. The profile of its own gives this test a fresh application that no other - * test has sent an EKS request to, with EKS out of mock mode as boot restores only real clusters. - */ -@QuarkusTest -@TestProfile(EksBootRestoreIntegrationTest.FreshBootProfile.class) -class EksBootRestoreIntegrationTest { - - public static final class FreshBootProfile implements QuarkusTestProfile { - // Boot restores once quarkus.http.port listens; the suite's random port 0 never matches the bound one. - private static final int TEST_PORT = freePort(); - - @Override - public Map getConfigOverrides() { - return Map.of( - "floci.services.eks.mock", "false", - "quarkus.http.test-port", String.valueOf(TEST_PORT), - "quarkus.http.port", String.valueOf(TEST_PORT), - "floci.port", String.valueOf(TEST_PORT), - "floci.base-url", "http://localhost:" + TEST_PORT); - } - - private static int freePort() { - try { - return FreePorts.anyInterfacePort(); - } catch (IOException e) { - throw new UncheckedIOException("No free port for the EKS boot test application", e); - } - } - } - - @Test - void bootCreatesEksServiceWithoutAnEksRequest() { - // Boot restores once the HTTP port listens, from an async observer, so this can run first. - await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { - Map, Object> created = Arc.container().getActiveContext(ApplicationScoped.class) - .getState().getContextualInstances(); - assertTrue(created.keySet().stream().anyMatch(bean -> bean.getBeanClass() == EksService.class), - "EksService must be created at boot, not on the first EKS request"); - }); - } -}