mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 01:43:02 -05:00
test(docker): finish pool requests before checking stream gauge (#9468)
The Docker stream gauge test reads the process-wide active-stream counter, so the classes that open Floci Docker streams now take ParallelLocks.DOCKER_ACTIVE_STREAMS for reading and the gauge test takes it for writing, with an inventory check for new stream users. The gauge test waits for earlier callbacks to retire through one awaitStreamCount helper, the pool test waits for the third request's lease timeout instead of a second of silence, and both timeout tests share DockerTestSupport.rootCause.
This commit is contained in:
1 parent
4b7d57f016
commit
6906face9c
9 files changed
+70
-17
No files matched your search
@@ -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<Socket> accepted = new LinkedBlockingQueue<>();
|
||||
BlockingQueue<Throwable> 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<Frame>() {
|
||||
@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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+3
-8
@@ -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()) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
|
||||
+2
@@ -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";
|
||||
|
||||
+2
@@ -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 {
|
||||
|
||||
|
||||
@@ -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() {
|
||||
}
|
||||
}
|
||||
@@ -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<String, String> sources = sources();
|
||||
Set<String> 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<String, String> sources = sources();
|
||||
|
||||
Reference in new issue
Block a user