diff --git a/messaging/messaging-policy/src/test/java/dev/caskeleton/messaging/policy/MessagingAdmissionControllerTest.java b/messaging/messaging-policy/src/test/java/dev/caskeleton/messaging/policy/MessagingAdmissionControllerTest.java index 93c7820..2306d76 100644 --- a/messaging/messaging-policy/src/test/java/dev/caskeleton/messaging/policy/MessagingAdmissionControllerTest.java +++ b/messaging/messaging-policy/src/test/java/dev/caskeleton/messaging/policy/MessagingAdmissionControllerTest.java @@ -7,11 +7,14 @@ import dev.caskeleton.messaging.api.error.MessageBackpressureException; import dev.caskeleton.messaging.api.error.MessageTooLargeException; import java.time.Duration; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import org.junit.jupiter.api.Test; class MessagingAdmissionControllerTest { private static final String DESTINATION = "order.created"; + private static final String OTHER_DESTINATION = "invoice.issued"; + private static final Duration WAITER_BOUND = Duration.ofSeconds(30); private static MessagingAdmissionController controller(int limit) { return new MessagingAdmissionController( @@ -182,16 +185,49 @@ class MessagingAdmissionControllerTest { void aWaitingCallerIsAdmittedOnceAPermitIsReturned() throws InterruptedException { MessagingAdmissionController controller = new MessagingAdmissionController( - new PayloadLimitGuard(PayloadPolicy.defaults()), - new InFlightLimiter(1, Duration.ofSeconds(2))); + new PayloadLimitGuard(PayloadPolicy.defaults()), new InFlightLimiter(1, WAITER_BOUND)); controller.admit(DESTINATION, 100); + AtomicReference refused = new AtomicReference<>(); + // Another destination, so only the process-wide permit is contested. A second publish to + // DESTINATION is refused at once by its own ceiling (equal to the process limit here) and + // never waits. + Thread waiter = + new Thread( + () -> { + try { + controller.admit(OTHER_DESTINATION, 100); + } catch (RuntimeException refusal) { + refused.set(refusal); + } + }); - Thread waiter = new Thread(() -> controller.admit(DESTINATION, 100)); waiter.start(); + awaitQueuedOnAPermit(waiter, refused); controller.complete(DESTINATION); - waiter.join(Duration.ofSeconds(5).toMillis()); + waiter.join(WAITER_BOUND.toMillis()); + assertThat(refused.get()).as("the waiter was refused instead of admitted").isNull(); assertThat(waiter.isAlive()).isFalse(); assertThat(controller.inFlight()).isEqualTo(1); + assertThat(controller.inFlight(OTHER_DESTINATION)).isEqualTo(1); + } + + /** + * Waits until {@code waiter} is parked in the limiter's bounded wait, so the permit is returned + * to a caller that is already queued rather than to one that has not arrived yet. + */ + private static void awaitQueuedOnAPermit(Thread waiter, AtomicReference refused) + throws InterruptedException { + long deadline = System.nanoTime() + WAITER_BOUND.toNanos(); + while (waiter.getState() != Thread.State.TIMED_WAITING + && waiter.isAlive() + && System.nanoTime() < deadline) { + Thread.sleep(1); + } + assertThat(waiter.getState()) + .as( + "the waiter must be queued on the permit before it is returned (refused: %s)", + refused.get()) + .isEqualTo(Thread.State.TIMED_WAITING); } }