test(notification): 알림 worker 둘의 claim 수·지표·종료·pass 실패를 시험한다 #78

Merged
donghyeon.kang merged 2 commits from test/notification-worker-behavior into main 2026-11-21 11:12:09 +00:00
2 changed files with 244 additions and 0 deletions
@@ -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.
*
* <p>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());
}
}
@@ -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.
*
* <p>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.
*
* <p>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.
*
* <p>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<Boolean> closeHadReturnedWhenTheClaimEnded = new AtomicReference<>();
when(claim.handle(any()))
.thenAnswer(
invocation -> {
claimStarted.countDown();
closeHadReturnedWhenTheClaimEnded.set(
awaitIgnoringInterrupts(closeReturned, Duration.ofSeconds(2)));
claimEnded.countDown();
return List.<RecipientLease>of();
});
worker = worker(10, 2);
worker.start();
awaitOrFail(claimStarted, "the polling thread never claimed");
// when
try {
worker.close();
} finally {
// The signal is close() returning, not a second action.
closeReturned.countDown();
}
// then
awaitOrFail(claimEnded, "the claim in flight never ended");
assertThat(closeHadReturnedWhenTheClaimEnded).hasValue(false);
}
private NotificationSchedulerWorker worker(int claimBatchSize, int maxGlobalConcurrency) {
return new NotificationSchedulerWorker(
claim,
dispatch,
metrics,
new NotificationDispatchProperties(
claimBatchSize,
LEASE,
Duration.ofSeconds(1),
maxGlobalConcurrency,
0,
Duration.ofSeconds(1),
Duration.ofMinutes(5),
// Long enough that close() never gives up on the polling thread inside this test.
Duration.ofSeconds(30)),
backlog,
"worker-1");
}
private static void awaitOrFail(CountDownLatch latch, String failure)
throws InterruptedException {
if (!latch.await(10, TimeUnit.SECONDS)) {
throw new AssertionError(failure);
}
}
/**
* Waits for the latch through interrupts, then hands the interrupt back to the thread.
*
* @return whether the latch opened within the bound
*/
private static boolean awaitIgnoringInterrupts(CountDownLatch latch, Duration bound) {
long deadline = System.nanoTime() + bound.toNanos();
boolean interrupted = false;
try {
while (true) {
try {
return latch.await(deadline - System.nanoTime(), TimeUnit.NANOSECONDS);
} catch (InterruptedException ignored) {
interrupted = true;
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
}