test: share monitor waits across EKS, Lambda and ECS tests (#10127)

Use the shared thread-state poller for layer policy calls and task
startup. Keep the held monitor and fail if the worker exits before
blocking.

Move the EKS state check into ThreadStates and pass each caller's
timeout through to the shared poller.

Signed-off-by: Jackson Price <jxnprice@amazon.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-12-04 07:00:01 -04:00
1 parent d08b578474
commit f6de4caa5e
4 files changed
+22 -27

No files matched your search

@@ -14,6 +14,7 @@ import io.floci.aws.services.ecs.model.ContainerDefinition;
import io.floci.aws.services.ecs.model.EcsTask;
import io.floci.aws.services.ecs.model.LaunchType;
import io.floci.aws.testing.ParallelLocks;
import io.floci.aws.testing.ThreadStates;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceAccessMode;
import org.junit.jupiter.api.parallel.ResourceLock;
@@ -291,12 +292,8 @@ class EcsServiceTeardownTest {
Thread starter = startingThread.get();
synchronized (startingTask.get()) {
finishStart.countDown();
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (starter.getState() != Thread.State.BLOCKED) {
assertTrue(starter.isAlive(), "startup finished without acquiring the task lock");
assertTrue(System.nanoTime() < deadline, "startup did not wait for the task lock");
Thread.onSpinWait();
}
Thread.State state = ThreadStates.awaitBlockedOrTerminated(starter, Duration.ofSeconds(5), "task startup");
assertEquals(Thread.State.BLOCKED, state, "startup finished without waiting for the task lock");
}
starter.join(5000);
assertFalse(starter.isAlive());
@@ -83,7 +83,6 @@ import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Base64;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@@ -102,6 +101,7 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import static io.floci.aws.testing.ThreadStates.awaitBlockedOrTerminated;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
@@ -2033,7 +2033,8 @@ class EksServiceTest {
assertTrue(stopping.await(10, TimeUnit.SECONDS));
exiting.start();
assertEquals(Thread.State.BLOCKED, awaitBlockedOrTerminated(exiting, "the exit waits for the delete"));
assertEquals(Thread.State.BLOCKED,
awaitBlockedOrTerminated(exiting, Duration.ofSeconds(5), "the exit waits for the delete"));
} finally {
release.countDown();
deleter.join(10_000);
@@ -2147,7 +2148,8 @@ class EksServiceTest {
Cluster swapping = invocation.getArgument(0);
swapping.setContainerId(null);
deleter.start();
deleterDuringSwap.set(awaitBlockedOrTerminated(deleter, "the delete during the swap"));
deleterDuringSwap.set(awaitBlockedOrTerminated(deleter, Duration.ofSeconds(5),
"the delete during the swap"));
swapping.setContainerId("replacement-container");
return true;
});
@@ -2202,7 +2204,8 @@ class EksServiceTest {
doAnswer(invocation -> {
deleter.start();
assertEquals(Thread.State.BLOCKED,
awaitBlockedOrTerminated(deleter, "the delete during launch-template node group storage"));
awaitBlockedOrTerminated(deleter, Duration.ofSeconds(5),
"the delete during launch-template node group storage"));
return null;
}).when(autoScaling).createEksManagedGroup(anyString(), anyString(), anyInt(), anyInt(), anyInt(), any(), any());
doAnswer(invocation -> {
@@ -2260,7 +2263,7 @@ class EksServiceTest {
first.start();
assertTrue(firstStopping.await(5, TimeUnit.SECONDS));
second.start();
Thread.State secondWhileFirstRuns = awaitBlockedOrTerminated(second, "the second delete");
Thread.State secondWhileFirstRuns = awaitBlockedOrTerminated(second, Duration.ofSeconds(5), "the second delete");
releaseFirst.countDown();
first.join(5000);
second.join(5000);
@@ -2286,12 +2289,6 @@ class EksServiceTest {
});
}
// BLOCKED on any monitor counts; the assertions after it show the wait was on the capacity lock.
private static Thread.State awaitBlockedOrTerminated(Thread thread, String what) {
return ThreadStates.awaitState(thread, EnumSet.of(Thread.State.BLOCKED, Thread.State.TERMINATED),
Duration.ofSeconds(5), what);
}
@Test
void firstNodeGroupSwapOnALiveClusterStoresBoth() {
EksClusterManager clusterManager = capacitySwapManager(true);
@@ -5,9 +5,11 @@ import io.floci.aws.core.common.JacksonConfig;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.services.lambda.model.LambdaLayerVersion;
import io.floci.aws.testing.ThreadStates;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
@@ -17,7 +19,6 @@ import static io.floci.aws.services.lambda.LambdaLayerFixtures.layer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
* A layer version's deletion and the changes and reads of its policy hold one lock: a permission waiting while its
@@ -107,14 +108,9 @@ class LambdaLayerPolicyServiceTest {
}
}
/** Waits until the thread is waiting to take a lock, failing if it ends or ten seconds pass first. */
private static void awaitBlocked(Thread thread) {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
while (thread.getState() != Thread.State.BLOCKED) {
if (!thread.isAlive() || System.nanoTime() > deadline) {
fail("the call did not wait for the layer version's lock (" + thread.getState() + ")");
}
Thread.onSpinWait();
}
private static void awaitBlocked(Thread worker) {
Thread.State state = ThreadStates.awaitBlockedOrTerminated(worker, Duration.ofSeconds(10),
"the layer version call");
assertEquals(Thread.State.BLOCKED, state, "the call finished without waiting for the layer version lock");
}
}
@@ -87,6 +87,11 @@ public final class ThreadStates {
return awaitState(thread, PARKED_OR_DONE, Duration.ofSeconds(10), "the thread");
}
/** Returns the observed state so a caller can distinguish monitor contention from a worker that exited. */
public static Thread.State awaitBlockedOrTerminated(Thread thread, Duration timeout, String what) {
return awaitState(thread, EnumSet.of(Thread.State.BLOCKED, Thread.State.TERMINATED), timeout, what);
}
/** Waits for a worker to block entering a monitor in the given method. Keep the monitor held until this returns. */
public static void awaitBlockedIn(Thread worker, Class<?> declaringClass, String method, Duration timeout) {
String location = declaringClass.getName() + "." + method;