From d05710e1d939da91a2fe8f83976f8b4782bf81a3 Mon Sep 17 00:00:00 2001 From: Jackson Price <59149601+jprice8@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:40:17 -0600 Subject: [PATCH] test: require confirmed parked-store waits in service races (#8193) Reject an unfinished worker that never reaches a blocked or waiting state. Cover the observation timeout with an interruptible spinning worker. Reuse the assertion in endpoint, runtime-tag and repository-template tests while retaining each case's resource and error checks. Remove the unused AgentCore wait helper and ECR timeout-only checks. Reuse the existing worker-state assertion with a configurable observation window. Keep the five-second default for callers and use 20 ms for the unblocked-worker regression. Also reject a completed competing operation. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> --- .../BedrockAgentCoreConcurrencyFixture.java | 16 ---- ...drockAgentCoreEndpointConcurrencyTest.java | 78 ++++----------- ...ockAgentCoreRuntimeTagConcurrencyTest.java | 96 ++++--------------- .../aws/services/ecr/EcrServiceTest.java | 49 +++------- .../aws/testing/ParkingStoreAssertions.java | 18 +++- .../testing/ParkingStoreAssertionsTest.java | 45 +++++++++ 6 files changed, 110 insertions(+), 192 deletions(-) create mode 100644 src/test/java/io/floci/aws/testing/ParkingStoreAssertionsTest.java diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java index 756c6a0079..de182351bf 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java @@ -5,14 +5,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.services.bedrockagentcorecontrol.model.AgentRuntime; import io.floci.aws.testing.ParkingStore; -import io.floci.aws.testing.ThreadStates; - -import java.util.concurrent.Future; -import java.util.concurrent.atomic.AtomicReference; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; final class BedrockAgentCoreConcurrencyFixture { @@ -38,12 +30,4 @@ final class BedrockAgentCoreConcurrencyFixture { AgentRuntime delete() { return service.deleteAgentRuntime(runtimeId, null, REGION); } - - static void assertBlocked(AtomicReference worker, Future task) throws InterruptedException { - ThreadStates.awaitBlockedOrDone(worker, task); - assertNotNull(worker.get(), "the competing worker must start"); - assertEquals(Thread.State.BLOCKED, worker.get().getState(), - "the competing worker must overlap the parked storage operation"); - assertFalse(task.isDone()); - } } diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointConcurrencyTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointConcurrencyTest.java index 03e039b178..26ce722fff 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointConcurrencyTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointConcurrencyTest.java @@ -1,95 +1,53 @@ package io.floci.aws.services.bedrockagentcorecontrol; import io.floci.aws.core.common.AwsException; -import io.floci.aws.services.bedrockagentcorecontrol.model.AgentRuntime; import io.floci.aws.services.bedrockagentcorecontrol.model.AgentRuntimeEndpoint; import org.junit.jupiter.api.Test; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import static io.floci.aws.services.bedrockagentcorecontrol.BedrockAgentCoreConcurrencyFixture.REGION; -import static io.floci.aws.services.bedrockagentcorecontrol.BedrockAgentCoreConcurrencyFixture.assertBlocked; +import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; class BedrockAgentCoreEndpointConcurrencyTest { @Test void concurrentClientTokenReplayWaitsForPublication() throws Exception { BedrockAgentCoreConcurrencyFixture fixture = new BedrockAgentCoreConcurrencyFixture(); + AtomicReference first = new AtomicReference<>(); + AtomicReference second = new AtomicReference<>(); fixture.storage.parkNextWrite(); - try (ExecutorService executor = Executors.newFixedThreadPool(2)) { - Future first = executor.submit(() -> createEndpoint(fixture, "token")); - try { - assertTrue(fixture.storage.awaitParked()); - AtomicReference secondThread = new AtomicReference<>(); - Future second = executor.submit(() -> { - secondThread.set(Thread.currentThread()); - return createEndpoint(fixture, "token"); - }); - assertBlocked(secondThread, second); - fixture.storage.release(); - assertSame(first.get(5, TimeUnit.SECONDS), second.get(5, TimeUnit.SECONDS)); - assertEquals(1, namedEndpointCount(fixture)); - } finally { - fixture.storage.release(); - } - } + assertOperationWaitsForParkedStore(fixture.storage, + () -> first.set(createEndpoint(fixture, "token")), + () -> second.set(createEndpoint(fixture, "token"))); + assertSame(first.get(), second.get()); + assertEquals(1, namedEndpointCount(fixture)); } @Test void concurrentSameNameGetsConflictAfterPublication() throws Exception { BedrockAgentCoreConcurrencyFixture fixture = new BedrockAgentCoreConcurrencyFixture(); fixture.storage.parkNextWrite(); - try (ExecutorService executor = Executors.newFixedThreadPool(2)) { - Future first = executor.submit(() -> createEndpoint(fixture, null)); - try { - assertTrue(fixture.storage.awaitParked()); - AtomicReference secondThread = new AtomicReference<>(); - Future second = executor.submit(() -> { - secondThread.set(Thread.currentThread()); - return assertThrows(AwsException.class, () -> createEndpoint(fixture, null)); + assertOperationWaitsForParkedStore(fixture.storage, + () -> createEndpoint(fixture, null), + () -> { + AwsException failure = assertThrows(AwsException.class, () -> createEndpoint(fixture, null)); + assertEquals("ConflictException", failure.getErrorCode()); }); - assertBlocked(secondThread, second); - fixture.storage.release(); - first.get(5, TimeUnit.SECONDS); - assertEquals("ConflictException", second.get(5, TimeUnit.SECONDS).getErrorCode()); - assertEquals(1, namedEndpointCount(fixture)); - } finally { - fixture.storage.release(); - } - } + assertEquals(1, namedEndpointCount(fixture)); } @Test void deletingRuntimeCannotBeUndoneByEndpointPublication() throws Exception { BedrockAgentCoreConcurrencyFixture fixture = new BedrockAgentCoreConcurrencyFixture(); fixture.storage.parkNextWrite(); - try (ExecutorService executor = Executors.newFixedThreadPool(2)) { - Future first = executor.submit(() -> createEndpoint(fixture, null)); - try { - assertTrue(fixture.storage.awaitParked()); - AtomicReference deleteThread = new AtomicReference<>(); - Future deletion = executor.submit(() -> { - deleteThread.set(Thread.currentThread()); - return fixture.delete(); - }); - assertBlocked(deleteThread, deletion); - fixture.storage.release(); - first.get(5, TimeUnit.SECONDS); - deletion.get(5, TimeUnit.SECONDS); - assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, - () -> fixture.service.getAgentRuntime(fixture.runtimeId, REGION)).getErrorCode()); - } finally { - fixture.storage.release(); - } - } + assertOperationWaitsForParkedStore(fixture.storage, + () -> createEndpoint(fixture, null), fixture::delete); + assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, + () -> fixture.service.getAgentRuntime(fixture.runtimeId, REGION)).getErrorCode()); } private static AgentRuntimeEndpoint createEndpoint(BedrockAgentCoreConcurrencyFixture fixture, diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreRuntimeTagConcurrencyTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreRuntimeTagConcurrencyTest.java index 2e063814cc..f074cbeec3 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreRuntimeTagConcurrencyTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreRuntimeTagConcurrencyTest.java @@ -1,24 +1,17 @@ package io.floci.aws.services.bedrockagentcorecontrol; import io.floci.aws.core.common.AwsException; -import io.floci.aws.services.bedrockagentcorecontrol.model.AgentRuntime; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import java.util.List; import java.util.Map; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; import static io.floci.aws.services.bedrockagentcorecontrol.BedrockAgentCoreConcurrencyFixture.REGION; -import static io.floci.aws.services.bedrockagentcorecontrol.BedrockAgentCoreConcurrencyFixture.assertBlocked; +import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; class BedrockAgentCoreRuntimeTagConcurrencyTest { @@ -27,24 +20,9 @@ class BedrockAgentCoreRuntimeTagConcurrencyTest { void deletionWaitsForTagPublication(boolean remove) throws Exception { BedrockAgentCoreConcurrencyFixture fixture = taggedRuntime(); fixture.storage.parkNextWrite(); - try (ExecutorService executor = Executors.newFixedThreadPool(2)) { - Future tagging = executor.submit(() -> changeTags(fixture, remove)); - try { - assertTrue(fixture.storage.awaitParked()); - AtomicReference deleteThread = new AtomicReference<>(); - Future deletion = executor.submit(() -> { - deleteThread.set(Thread.currentThread()); - return fixture.delete(); - }); - assertBlocked(deleteThread, deletion); - fixture.storage.release(); - tagging.get(5, TimeUnit.SECONDS); - deletion.get(5, TimeUnit.SECONDS); - assertAbsent(fixture); - } finally { - fixture.storage.release(); - } - } + assertOperationWaitsForParkedStore(fixture.storage, + () -> changeTags(fixture, remove), fixture::delete); + assertAbsent(fixture); } @ParameterizedTest @@ -52,24 +30,12 @@ class BedrockAgentCoreRuntimeTagConcurrencyTest { void tagWriteRechecksExistenceAfterDeletion(boolean remove) throws Exception { BedrockAgentCoreConcurrencyFixture fixture = taggedRuntime(); fixture.storage.parkNextRead(); - try (ExecutorService executor = Executors.newFixedThreadPool(2)) { - Future deletion = executor.submit(fixture::delete); - try { - assertTrue(fixture.storage.awaitParked()); - AtomicReference tagThread = new AtomicReference<>(); - Future tagging = executor.submit(() -> { - tagThread.set(Thread.currentThread()); - return assertThrows(AwsException.class, () -> changeTags(fixture, remove)); + assertOperationWaitsForParkedStore(fixture.storage, fixture::delete, + () -> { + AwsException failure = assertThrows(AwsException.class, () -> changeTags(fixture, remove)); + assertEquals("ResourceNotFoundException", failure.getErrorCode()); }); - assertBlocked(tagThread, tagging); - fixture.storage.release(); - deletion.get(5, TimeUnit.SECONDS); - assertEquals("ResourceNotFoundException", tagging.get(5, TimeUnit.SECONDS).getErrorCode()); - assertAbsent(fixture); - } finally { - fixture.storage.release(); - } - } + assertAbsent(fixture); } @ParameterizedTest @@ -89,50 +55,24 @@ class BedrockAgentCoreRuntimeTagConcurrencyTest { @ValueSource(booleans = {false, true}) void tagReadWaitsForTagPublication(boolean remove) throws Exception { BedrockAgentCoreConcurrencyFixture fixture = taggedRuntime(); + Map expected = remove ? Map.of("other", "retained") : Map.of("key", "new", "other", "retained"); fixture.storage.parkNextWrite(); - try (ExecutorService executor = Executors.newFixedThreadPool(2)) { - Future tagging = executor.submit(() -> changeTags(fixture, remove)); - try { - assertTrue(fixture.storage.awaitParked()); - AtomicReference readThread = new AtomicReference<>(); - Future> reading = executor.submit(() -> { - readThread.set(Thread.currentThread()); - return fixture.service.getTagsByArn(REGION, fixture.arn); - }); - assertBlocked(readThread, reading); - fixture.storage.release(); - tagging.get(5, TimeUnit.SECONDS); - assertEquals(remove ? Map.of("other", "retained") : Map.of("key", "new", "other", "retained"), - reading.get(5, TimeUnit.SECONDS)); - } finally { - fixture.storage.release(); - } - } + assertOperationWaitsForParkedStore(fixture.storage, + () -> changeTags(fixture, remove), + () -> assertEquals(expected, fixture.service.getTagsByArn(REGION, fixture.arn))); } @Test void tagReadRechecksExistenceAfterDeletion() throws Exception { BedrockAgentCoreConcurrencyFixture fixture = taggedRuntime(); fixture.storage.parkNextRead(); - try (ExecutorService executor = Executors.newFixedThreadPool(2)) { - Future deletion = executor.submit(fixture::delete); - try { - assertTrue(fixture.storage.awaitParked()); - AtomicReference readThread = new AtomicReference<>(); - Future reading = executor.submit(() -> { - readThread.set(Thread.currentThread()); - return assertThrows(AwsException.class, + assertOperationWaitsForParkedStore(fixture.storage, fixture::delete, + () -> { + AwsException failure = assertThrows(AwsException.class, () -> fixture.service.getTagsByArn(REGION, fixture.arn)); + assertEquals("ResourceNotFoundException", failure.getErrorCode()); }); - assertBlocked(readThread, reading); - fixture.storage.release(); - deletion.get(5, TimeUnit.SECONDS); - assertEquals("ResourceNotFoundException", reading.get(5, TimeUnit.SECONDS).getErrorCode()); - assertAbsent(fixture); - } finally { - fixture.storage.release(); - } - } + assertAbsent(fixture); } @Test diff --git a/src/test/java/io/floci/aws/services/ecr/EcrServiceTest.java b/src/test/java/io/floci/aws/services/ecr/EcrServiceTest.java index 6fe6bb4e59..76d2fa9347 100644 --- a/src/test/java/io/floci/aws/services/ecr/EcrServiceTest.java +++ b/src/test/java/io/floci/aws/services/ecr/EcrServiceTest.java @@ -46,10 +46,10 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -2347,25 +2347,14 @@ class EcrServiceTest { void creationTemplate_aSecondCreateOfThePrefixWaitsAndIsRefused() throws Exception { ParkingStore store = new ParkingStore<>(); EcrService templates = templateService(store); - ExecutorService pool = Executors.newFixedThreadPool(2); - try { - store.parkNextRead(); - Future first = pool.submit(() -> templates.createRepositoryCreationTemplate( - template("prod", "REPLICATION"), REGION)); - assertTrue(store.awaitParked()); - Future second = pool.submit(() -> templates.createRepositoryCreationTemplate( - template("prod", "REPLICATION"), REGION)); - assertThrows(TimeoutException.class, () -> second.get(200, TimeUnit.MILLISECONDS), - "the second create must wait for the first"); - - store.release(); - first.get(5, TimeUnit.SECONDS); - ExecutionException refused = assertThrows(ExecutionException.class, () -> second.get(5, TimeUnit.SECONDS)); - assertEquals("TemplateAlreadyExistsException", ((AwsException) refused.getCause()).getErrorCode()); - } finally { - store.release(); - pool.shutdownNow(); - } + store.parkNextRead(); + assertOperationWaitsForParkedStore(store, + () -> templates.createRepositoryCreationTemplate(template("prod", "REPLICATION"), REGION), + () -> { + AwsException failure = assertThrows(AwsException.class, + () -> templates.createRepositoryCreationTemplate(template("prod", "REPLICATION"), REGION)); + assertEquals("TemplateAlreadyExistsException", failure.getErrorCode()); + }); } @Test @@ -2375,22 +2364,10 @@ class EcrServiceTest { templates.createRepositoryCreationTemplate(template("prod", "REPLICATION"), REGION); RepositoryCreationTemplate change = template("prod"); change.setDescription("changed"); - ExecutorService pool = Executors.newFixedThreadPool(2); - try { - store.parkNextRead(); - Future update = pool.submit(() -> templates.updateRepositoryCreationTemplate(change, REGION)); - assertTrue(store.awaitParked()); - Future delete = pool.submit(() -> templates.deleteRepositoryCreationTemplate("prod", REGION)); - assertThrows(TimeoutException.class, () -> delete.get(200, TimeUnit.MILLISECONDS), - "the delete must wait for the update"); - - store.release(); - update.get(5, TimeUnit.SECONDS); - delete.get(5, TimeUnit.SECONDS); - } finally { - store.release(); - pool.shutdownNow(); - } + store.parkNextRead(); + assertOperationWaitsForParkedStore(store, + () -> templates.updateRepositoryCreationTemplate(change, REGION), + () -> templates.deleteRepositoryCreationTemplate("prod", REGION)); assertTrue(templates.describeRepositoryCreationTemplates(null, null, null, REGION).items().isEmpty()); } diff --git a/src/test/java/io/floci/aws/testing/ParkingStoreAssertions.java b/src/test/java/io/floci/aws/testing/ParkingStoreAssertions.java index fcdf73115b..c815a6b519 100644 --- a/src/test/java/io/floci/aws/testing/ParkingStoreAssertions.java +++ b/src/test/java/io/floci/aws/testing/ParkingStoreAssertions.java @@ -1,5 +1,8 @@ package io.floci.aws.testing; +import java.time.Duration; +import java.util.EnumSet; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -8,7 +11,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicReference; -import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone; +import static io.floci.aws.testing.ThreadStates.awaitState; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -25,16 +28,27 @@ public final class ParkingStoreAssertions { public static void assertOperationWaitsForParkedStore(ParkingStore store, Runnable parkedOperation, Runnable waitingOperation) throws InterruptedException, ExecutionException, TimeoutException { + assertOperationWaitsForParkedStore(store, parkedOperation, waitingOperation, Duration.ofSeconds(5)); + } + + /** Overrides the worker-state observation window; startup, result and cleanup bounds stay unchanged. */ + static void assertOperationWaitsForParkedStore(ParkingStore store, Runnable parkedOperation, + Runnable waitingOperation, Duration blockingTimeout) + throws InterruptedException, ExecutionException, TimeoutException { ExecutorService executor = Executors.newFixedThreadPool(2); try { Future parked = executor.submit(parkedOperation); assertTrue(store.awaitParked(), "first operation reached the armed store"); AtomicReference waitingThread = new AtomicReference<>(); + CountDownLatch waitingStarted = new CountDownLatch(1); Future waiting = executor.submit(() -> { waitingThread.set(Thread.currentThread()); + waitingStarted.countDown(); waitingOperation.run(); }); - awaitBlockedOrDone(waitingThread, waiting); + assertTrue(waitingStarted.await(5, TimeUnit.SECONDS), "second operation started"); + awaitState(waitingThread.get(), EnumSet.of(Thread.State.BLOCKED, Thread.State.WAITING), + blockingTimeout, "second operation"); assertFalse(waiting.isDone(), "second operation must wait for the parked operation"); store.release(); parked.get(5, TimeUnit.SECONDS); diff --git a/src/test/java/io/floci/aws/testing/ParkingStoreAssertionsTest.java b/src/test/java/io/floci/aws/testing/ParkingStoreAssertionsTest.java new file mode 100644 index 0000000000..6fc4b69993 --- /dev/null +++ b/src/test/java/io/floci/aws/testing/ParkingStoreAssertionsTest.java @@ -0,0 +1,45 @@ +package io.floci.aws.testing; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import java.time.Duration; + +import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ParkingStoreAssertionsTest { + + @Test + void rejectsAnOperationThatFinishesBeforeTheStoreIsReleased() { + ParkingStore store = new ParkingStore<>(); + store.parkNextWrite(); + + AssertionError failure = assertThrows(AssertionError.class, + () -> assertOperationWaitsForParkedStore(store, + () -> store.put("key", "value"), () -> { })); + + assertTrue(failure.getMessage().startsWith( + "second operation must wait for the parked operation")); + } + + @Test + @Timeout(20) + void rejectsAnOperationThatNeverBlocks() { + ParkingStore store = new ParkingStore<>(); + store.parkNextWrite(); + + AssertionError failure = assertThrows(AssertionError.class, + () -> assertOperationWaitsForParkedStore(store, + () -> store.put("key", "value"), + () -> { + while (!Thread.currentThread().isInterrupted()) { + Thread.onSpinWait(); + } + }, Duration.ofMillis(20))); + + assertTrue(failure.getMessage().startsWith( + "second operation never reached [BLOCKED, WAITING]")); + } +}