From 69bf15ee6ef632199a2037878ef3e8e6e9e76d59 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Sat, 21 Nov 2026 11:12:58 +0900 Subject: [PATCH] =?UTF-8?q?test(mongo):=20=EC=9E=AC=EC=8B=9C=EB=8F=84=20bu?= =?UTF-8?q?dget=EC=9D=98=20backoff=20=ED=91=9C=EC=99=80=20=EB=A7=88?= =?UTF-8?q?=EC=A7=80=EB=A7=89=20=ED=97=88=EC=9A=A9=20=EC=8B=9C=EB=8F=84?= =?UTF-8?q?=EB=A5=BC=20=EB=8B=A8=EC=96=B8=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MongoRetryBudget.standard()는 운영 경로(SpringMongoTransactionExecutor)가 쓰는 재시도 한도다. 그런데 이것을 부르는 시험 둘이 약속을 단언하지 않았다. backoffGrowsAndStaysInsideTheCeiling은 진짜 난수로 delayBefore(1)이 0이고 delayBefore(5)가 상한 이하라는 것만 봤다. theRetryBudgetBoundsAttemptsAndElapsedTime 은 시도 4와 경과 1분의 거절, 시도 2의 허용만 보고 마지막 허용 시도 3은 안 봤다. 그래서 지수 증가를 지우거나(지연이 늘 20ms), jitter를 지우거나, 시도 상한 <=를 <로 바꿔도(세 번째 시도가 막힘) 시험이 모두 통과했다. 두 시험을 @ParameterizedTest 표로 바꾼다. - backoff: nextDouble()이 늘 같은 값을 주는 RandomGenerator를 넣고 시도 1..7과 66의 대기를 정확한 ms로 단언한다. nextDouble 0.0은 jitter 하한(10·20·40·80·160·250), 0.999는 단계 바로 아래(19·39·79·159·319·499) 다. 시도 7이 상한 500에서 멈추는 것을 보인다. 시도 66은 shift 거리 clamp를 지키는 행이다(Java는 long shift 거리의 아래 6비트만 쓴다). - 시도 한도: 시도 2·3 허용, 4 거절, 경과 9999ms 허용, 10000ms 거절, 60000ms 거절, 첫 시도는 경과 60000ms에도 허용. 버린 안: coordinator 수준에서 sleep 시간을 잰다. 벽시계 흔들림에 기댄다. 위험·한계: 시험만 바꾼다. main 코드와 다른 시험의 budget은 그대로다. mongoStableContractTest의 실행 수가 2에서 21로 는다(메서드 수는 같다). 기대값은 standard()의 값(20ms·500ms·0.5)에 묶인다. 그 값을 바꾸면 이 표도 함께 바꿔야 한다. 그것이 이 시험의 목적이다. 검증: - desktop scripts/verify.sh leaf :adapter:outbound:persistence-mongo rc=0 (트리 f21bbd2d, tests=583 failures=0) - desktop scripts/verify.sh pr --no-cache rc=0 (트리 f21bbd2d, tests=7329 failures=0 skipped=35, FROM-CACHE 0) - 노트북 scripts/verify.sh docs rc=0 - 변이 5개(지수 shift·jitter·시도 상한 <=·shift clamp·첫 시도 조기 반환) 각각 고친 시험이 실패, 고치기 전 시험은 앞 셋 모두 통과 Refs: REV-BE-0002 --- .../MongoTransactionRetryCoordinatorTest.java | 71 ++++++++++++++++--- 1 file changed, 61 insertions(+), 10 deletions(-) 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( -- 2.54.0