diff --git a/src/test/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPoolTest.java b/src/test/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPoolTest.java index 1d203eb70a..b504f9fea9 100644 --- a/src/test/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPoolTest.java +++ b/src/test/java/io/floci/aws/services/lambda/ProvisionedConcurrencyPoolTest.java @@ -741,8 +741,11 @@ class ProvisionedConcurrencyPoolTest { await().atMost(Duration.ofSeconds(5)).until(() -> "FAILED".equals(get().status())); assertTrue(get().reason().contains("controlled init failure")); assertEquals(0, get().allocated()); + // The worker publishes FAILED before it stops the environment it gave up on, and a put is + // refused until that stop has finished, so the retry is what the test waits for. + await().atMost(Duration.ofSeconds(5)).ignoreExceptionsInstanceOf(AwsException.class) + .until(() -> put(1) != null); assertEquals(1, first.stops.get()); - put(1); Environment second = launched(); second.initialized.complete(null); ready(); diff --git a/src/test/java/io/floci/aws/testing/AwaitilityDefaults.java b/src/test/java/io/floci/aws/testing/AwaitilityDefaults.java new file mode 100644 index 0000000000..cdb555fb24 --- /dev/null +++ b/src/test/java/io/floci/aws/testing/AwaitilityDefaults.java @@ -0,0 +1,35 @@ +package io.floci.aws.testing; + +import io.quarkus.test.junit.callback.QuarkusTestBeforeClassCallback; +import org.awaitility.Awaitility; +import org.junit.platform.launcher.LauncherSession; +import org.junit.platform.launcher.LauncherSessionListener; + +import java.time.Duration; + +/** + * Awaitility sleeps one poll interval before its first check unless told otherwise, so every + * {@code await()} paid 100 ms even when its condition already held. This makes the first check + * immediate for the whole suite; an await that needs a delay sets {@code pollDelay} itself. + * + *
Awaitility's defaults are static, and a {@code @QuarkusTest} class sees its own copy of + * Awaitility, loaded by the Quarkus runtime class loader. So the class is registered twice: as a + * launcher session listener for the copy plain test classes see, and as a Quarkus before-class + * callback, which Quarkus loads through the runtime class loader of each test application. + */ +public class AwaitilityDefaults implements LauncherSessionListener, QuarkusTestBeforeClassCallback { + + @Override + public void launcherSessionOpened(LauncherSession session) { + applyDefaults(); + } + + @Override + public void beforeClass(Class> testClass) { + applyDefaults(); + } + + private static void applyDefaults() { + Awaitility.setDefaultPollDelay(Duration.ZERO); + } +} diff --git a/src/test/java/io/floci/aws/testing/AwaitilityDefaultsIntegrationTest.java b/src/test/java/io/floci/aws/testing/AwaitilityDefaultsIntegrationTest.java new file mode 100644 index 0000000000..6be1359b40 --- /dev/null +++ b/src/test/java/io/floci/aws/testing/AwaitilityDefaultsIntegrationTest.java @@ -0,0 +1,13 @@ +package io.floci.aws.testing; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +@QuarkusTest +class AwaitilityDefaultsIntegrationTest { + + @Test + void awaitInAQuarkusTestChecksAHeldConditionWithoutSleepingFirst() { + AwaitilityDefaultsTest.assertFirstCheckIsImmediate(); + } +} diff --git a/src/test/java/io/floci/aws/testing/AwaitilityDefaultsTest.java b/src/test/java/io/floci/aws/testing/AwaitilityDefaultsTest.java new file mode 100644 index 0000000000..6723192986 --- /dev/null +++ b/src/test/java/io/floci/aws/testing/AwaitilityDefaultsTest.java @@ -0,0 +1,34 @@ +package io.floci.aws.testing; + +import org.junit.jupiter.api.Test; + +import java.time.Duration; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.*; + +class AwaitilityDefaultsTest { + + private static final Duration DEFAULT_POLL_DELAY = Duration.ofMillis(100); + + @Test + void awaitChecksAHeldConditionWithoutSleepingFirst() { + assertFirstCheckIsImmediate(); + } + + /** + * With Awaitility's own default every await sleeps 100 ms before its first check, so the + * fastest of several attempts tells the two apart without depending on a quiet machine. + */ + static void assertFirstCheckIsImmediate() { + long fastest = Long.MAX_VALUE; + for (int attempt = 0; attempt < 5; attempt++) { + long start = System.nanoTime(); + await().atMost(Duration.ofSeconds(5)).until(() -> true); + fastest = Math.min(fastest, System.nanoTime() - start); + } + assertTrue(fastest < DEFAULT_POLL_DELAY.toNanos(), + "an await on a held condition took " + Duration.ofNanos(fastest).toMillis() + + " ms: AwaitilityDefaults did not reach this class loader's Awaitility"); + } +} diff --git a/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeClassCallback b/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeClassCallback new file mode 100644 index 0000000000..30faca7217 --- /dev/null +++ b/src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeClassCallback @@ -0,0 +1 @@ +io.floci.aws.testing.AwaitilityDefaults diff --git a/src/test/resources/META-INF/services/org.junit.platform.launcher.LauncherSessionListener b/src/test/resources/META-INF/services/org.junit.platform.launcher.LauncherSessionListener new file mode 100644 index 0000000000..30faca7217 --- /dev/null +++ b/src/test/resources/META-INF/services/org.junit.platform.launcher.LauncherSessionListener @@ -0,0 +1 @@ +io.floci.aws.testing.AwaitilityDefaults