mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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>
This commit is contained in:
1 parent
d3c8412977
commit
d05710e1d9
6 files changed
+110
-192
No files matched your search
-16
@@ -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<Thread> 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());
|
||||
}
|
||||
}
|
||||
+18
-60
@@ -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<AgentRuntimeEndpoint> first = new AtomicReference<>();
|
||||
AtomicReference<AgentRuntimeEndpoint> second = new AtomicReference<>();
|
||||
fixture.storage.parkNextWrite();
|
||||
try (ExecutorService executor = Executors.newFixedThreadPool(2)) {
|
||||
Future<AgentRuntimeEndpoint> first = executor.submit(() -> createEndpoint(fixture, "token"));
|
||||
try {
|
||||
assertTrue(fixture.storage.awaitParked());
|
||||
AtomicReference<Thread> secondThread = new AtomicReference<>();
|
||||
Future<AgentRuntimeEndpoint> 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<AgentRuntimeEndpoint> first = executor.submit(() -> createEndpoint(fixture, null));
|
||||
try {
|
||||
assertTrue(fixture.storage.awaitParked());
|
||||
AtomicReference<Thread> secondThread = new AtomicReference<>();
|
||||
Future<AwsException> 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<AgentRuntimeEndpoint> first = executor.submit(() -> createEndpoint(fixture, null));
|
||||
try {
|
||||
assertTrue(fixture.storage.awaitParked());
|
||||
AtomicReference<Thread> deleteThread = new AtomicReference<>();
|
||||
Future<AgentRuntime> 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,
|
||||
|
||||
+18
-78
@@ -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<Thread> deleteThread = new AtomicReference<>();
|
||||
Future<AgentRuntime> 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<AgentRuntime> deletion = executor.submit(fixture::delete);
|
||||
try {
|
||||
assertTrue(fixture.storage.awaitParked());
|
||||
AtomicReference<Thread> tagThread = new AtomicReference<>();
|
||||
Future<AwsException> 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<String, String> 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<Thread> readThread = new AtomicReference<>();
|
||||
Future<Map<String, String>> 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<AgentRuntime> deletion = executor.submit(fixture::delete);
|
||||
try {
|
||||
assertTrue(fixture.storage.awaitParked());
|
||||
AtomicReference<Thread> readThread = new AtomicReference<>();
|
||||
Future<AwsException> 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
|
||||
|
||||
@@ -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<RepositoryCreationTemplate> 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());
|
||||
}
|
||||
|
||||
@@ -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<Thread> 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);
|
||||
|
||||
@@ -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<String> 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<String> 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]"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user