diff --git a/src/test/java/io/floci/aws/core/common/docker/DockerClientProducerTest.java b/src/test/java/io/floci/aws/core/common/docker/DockerClientProducerTest.java index 9db903a983..69f79dfbe7 100644 --- a/src/test/java/io/floci/aws/core/common/docker/DockerClientProducerTest.java +++ b/src/test/java/io/floci/aws/core/common/docker/DockerClientProducerTest.java @@ -7,6 +7,7 @@ import com.github.dockerjava.core.DefaultDockerClientConfig; import io.floci.aws.config.EmulatorConfig; import io.floci.aws.core.common.Digests; import io.floci.aws.testing.ParallelLocks; +import org.apache.hc.core5.http.ConnectionRequestTimeoutException; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.parallel.ResourceAccessMode; @@ -30,6 +31,7 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.stream.Stream; +import static io.floci.aws.core.common.docker.DockerTestSupport.rootCause; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.*; @@ -49,6 +51,7 @@ import static org.mockito.Mockito.when; * assertion failure, normalizeDockerHost method does not exist yet, * confirming the missing normalization logic). */ +@ResourceLock(value = ParallelLocks.DOCKER_ACTIVE_STREAMS, mode = ResourceAccessMode.READ) @ResourceLock(value = ParallelLocks.PORTS, mode = ResourceAccessMode.READ) class DockerClientProducerTest { @@ -766,6 +769,7 @@ class DockerClientProducerTest { void dockerClient_opensAtMostMaxConnectionsToTheDaemon() throws Exception { try (ServerSocket daemon = new ServerSocket(0, 50, InetAddress.getLoopbackAddress())) { BlockingQueue accepted = new LinkedBlockingQueue<>(); + BlockingQueue failures = new LinkedBlockingQueue<>(); Thread acceptor = new Thread(() -> { try { while (!daemon.isClosed()) { @@ -784,7 +788,7 @@ class DockerClientProducerTest { when(docker.dockerHost()).thenReturn("tcp://127.0.0.1:" + daemon.getLocalPort()); when(docker.dockerConfigPath()).thenReturn(Optional.empty()); when(docker.maxConnections()).thenReturn(2); - when(docker.connectionRequestTimeoutSeconds()).thenReturn(30); + when(docker.connectionRequestTimeoutSeconds()).thenReturn(1); when(docker.apiTimeoutSeconds()).thenReturn(300); DockerClient client = new DockerClientProducer(config).dockerClient(); @@ -795,18 +799,21 @@ class DockerClientProducerTest { .withFollowStream(true) .exec(new ResultCallback.Adapter() { @Override - public void onError(Throwable ignored) { - // Expected once the client is closed under the pending streams. + public void onError(Throwable failure) { + failures.add(failure); } }); } try (Socket first = accepted.poll(10, TimeUnit.SECONDS); - Socket second = accepted.poll(10, TimeUnit.SECONDS); - Socket third = accepted.poll(1, TimeUnit.SECONDS)) { + Socket second = accepted.poll(10, TimeUnit.SECONDS)) { assertNotNull(first, "first stream should connect"); assertNotNull(second, "second stream should connect"); - assertNull(third, "third stream should wait for a pooled connection instead of opening one"); + Throwable failure = failures.poll(10, TimeUnit.SECONDS); + assertNotNull(failure, "the third stream must reach the pool lease deadline"); + assertInstanceOf(ConnectionRequestTimeoutException.class, rootCause(failure)); + assertNull(accepted.poll(), + "third stream should wait for a pooled connection instead of opening one"); } } finally { client.close(); diff --git a/src/test/java/io/floci/aws/core/common/docker/DockerPoolGaugeLoggerTest.java b/src/test/java/io/floci/aws/core/common/docker/DockerPoolGaugeLoggerTest.java index 5f1c6ab366..cd3ebc07c2 100644 --- a/src/test/java/io/floci/aws/core/common/docker/DockerPoolGaugeLoggerTest.java +++ b/src/test/java/io/floci/aws/core/common/docker/DockerPoolGaugeLoggerTest.java @@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +@ResourceLock(ParallelLocks.DOCKER_ACTIVE_STREAMS) @ResourceLock(value = ParallelLocks.PORTS, mode = ResourceAccessMode.READ) class DockerPoolGaugeLoggerTest { @@ -104,6 +105,8 @@ class DockerPoolGaugeLoggerTest { }; DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder() .withDockerHost("tcp://127.0.0.1:2375").build(); + // Earlier clients can close before their callbacks retire; the lock prevents new producers. + awaitStreamCount(0, "earlier stream callbacks should retire"); int before = DockerPoolGaugeLogger.countStreamThreads(); try (DockerClient client = DockerClientProducer.newDockerClient(config, transport); @@ -127,10 +130,14 @@ class DockerPoolGaugeLoggerTest { release.countDown(); assertTrue(callback.awaitCompletion(5, TimeUnit.SECONDS)); } + awaitStreamCount(before, "an ended stream is no longer counted"); + } + + private static void awaitStreamCount(int expected, String message) throws InterruptedException { long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); - while (DockerPoolGaugeLogger.countStreamThreads() != before && System.nanoTime() < deadline) { - Thread.onSpinWait(); + while (DockerPoolGaugeLogger.countStreamThreads() != expected && System.nanoTime() < deadline) { + Thread.sleep(1); } - assertEquals(before, DockerPoolGaugeLogger.countStreamThreads(), "an ended stream is no longer counted"); + assertEquals(expected, DockerPoolGaugeLogger.countStreamThreads(), message); } } diff --git a/src/test/java/io/floci/aws/core/common/docker/DockerPoolSaturationNightlyIntegrationTest.java b/src/test/java/io/floci/aws/core/common/docker/DockerPoolSaturationNightlyIntegrationTest.java index e52ba40479..3006317109 100644 --- a/src/test/java/io/floci/aws/core/common/docker/DockerPoolSaturationNightlyIntegrationTest.java +++ b/src/test/java/io/floci/aws/core/common/docker/DockerPoolSaturationNightlyIntegrationTest.java @@ -22,6 +22,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.condition.EnabledIfSystemProperty; +import org.junit.jupiter.api.parallel.ResourceAccessMode; import org.junit.jupiter.api.parallel.ResourceLock; import java.io.Closeable; @@ -35,6 +36,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.function.Predicate; +import static io.floci.aws.core.common.docker.DockerTestSupport.rootCause; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -60,6 +62,7 @@ import static org.junit.jupiter.api.Assertions.fail; * three-minute bound fits the default on a hosted runner, so a much larger ramp needs it raised too. */ @EnabledIfSystemProperty(named = "nightly.checks.enabled", matches = "true") +@ResourceLock(value = ParallelLocks.DOCKER_ACTIVE_STREAMS, mode = ResourceAccessMode.READ) @ResourceLock(ParallelLocks.CONFIG_MAPPING) class DockerPoolSaturationNightlyIntegrationTest { @@ -271,14 +274,6 @@ class DockerPoolSaturationNightlyIntegrationTest { return sorted.get(sorted.size() / 2); } - private static Throwable rootCause(Throwable error) { - Throwable cause = error; - while (cause.getCause() != null && cause.getCause() != cause) { - cause = cause.getCause(); - } - return cause; - } - private void removeLeftovers() { for (Container container : control.listContainersCmd().withShowAll(true) .withLabelFilter(Map.of(LABEL, LABEL_VALUE)).exec()) { diff --git a/src/test/java/io/floci/aws/core/common/docker/DockerTestSupport.java b/src/test/java/io/floci/aws/core/common/docker/DockerTestSupport.java new file mode 100644 index 0000000000..53592cc200 --- /dev/null +++ b/src/test/java/io/floci/aws/core/common/docker/DockerTestSupport.java @@ -0,0 +1,16 @@ +package io.floci.aws.core.common.docker; + +final class DockerTestSupport { + + private DockerTestSupport() { + } + + static Throwable rootCause(Throwable error) { + Throwable cause = error; + while (cause.getCause() != null && cause.getCause() != cause) { + cause = cause.getCause(); + } + return cause; + } + +} diff --git a/src/test/java/io/floci/aws/core/common/docker/FlociInvocationBuilderTest.java b/src/test/java/io/floci/aws/core/common/docker/FlociInvocationBuilderTest.java index d9f2dc9c63..3ed4743aad 100644 --- a/src/test/java/io/floci/aws/core/common/docker/FlociInvocationBuilderTest.java +++ b/src/test/java/io/floci/aws/core/common/docker/FlociInvocationBuilderTest.java @@ -29,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; /** How a stream from {@link FlociInvocationBuilder#get()} gives its connection back when it is closed. */ +@ResourceLock(value = ParallelLocks.DOCKER_ACTIVE_STREAMS, mode = ResourceAccessMode.READ) @ResourceLock(value = ParallelLocks.PORTS, mode = ResourceAccessMode.READ) class FlociInvocationBuilderTest { diff --git a/src/test/java/io/floci/aws/core/common/docker/LocallyBuiltHelperImageDockerIntegrationTest.java b/src/test/java/io/floci/aws/core/common/docker/LocallyBuiltHelperImageDockerIntegrationTest.java index 7decf93a90..32313b1718 100644 --- a/src/test/java/io/floci/aws/core/common/docker/LocallyBuiltHelperImageDockerIntegrationTest.java +++ b/src/test/java/io/floci/aws/core/common/docker/LocallyBuiltHelperImageDockerIntegrationTest.java @@ -12,6 +12,7 @@ import io.floci.aws.testing.ParallelLocks; import io.smallrye.config.SmallRyeConfigBuilder; import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.ResourceAccessMode; import org.junit.jupiter.api.parallel.ResourceLock; import java.io.InputStream; @@ -28,6 +29,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** Builds and reuses a native helper when the configured Docker daemon answers. */ +@ResourceLock(value = ParallelLocks.DOCKER_ACTIVE_STREAMS, mode = ResourceAccessMode.READ) @ResourceLock(ParallelLocks.CONFIG_MAPPING) class LocallyBuiltHelperImageDockerIntegrationTest { private static final String BASE = "busybox:1.37.0"; diff --git a/src/test/java/io/floci/aws/services/lambda/launcher/WorkingDirectoryAccessDockerIntegrationTest.java b/src/test/java/io/floci/aws/services/lambda/launcher/WorkingDirectoryAccessDockerIntegrationTest.java index 0f5e931586..c73c280a24 100644 --- a/src/test/java/io/floci/aws/services/lambda/launcher/WorkingDirectoryAccessDockerIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/lambda/launcher/WorkingDirectoryAccessDockerIntegrationTest.java @@ -13,6 +13,7 @@ import io.floci.aws.testing.TestImages; import io.smallrye.config.SmallRyeConfigBuilder; import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.ResourceAccessMode; import org.junit.jupiter.api.parallel.ResourceLock; import java.io.ByteArrayInputStream; @@ -26,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; /** Reads the working directory's permissions from a created, unstarted container of a real image. */ +@ResourceLock(value = ParallelLocks.DOCKER_ACTIVE_STREAMS, mode = ResourceAccessMode.READ) @ResourceLock(ParallelLocks.CONFIG_MAPPING) class WorkingDirectoryAccessDockerIntegrationTest { diff --git a/src/test/java/io/floci/aws/testing/ParallelLocks.java b/src/test/java/io/floci/aws/testing/ParallelLocks.java index e47f34c7ec..fb7234cb77 100644 --- a/src/test/java/io/floci/aws/testing/ParallelLocks.java +++ b/src/test/java/io/floci/aws/testing/ParallelLocks.java @@ -30,6 +30,9 @@ public final class ParallelLocks { */ public static final String CONFIG_MAPPING = "floci-config-mapping"; + /** The process-wide count of active Floci Docker callbacks. Producers read-lock; exact observers write-lock. */ + public static final String DOCKER_ACTIVE_STREAMS = "floci-docker-active-streams"; + private ParallelLocks() { } } diff --git a/src/test/java/io/floci/aws/testing/ParallelLocksInventoryTest.java b/src/test/java/io/floci/aws/testing/ParallelLocksInventoryTest.java index 1df846913a..18f7f879f0 100644 --- a/src/test/java/io/floci/aws/testing/ParallelLocksInventoryTest.java +++ b/src/test/java/io/floci/aws/testing/ParallelLocksInventoryTest.java @@ -43,6 +43,26 @@ class ParallelLocksInventoryTest { private static final Pattern CONFIG_MAPPING_LOCK = Pattern.compile( "@ResourceLock\\(\\s*(?:value\\s*=\\s*)?ParallelLocks\\.CONFIG_MAPPING\\b"); + private static final Pattern USES_FLOCI_DOCKER_STREAMS = Pattern.compile( + "new DockerClientProducer\\s*\\(|DockerClientProducer\\.newDockerClient\\s*\\(" + + "|new FlociInvocationBuilder\\s*\\("); + private static final Pattern DOCKER_ACTIVE_STREAMS_LOCK = Pattern.compile( + "@ResourceLock\\(\\s*(?:value\\s*=\\s*)?ParallelLocks\\.DOCKER_ACTIVE_STREAMS\\b"); + + @Test + void everyPlainFlociDockerStreamConsumerDeclaresItsLock() { + Map sources = sources(); + Set missing = new TreeSet<>(); + sources.forEach((name, source) -> { + if (isTestClass(name) && USES_FLOCI_DOCKER_STREAMS.matcher(source).find() + && !isQuarkus(name, sources, new TreeSet<>()) && !DOCKER_ACTIVE_STREAMS_LOCK.matcher(source).find()) { + missing.add(name); + } + }); + assertTrue(missing.isEmpty(), "Plain Floci Docker stream consumers without " + + "@ResourceLock(ParallelLocks.DOCKER_ACTIVE_STREAMS): " + missing); + } + @Test void everyPlainClassThatOpensASocketDeclaresThePortsLock() { Map sources = sources();