diff --git a/src/bootstrap/notification-platform/src/test/java/dev/caskeleton/composition/notification/NotificationBackgroundWorkersTest.java b/src/bootstrap/notification-platform/src/test/java/dev/caskeleton/composition/notification/NotificationBackgroundWorkersTest.java new file mode 100644 index 00000000..a1898263 --- /dev/null +++ b/src/bootstrap/notification-platform/src/test/java/dev/caskeleton/composition/notification/NotificationBackgroundWorkersTest.java @@ -0,0 +1,61 @@ +package dev.caskeleton.composition.notification; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import dev.caskeleton.application.notification.platform.callback.ReplayProviderEventsUseCase; +import dev.caskeleton.application.notification.platform.dispatch.ReconcileOutstandingAttemptsUseCase; +import dev.caskeleton.application.notification.platform.dispatch.RecoverAbandonedDeliveriesCommand; +import dev.caskeleton.application.notification.platform.dispatch.RecoverAbandonedDeliveriesUseCase; +import java.time.Duration; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +/** + * A failed pass does not stop the schedule. An exception that escapes a task given to {@code + * scheduleWithFixedDelay} cancels every later run without a word, so a recovery pass that failed + * once would never run again while the process lived. + * + *
The three passes are concrete use case classes whose dependencies are final services of the + * application leaf, so they are Mockito stand-ins, and the failure is injected through the recovery + * pass. The replay and reconciliation passes go through the same wrapper in {@code schedule}, so + * one pass shows the rule for the three. The wait is Mockito's bounded verification, not a sleep: + * it ends at the second call. + */ +class NotificationBackgroundWorkersTest { + + private final RecoverAbandonedDeliveriesUseCase recovery = + mock(RecoverAbandonedDeliveriesUseCase.class); + private final ReplayProviderEventsUseCase replay = mock(ReplayProviderEventsUseCase.class); + private final ReconcileOutstandingAttemptsUseCase reconciliation = + mock(ReconcileOutstandingAttemptsUseCase.class); + + private NotificationBackgroundWorkers workers; + + @AfterEach + void closeWorkers() { + if (workers != null) { + workers.close(); + } + } + + @Test + void aRecoveryPassThatThrowsRunsAgainOnItsSchedule() { + // given + when(recovery.handle(any())) + .thenThrow(new IllegalStateException("lease store unavailable")) + .thenReturn(0); + workers = + new NotificationBackgroundWorkers( + recovery, replay, reconciliation, Duration.ofMillis(5), Duration.ofSeconds(5)); + + // when + workers.start(); + + // then + verify(recovery, timeout(10_000).atLeast(2)).handle(new RecoverAbandonedDeliveriesCommand()); + } +} diff --git a/src/bootstrap/notification-platform/src/test/java/dev/caskeleton/composition/notification/NotificationSchedulerWorkerTest.java b/src/bootstrap/notification-platform/src/test/java/dev/caskeleton/composition/notification/NotificationSchedulerWorkerTest.java new file mode 100644 index 00000000..9f5a9cc7 --- /dev/null +++ b/src/bootstrap/notification-platform/src/test/java/dev/caskeleton/composition/notification/NotificationSchedulerWorkerTest.java @@ -0,0 +1,183 @@ +package dev.caskeleton.composition.notification; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import dev.caskeleton.adapter.outbound.notification.platform.dispatch.NotificationDispatchProperties; +import dev.caskeleton.application.notification.platform.dispatch.ClaimDueDeliveriesCommand; +import dev.caskeleton.application.notification.platform.dispatch.ClaimDueDeliveriesUseCase; +import dev.caskeleton.application.notification.platform.dispatch.DispatchClaimedDeliveryUseCase; +import dev.caskeleton.application.notification.platform.dispatch.RecipientLease; +import dev.caskeleton.application.notification.platform.observation.MeasureDispatchBacklogUseCase; +import dev.caskeleton.application.notification.platform.observation.NotificationMetricName; +import dev.caskeleton.application.notification.platform.observation.NotificationMetricsPort; +import java.time.Duration; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +/** + * The scheduler worker's own rules: how much it claims, what it publishes as queue depth, and what + * "closed" means. Each of the three was a defect once (the comments in {@link + * NotificationSchedulerWorker} say which), and nothing constructed the worker to see them come + * back. + * + *
The worker is built with {@code new}, the way {@link NotificationWorkerConfiguration} builds + * it (README: the worker is not a bean of its own). Its collaborators are concrete use case classes + * whose dependencies are final services of the application leaf, so they are Mockito stand-ins + * here, as the outbox relay's scheduler tests stand in for theirs; nothing in this class depends on + * what the use cases do inside. The metrics port has no fake in a testFixtures set, and the one + * thing read from it is the gauge call, so it is a stand-in too. Time is never waited out: each + * step waits on a latch with a bound, and a bound that runs out fails the test. + */ +class NotificationSchedulerWorkerTest { + + private static final Duration LEASE = Duration.ofSeconds(30); + + private final ClaimDueDeliveriesUseCase claim = mock(ClaimDueDeliveriesUseCase.class); + private final DispatchClaimedDeliveryUseCase dispatch = + mock(DispatchClaimedDeliveryUseCase.class); + private final NotificationMetricsPort metrics = mock(NotificationMetricsPort.class); + private final MeasureDispatchBacklogUseCase backlog = mock(MeasureDispatchBacklogUseCase.class); + + private NotificationSchedulerWorker worker; + + @AfterEach + void closeWorker() { + if (worker != null) { + worker.close(); + } + } + + @Test + @DisplayName("동시 실행 허가가 2개면 batch가 10이어도 2건만 claim한다") + void aBatchLargerThanTheFreePermitsClaimsOnlyWhatCanStartNow() { + // given + when(claim.handle(any())).thenReturn(List.of()); + worker = worker(10, 2); + + // when + worker.runOnce(); + + // then + verify(claim).handle(new ClaimDueDeliveriesCommand("worker-1", 2, LEASE)); + } + + @Test + @DisplayName("큐 깊이 지표는 이번 batch 크기가 아니라 backlog 조회 결과다") + void theQueueDepthGaugeReportsTheBacklogRatherThanTheBatch() { + // given + when(claim.handle(any())).thenReturn(List.of()); + when(backlog.handle(any())).thenReturn(12_345L); + worker = worker(10, 2); + + // when + worker.runOnce(); + + // then + verify(metrics).gauge(NotificationMetricName.QUEUE_DEPTH, Map.of(), 12_345.0); + } + + /** + * A claim in flight when shutdown begins ends before close() returns. + * + *
The stand-in claim does not stop when interrupted, as a claim inside a JDBC call does not, + * and gives close() two seconds to return while it is still running. A close() that waits for the + * polling thread cannot return in that time, because the thread it waits for is the one running + * the claim; a close() that only asks the thread to stop returns at once, and the claim then ends + * with the worker already "closed" — the leases it took held by a process that has shut down. + * + *
The bound only decides how fast a missing join is caught: the correct close() returns false
+ * whatever the load, and a close() without the join that takes longer than the bound to return
+ * would let that one run pass.
+ */
+ @Test
+ void closeReturnsOnlyAfterTheClaimInFlightHasEnded() throws InterruptedException {
+ // given
+ CountDownLatch claimStarted = new CountDownLatch(1);
+ CountDownLatch closeReturned = new CountDownLatch(1);
+ CountDownLatch claimEnded = new CountDownLatch(1);
+ AtomicReference