diff --git a/src/adapter/outbound/persistence-mongo/src/test/java/dev/caskeleton/adapter/outbound/mongo/transaction/retry/MongoTransactionRetryCoordinatorTest.java b/src/adapter/outbound/persistence-mongo/src/test/java/dev/caskeleton/adapter/outbound/mongo/transaction/retry/MongoTransactionRetryCoordinatorTest.java index 62c17905..3db7f005 100644 --- a/src/adapter/outbound/persistence-mongo/src/test/java/dev/caskeleton/adapter/outbound/mongo/transaction/retry/MongoTransactionRetryCoordinatorTest.java +++ b/src/adapter/outbound/persistence-mongo/src/test/java/dev/caskeleton/adapter/outbound/mongo/transaction/retry/MongoTransactionRetryCoordinatorTest.java @@ -20,6 +20,8 @@ import java.util.random.RandomGenerator; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.springframework.data.mongodb.core.MongoOperations; /** Design §14.3, D-10 — an unknown commit retries the commit and never the body. */ @@ -132,22 +134,71 @@ class MongoTransactionRetryCoordinatorTest { .containsExactly(MongoRetryScope.COMMIT_ONLY); } - @Test - void theRetryBudgetBoundsAttemptsAndElapsedTime() { + @DisplayName("standard budget은 첫 시도는 늘, 재시도는 시도 3까지 경과 10초 미만일 때만 허용한다") + @ParameterizedTest(name = "시도 {0}, 경과 {1} ms → {2}") + @CsvSource({ + "2, 10, true", + "3, 10, true", + "4, 10, false", + "3, 9999, true", + "3, 10000, false", + "2, 60000, false", + "1, 60000, true" + }) + void theRetryBudgetBoundsAttemptsAndElapsedTime( + int nextAttempt, long elapsedMillis, boolean allowed) { MongoRetryBudget budget = MongoRetryBudget.standard(); - assertThat(budget.allowsAttempt(4, Duration.ZERO)).isFalse(); - assertThat(budget.allowsAttempt(2, Duration.ofMinutes(1))).isFalse(); - assertThat(budget.allowsAttempt(2, Duration.ofMillis(10))).isTrue(); + assertThat(budget.allowsAttempt(nextAttempt, Duration.ofMillis(elapsedMillis))) + .isEqualTo(allowed); } - @Test - void backoffGrowsAndStaysInsideTheCeiling() { + /** + * standard(): base 20 ms, ceiling 500 ms, jitter 0.5. The step before attempt n is 20 ms doubled + * n-2 times and held at 500 ms; the delay is that step times 0.5 plus 0.5 times nextDouble(). + * nextDouble() is in [0, 1), so 0.0 gives the lower bound (half the step) and 0.999 sits just + * under the step. Attempt 66 is there because Java uses only the low six bits of a long shift + * distance: without the clamp on the shift, attempt 66 would shift by 0 and wait 10 ms instead of + * 250 ms. + */ + @DisplayName("standard budget의 대기는 20 ms에서 두 배씩 늘어 500 ms에서 멈추고, jitter는 그 절반까지만 줄인다") + @ParameterizedTest(name = "시도 {0}, nextDouble {1} → {2} ms") + @CsvSource({ + "1, 0.999, 0", + "2, 0.0, 10", + "2, 0.999, 19", + "3, 0.0, 20", + "3, 0.999, 39", + "4, 0.0, 40", + "4, 0.999, 79", + "5, 0.0, 80", + "5, 0.999, 159", + "6, 0.0, 160", + "6, 0.999, 319", + "7, 0.0, 250", + "7, 0.999, 499", + "66, 0.0, 250" + }) + void backoffGrowsAndStaysInsideTheCeiling(int attempt, double random, long expectedMillis) { MongoRetryBudget budget = MongoRetryBudget.standard(); - RandomGenerator fixed = RandomGenerator.getDefault(); - assertThat(budget.delayBefore(1, fixed)).isEqualTo(Duration.ZERO); - assertThat(budget.delayBefore(5, fixed)).isLessThanOrEqualTo(budget.maxBackoff()); + assertThat(budget.delayBefore(attempt, constant(random))) + .isEqualTo(Duration.ofMillis(expectedMillis)); + } + + /** A generator whose {@code nextDouble()} is always {@code value}, so the jitter is fixed. */ + private static RandomGenerator constant(double value) { + return new RandomGenerator() { + @Override + public double nextDouble() { + return value; + } + + @Override + public long nextLong() { + throw new UnsupportedOperationException("the retry budget draws only nextDouble()"); + } + }; } private static MongoTransactionRetryCoordinator coordinator(