test: reuse parked-store assertions in service races (#8188)

This commit is contained in:
Jackson Price authored and GitHub committed 2026-11-12 23:37:29 -04:00
1 parent 315367f21f
commit 919f25ea0b
6 files changed
+33 -172

No files matched your search

@@ -9,16 +9,9 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
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.testing.ThreadStates.awaitBlockedOrDone;
import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
@@ -63,29 +56,9 @@ class AppConfigServiceTaggingTest {
Application app = service.createApplication(Map.of("Name", "orders"));
store.parkNextWrite();
ExecutorService pool = Executors.newFixedThreadPool(2);
try {
Future<?> tag = pool.submit(() -> service.tagApplication(app.getId(), Map.of("team", "payments")));
assertTrue(store.awaitParked(), "the tag write must reach the store");
AtomicReference<Thread> deleteThread = new AtomicReference<>();
Future<?> delete = pool.submit(() -> {
deleteThread.set(Thread.currentThread());
service.deleteApplication(app.getId());
});
awaitBlockedOrDone(deleteThread, delete);
assertFalse(delete.isDone(), "the delete ran while a tag write on the same application was mid-flight");
assertEquals(Thread.State.BLOCKED, deleteThread.get().getState(),
"the delete waits on the service's monitor");
store.release();
tag.get(2, TimeUnit.SECONDS);
delete.get(2, TimeUnit.SECONDS);
} catch (ExecutionException e) {
throw new AssertionError(e.getCause());
} finally {
store.release();
pool.shutdownNow();
}
assertOperationWaitsForParkedStore(store,
() -> service.tagApplication(app.getId(), Map.of("team", "payments")),
() -> service.deleteApplication(app.getId()));
assertTrue(service.listApplications().isEmpty(), "the application outlived its delete");
}
@@ -11,7 +11,6 @@ import io.floci.aws.services.backup.model.BackupPlan;
import io.floci.aws.services.backup.model.BackupSelection;
import io.floci.aws.services.sns.SnsService;
import io.floci.aws.testing.ParkingStore;
import io.floci.aws.testing.ThreadStates;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
@@ -20,12 +19,8 @@ import org.junit.jupiter.params.provider.ValueSource;
import java.nio.file.Path;
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.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@@ -107,39 +102,24 @@ class BackupDeletedPlanServiceTest {
AccountAwareStorageBackend<BackupSelection> selections =
new AccountAwareStorageBackend<>(innerSelections, null, ACCOUNT);
BackupService service = service(storage(plans, selections), config());
ExecutorService workers = Executors.newFixedThreadPool(2);
try {
BackupPlan plan = service.createBackupPlan("ordered", List.of(), null, REGION);
Runnable delete = () -> service.deleteBackupPlan(plan.getBackupPlanId());
Runnable select = () -> service.createBackupSelection(plan.getBackupPlanId(), "selection", "role",
List.of("resource"), List.of(), null);
if (deleteFirst) {
innerPlans.parkNextWrite();
} else {
innerSelections.parkNextWrite();
}
Future<?> first = workers.submit(deleteFirst ? delete : select);
assertTrue(deleteFirst ? innerPlans.awaitParked() : innerSelections.awaitParked());
AtomicReference<Thread> competing = new AtomicReference<>();
Future<AwsException> second = workers.submit(() -> {
competing.set(Thread.currentThread());
return assertThrows(AwsException.class, (deleteFirst ? select : delete)::run);
ParkingStore<?> parkedStore = deleteFirst ? innerPlans : innerSelections;
Runnable first = deleteFirst ? delete : select;
Runnable competing = deleteFirst ? select : delete;
String expectedError = deleteFirst ? "ResourceNotFoundException" : "InvalidRequestException";
parkedStore.parkNextWrite();
assertOperationWaitsForParkedStore(parkedStore, first, () -> {
AwsException failure = assertThrows(AwsException.class, competing::run);
assertEquals(expectedError, failure.getErrorCode());
});
ThreadStates.awaitBlockedOrDone(competing, second);
assertFalse(second.isDone(), "competing mutation must wait for the same plan lock");
innerPlans.release();
innerSelections.release();
first.get(5, TimeUnit.SECONDS);
assertEquals(deleteFirst ? "ResourceNotFoundException" : "InvalidRequestException",
second.get(5, TimeUnit.SECONDS).getErrorCode());
assertEquals(deleteFirst ? 0 : 1, service.listBackupSelections(plan.getBackupPlanId()).size());
assertEquals(deleteFirst ? 0 : 1, service.listBackupPlans().size());
assertEquals(1, service.listBackupPlans(REGION, true, null, null).items().size());
} finally {
innerPlans.release();
innerSelections.release();
workers.shutdownNow();
assertTrue(workers.awaitTermination(5, TimeUnit.SECONDS));
service.shutdown();
}
}
@@ -12,23 +12,19 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
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 java.util.stream.Collectors;
import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone;
import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -240,24 +236,9 @@ class AwsConfigServiceAggregationAuthorizationTest {
service.tagResource(REGION, arn, List.of(Map.of("Key", "env", "Value", "prod")));
packs.parkNextWrite();
ExecutorService pool = Executors.newFixedThreadPool(2);
try {
Future<?> put = pool.submit(() -> service.putConformancePack(REGION, "pack", "s3://bucket/v2.yaml", null));
assertTrue(packs.awaitParked(), "the update must reach its persist");
AtomicReference<Thread> deleteThread = new AtomicReference<>();
Future<?> delete = pool.submit(() -> {
deleteThread.set(Thread.currentThread());
service.deleteConformancePack(REGION, "pack");
});
awaitBlockedOrDone(deleteThread, delete);
assertFalse(delete.isDone(), "the delete ran while an update of the same pack was mid-write");
packs.release();
put.get(2, TimeUnit.SECONDS);
delete.get(2, TimeUnit.SECONDS);
} finally {
pool.shutdownNow();
}
assertOperationWaitsForParkedStore(packs,
() -> service.putConformancePack(REGION, "pack", "s3://bucket/v2.yaml", null),
() -> service.deleteConformancePack(REGION, "pack"));
assertTrue(service.describeConformancePacks(REGION, null).isEmpty(),
"a Delete that succeeded left the pack it deleted in the store");
@@ -15,17 +15,10 @@ import org.junit.jupiter.api.function.Executable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
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.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone;
import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -296,27 +289,9 @@ class AwsConfigServiceTaggingTest {
String arn = service.putConformancePack(REGION, "pack", "s3://bucket/t.yaml", null).conformancePackArn();
tagStore.parkNextWrite();
ExecutorService pool = Executors.newFixedThreadPool(2);
try {
Future<?> tag = pool.submit(() -> service.tagResource(REGION, arn, ENV_PROD));
assertTrue(tagStore.awaitParked(), "the tag write must reach its persist");
AtomicReference<Thread> deleteThread = new AtomicReference<>();
Future<?> delete = pool.submit(() -> {
deleteThread.set(Thread.currentThread());
service.deleteConformancePack(REGION, "pack");
});
awaitBlockedOrDone(deleteThread, delete);
assertFalse(delete.isDone(), "the delete ran while a tag write on the same pack was mid-flight");
tagStore.release();
tag.get(2, TimeUnit.SECONDS);
delete.get(2, TimeUnit.SECONDS);
} catch (ExecutionException e) {
throw new AssertionError(e.getCause());
} finally {
tagStore.release();
pool.shutdownNow();
}
assertOperationWaitsForParkedStore(tagStore,
() -> service.tagResource(REGION, arn, ENV_PROD),
() -> service.deleteConformancePack(REGION, "pack"));
assertTrue(service.storedTags(arn).isEmpty(), "a tag entry outlived the pack it was written for");
}
@@ -10,16 +10,9 @@ import org.junit.jupiter.api.Test;
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.testing.ThreadStates.awaitBlockedOrDone;
import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -37,24 +30,10 @@ class Ec2DefaultResourcesSeedTest {
Ec2Service service = newService(subnets);
// Holds the first call in the middle of seeding, before any default subnet is stored.
subnets.parkNextWrite();
ExecutorService pool = Executors.newFixedThreadPool(2);
try {
Future<?> first = pool.submit(() -> service.ensureDefaultResources(REGION));
assertTrue(subnets.awaitParked(), "the first call must reach its first subnet write");
AtomicReference<Thread> secondThread = new AtomicReference<>();
Future<Integer> second = pool.submit(() -> {
secondThread.set(Thread.currentThread());
return service.describeSubnets(REGION, List.of(), Map.of()).size();
});
awaitBlockedOrDone(secondThread, second);
assertFalse(second.isDone(), "the second call read the default network while it was still being seeded");
subnets.release();
first.get(2, TimeUnit.SECONDS);
assertEquals(3, second.get(2, TimeUnit.SECONDS), "the second call sees the three default subnets");
} finally {
pool.shutdownNow();
}
assertOperationWaitsForParkedStore(subnets,
() -> service.ensureDefaultResources(REGION),
() -> assertEquals(3, service.describeSubnets(REGION, List.of(), Map.of()).size(),
"the second call sees the three default subnets"));
}
private Ec2Service newService(StorageBackend<String, Subnet> subnets) {
@@ -21,6 +21,7 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore;
import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -100,23 +101,9 @@ class SnsPolicyAndSandboxEditsTest {
// The verification parks right after reading the number, before writing it back verified.
sandbox.parkNextRead();
ExecutorService pool = Executors.newFixedThreadPool(2);
try {
Future<?> verify = pool.submit(() -> service.verifySmsSandboxPhoneNumber(PHONE_NUMBER, code, REGION));
assertTrue(sandbox.awaitParked(), "the verification must reach its read of the number");
AtomicReference<Thread> deleteThread = new AtomicReference<>();
Future<?> delete = pool.submit(() -> {
deleteThread.set(Thread.currentThread());
service.deleteSmsSandboxPhoneNumber(PHONE_NUMBER, REGION);
});
awaitBlockedOrDone(deleteThread, delete);
sandbox.release();
verify.get(2, TimeUnit.SECONDS);
delete.get(2, TimeUnit.SECONDS);
} finally {
pool.shutdownNow();
}
assertOperationWaitsForParkedStore(sandbox,
() -> service.verifySmsSandboxPhoneNumber(PHONE_NUMBER, code, REGION),
() -> service.deleteSmsSandboxPhoneNumber(PHONE_NUMBER, REGION));
assertTrue(service.listSmsSandboxPhoneNumbers(null, null, REGION).items().isEmpty(),
"a number deleted while it was being verified came back");
@@ -130,23 +117,9 @@ class SnsPolicyAndSandboxEditsTest {
// The resend parks right after reading the number, before storing its fresh code.
sandbox.parkNextRead();
ExecutorService pool = Executors.newFixedThreadPool(2);
try {
Future<?> resend = pool.submit(() -> service.createSmsSandboxPhoneNumber(PHONE_NUMBER, null, REGION));
assertTrue(sandbox.awaitParked(), "the resend must reach its read of the number");
AtomicReference<Thread> deleteThread = new AtomicReference<>();
Future<?> delete = pool.submit(() -> {
deleteThread.set(Thread.currentThread());
service.deleteSmsSandboxPhoneNumber(PHONE_NUMBER, REGION);
});
awaitBlockedOrDone(deleteThread, delete);
sandbox.release();
resend.get(2, TimeUnit.SECONDS);
delete.get(2, TimeUnit.SECONDS);
} finally {
pool.shutdownNow();
}
assertOperationWaitsForParkedStore(sandbox,
() -> service.createSmsSandboxPhoneNumber(PHONE_NUMBER, null, REGION),
() -> service.deleteSmsSandboxPhoneNumber(PHONE_NUMBER, REGION));
assertTrue(service.listSmsSandboxPhoneNumbers(null, null, REGION).items().isEmpty(),
"a number deleted while a code was being resent came back");