review-followup Wave 3 라운드 3(P19): 3-D 공통 유틸 — Sha256·HmacSha256·Backoff·MdcKeys·TenantId, caller 트랜잭션 guard 하나
- shared-contract `Sha256`(약 65벌 흡수, notification `MessageDigestPort` 삭제), `HmacSha256`(서명 헬퍼), `Backoff`("full jitter" 뜻 하나),
`MdcKeys`(gRPC·STOMP에도 상관관계), registry edge application-notification → shared-contract
- application-core `TenantId` 하나로 web·GraphQL tenant 문법 통일, web operation 경로에 tenant 해석(JWT `tenant_id`)
- persistence-jpa `CallerTransaction` 하나(복사본 4벌 + `JpaStreamScope` 흡수), 범위 없는 멱등성·outbox 리포지토리 메서드 제거(E-6b)
- 주입된 `Clock` 우회 제거(objectstorage·websocket·mongo·web stream), GraphQL 오류가 HTTP request id 사용, notification UUIDv7은 uuid-creator
- 호출처 없는 `WebRequestContext`·argument resolver 삭제
- 결함 수정: `WebStreamRegistry` 상한 경합(ORCH-new-3 — bin 하나만 잠그는 lock 안에서 크기 확인, 원자 카운터로 슬롯 예약)
- 현황표: 소유자 결정 33(persistence-notification·fileserver → persistence-jpa edge), 34(id 포트 domain 전환 보류), 35(X2-P2-6b 나머지 보류)
검증: ./gradlew check architectureCheck verifyDependencyLocks verifyPublicPathSnapshot :app-bootstrap:verifyEnvKeys configContractCheck
:app-bootstrap:integrationTest :sample-portfolio:integrationTest :sample-portfolio:systemTest graphql·websocket systemTest
persistence-jpa jpaPlatformContractTest·postgresql{Idempotency,Outbox*}IntegrationTest --continue 통과(695 task)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4f4bd1fd34
commit
09e2dd63d6
@@ -24,6 +24,13 @@ envelope shape, metric cardinality bounds, tracing seam, domain-context propagat
|
||||
- Tracing contract types (`tracing/TraceParent`, `tracing/SpanErrorRecorder`) — W3C
|
||||
`traceparent` value type and the span-error-recording seam (feature-distributed-tracing-contract; the OTel
|
||||
tracer runtime is a fork-activated seam, so these stay Java-stdlib-only).
|
||||
- Stdlib helpers every leaf used to copy (review X2-P1-5, X2-P1-4b, X1-#9): `digest/Sha256` (the
|
||||
SHA-256 digest value and the one `MessageDigest.getInstance("SHA-256")`), `digest/HmacSha256`
|
||||
(sign and constant-time verify), `retry/Backoff` (capped exponential ceiling; "full jitter" means
|
||||
`U[0, ceiling]` and nothing else). Vendored `messaging:*` and the optional gRPC build do not depend
|
||||
on this module and keep their own copies.
|
||||
- `observability/MdcKeys`: the snake_case MDC key names (registry `docs/registries/mdc-keys.yaml`)
|
||||
every transport puts a request's correlation under — web, GraphQL, STOMP and gRPC alike.
|
||||
- Registry-rule mirrors read only by contract tests live in `testFixtures`, not `main`:
|
||||
`metrics/MetricNaming`, `metrics/CardinalityBounds`, `metrics/AlertSeverity`,
|
||||
`tracing/BaggageAllowlist`. Move one to `main` only when production code consumes it.
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package dev.caskeleton.shared.digest;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Objects;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
/**
|
||||
* HMAC-SHA256 signing and constant-time verification.
|
||||
*
|
||||
* <p>The one sign/verify pair the signed codecs share (cursor codecs, keyed fingerprints). Each
|
||||
* used to hold its own {@code Mac.getInstance} block, its own "unreachable" catch and — the part
|
||||
* that matters — its own verification, where a short-circuiting comparison would leak the correct
|
||||
* MAC one byte at a time. {@link #verify} compares with {@link MessageDigest#isEqual} and refuses a
|
||||
* presented MAC of the wrong length before comparing.
|
||||
*
|
||||
* <p>{@link Mac} is not thread-safe, so every call takes a fresh instance.
|
||||
*/
|
||||
public final class HmacSha256 {
|
||||
|
||||
/** HMAC-SHA256 produces exactly this many bytes. */
|
||||
public static final int MAC_BYTES = 32;
|
||||
|
||||
private static final String ALGORITHM = "HmacSHA256";
|
||||
|
||||
private HmacSha256() {}
|
||||
|
||||
/**
|
||||
* The MAC of {@code message} under {@code key}.
|
||||
*
|
||||
* @throws IllegalArgumentException if the key is empty
|
||||
*/
|
||||
public static byte[] sign(byte[] key, byte[] message) {
|
||||
Objects.requireNonNull(key, "key");
|
||||
Objects.requireNonNull(message, "message");
|
||||
if (key.length == 0) {
|
||||
throw new IllegalArgumentException("HMAC key must not be empty");
|
||||
}
|
||||
try {
|
||||
Mac mac = Mac.getInstance(ALGORITHM);
|
||||
mac.init(new SecretKeySpec(key, ALGORITHM));
|
||||
return mac.doFinal(message);
|
||||
} catch (GeneralSecurityException unreachable) {
|
||||
// HmacSHA256 is mandated by every JDK, and a non-empty raw key always initialises it.
|
||||
throw new IllegalStateException("HMAC-SHA256 is unavailable", unreachable);
|
||||
}
|
||||
}
|
||||
|
||||
/** The MAC of the UTF-8 encoding of {@code message} under {@code key}. */
|
||||
public static byte[] sign(byte[] key, String message) {
|
||||
Objects.requireNonNull(message, "message");
|
||||
return sign(key, message.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code presented} is the MAC of {@code message} under {@code key}, in constant time.
|
||||
*
|
||||
* @return {@code false} for a MAC of the wrong length, without comparing
|
||||
*/
|
||||
public static boolean verify(byte[] key, byte[] message, byte[] presented) {
|
||||
if (presented == null || presented.length != MAC_BYTES) {
|
||||
return false;
|
||||
}
|
||||
return MessageDigest.isEqual(sign(key, message), presented);
|
||||
}
|
||||
|
||||
/** {@link #verify(byte[], byte[], byte[])} over the UTF-8 encoding of {@code message}. */
|
||||
public static boolean verify(byte[] key, String message, byte[] presented) {
|
||||
Objects.requireNonNull(message, "message");
|
||||
return verify(key, message.getBytes(StandardCharsets.UTF_8), presented);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package dev.caskeleton.shared.digest;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* An immutable SHA-256 digest, and the one place this repository asks the JDK for SHA-256.
|
||||
*
|
||||
* <p>Every JDK must ship SHA-256, so {@link NoSuchAlgorithmException} is unreachable; before this
|
||||
* type each caller wrapped {@code MessageDigest.getInstance("SHA-256")} in its own try/catch, hex
|
||||
* encoder and "unreachable" comment. One-shot hashing is {@link #of(byte[])} or {@link
|
||||
* #ofUtf8(String)}; a caller that hashes a stream incrementally takes a fresh {@link
|
||||
* #newMessageDigest()}.
|
||||
*
|
||||
* <p>Vendored {@code messaging:*} and the optional gRPC build keep their own copies: they do not
|
||||
* depend on this module.
|
||||
*
|
||||
* @param bytes the 32 digest bytes
|
||||
*/
|
||||
@SuppressWarnings("ArrayRecordComponent")
|
||||
public record Sha256(byte[] bytes) {
|
||||
|
||||
public static final int BYTE_LENGTH = 32;
|
||||
|
||||
private static final String ALGORITHM = "SHA-256";
|
||||
|
||||
public Sha256 {
|
||||
if (bytes == null || bytes.length != BYTE_LENGTH) {
|
||||
throw new IllegalArgumentException("SHA-256 digest must contain exactly 32 bytes");
|
||||
}
|
||||
bytes = bytes.clone();
|
||||
}
|
||||
|
||||
/** The SHA-256 digest of {@code content}. */
|
||||
public static Sha256 of(byte[] content) {
|
||||
Objects.requireNonNull(content, "content");
|
||||
return new Sha256(newMessageDigest().digest(content));
|
||||
}
|
||||
|
||||
/** The SHA-256 digest of the UTF-8 encoding of {@code content}. */
|
||||
public static Sha256 ofUtf8(String content) {
|
||||
Objects.requireNonNull(content, "content");
|
||||
return of(content.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/** A fresh SHA-256 {@link MessageDigest} for incremental hashing. */
|
||||
public static MessageDigest newMessageDigest() {
|
||||
try {
|
||||
return MessageDigest.getInstance(ALGORITHM);
|
||||
} catch (NoSuchAlgorithmException unreachable) {
|
||||
// SHA-256 is mandated by every JDK.
|
||||
throw new IllegalStateException("SHA-256 algorithm unavailable", unreachable);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] bytes() {
|
||||
return bytes.clone();
|
||||
}
|
||||
|
||||
/** The digest as 64 lowercase hex characters. */
|
||||
public String hex() {
|
||||
return HexFormat.of().formatHex(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return this == other || (other instanceof Sha256 sha256 && Arrays.equals(bytes, sha256.bytes));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return hex();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package dev.caskeleton.shared.observability;
|
||||
|
||||
/**
|
||||
* The snake_case MDC key names every transport puts a request's correlation under.
|
||||
*
|
||||
* <p>The registry is {@code docs/registries/mdc-keys.yaml}. These lived in the web adapter, so a
|
||||
* GraphQL, WebSocket or gRPC request could not name them without depending on the servlet adapter
|
||||
* (review X2-P2-5); here any leaf that has the shared-contract edge can.
|
||||
*/
|
||||
public final class MdcKeys {
|
||||
|
||||
public static final String REQUEST_ID = "request_id";
|
||||
public static final String TRACE_ID = "trace_id";
|
||||
public static final String SPAN_ID = "span_id";
|
||||
public static final String CORRELATION_ID = "correlation_id";
|
||||
public static final String USER_PRINCIPAL = "user_principal";
|
||||
|
||||
private MdcKeys() {}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package dev.caskeleton.shared.retry;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Objects;
|
||||
import java.util.random.RandomGenerator;
|
||||
|
||||
/**
|
||||
* Capped exponential backoff and the jitter words that go with it, defined once.
|
||||
*
|
||||
* <p>The retry loops of this repository each wrote the formula themselves, and "full jitter" meant
|
||||
* three different things: {@code U(0, ceiling]}, {@code base·2ⁿ + U(0, base)} with no ceiling, and
|
||||
* a full jitter floored at the base. Here the words have one meaning each:
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@link #ceiling(int) ceiling}: {@code min(max, base · 2^(attempt-1))}, attempt 1-based.
|
||||
* <li>{@link #fullJitter(int, RandomGenerator) full jitter}: uniform in {@code [0, ceiling]}.
|
||||
* <li>{@link #decorrelatedJitter(Duration, RandomGenerator) decorrelated jitter}: uniform in
|
||||
* {@code [base, min(max, max(base, 3 · previous))]}.
|
||||
* </ul>
|
||||
*
|
||||
* <p>Arithmetic is in milliseconds and saturates rather than overflowing, so a large attempt number
|
||||
* yields the ceiling instead of a negative delay. The random source is injected so a schedule is
|
||||
* deterministic in a test.
|
||||
*
|
||||
* @param base the first attempt's ceiling; not negative (zero means every delay is zero)
|
||||
* @param max the largest delay this backoff produces; not smaller than {@code base}
|
||||
*/
|
||||
public record Backoff(Duration base, Duration max) {
|
||||
|
||||
public Backoff {
|
||||
Objects.requireNonNull(base, "base");
|
||||
Objects.requireNonNull(max, "max");
|
||||
if (base.isNegative()) {
|
||||
throw new IllegalArgumentException("backoff base must not be negative");
|
||||
}
|
||||
if (max.compareTo(base) < 0) {
|
||||
throw new IllegalArgumentException("backoff max must not be smaller than base");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The exponential ceiling for a 1-based attempt: {@code min(max, base · 2^(attempt-1))}.
|
||||
*
|
||||
* @throws IllegalArgumentException if {@code attempt < 1}
|
||||
*/
|
||||
public Duration ceiling(int attempt) {
|
||||
if (attempt < 1) {
|
||||
throw new IllegalArgumentException("attempt must be >= 1 (1-based), was " + attempt);
|
||||
}
|
||||
long baseMillis = base.toMillis();
|
||||
long maxMillis = max.toMillis();
|
||||
int shift = attempt - 1;
|
||||
// base << shift overflows once shift reaches the leading zeros of base; the ceiling is max
|
||||
// then.
|
||||
long scaled =
|
||||
shift >= Long.numberOfLeadingZeros(baseMillis) - 1 ? Long.MAX_VALUE : baseMillis << shift;
|
||||
return Duration.ofMillis(Math.min(scaled, maxMillis));
|
||||
}
|
||||
|
||||
/** Full jitter: a delay uniform in {@code [0, ceiling(attempt)]}. */
|
||||
public Duration fullJitter(int attempt, RandomGenerator random) {
|
||||
Objects.requireNonNull(random, "random");
|
||||
long ceilingMillis = ceiling(attempt).toMillis();
|
||||
return Duration.ofMillis(random.nextLong(ceilingMillis + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorrelated jitter: a delay uniform in {@code [base, min(max, max(base, 3 · previous))]}.
|
||||
*
|
||||
* @param previous the delay this schedule chose last time; the base for the first retry
|
||||
*/
|
||||
public Duration decorrelatedJitter(Duration previous, RandomGenerator random) {
|
||||
Objects.requireNonNull(previous, "previous");
|
||||
Objects.requireNonNull(random, "random");
|
||||
long lower = base.toMillis();
|
||||
long previousMillis = Math.max(0, previous.toMillis());
|
||||
long tripled = previousMillis > Long.MAX_VALUE / 3 ? Long.MAX_VALUE : previousMillis * 3;
|
||||
long upper = Math.min(max.toMillis(), Math.max(lower, tripled));
|
||||
return Duration.ofMillis(upper <= lower ? lower : random.nextLong(lower, upper + 1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package dev.caskeleton.shared.digest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HexFormat;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HmacSha256Test {
|
||||
|
||||
private static final byte[] KEY = "key".getBytes(StandardCharsets.UTF_8);
|
||||
private static final String MESSAGE = "The quick brown fox jumps over the lazy dog";
|
||||
|
||||
@Test
|
||||
@DisplayName("널리 쓰이는 공개 예제(키 \"key\", quick brown fox)의 MAC 값과 같다")
|
||||
void theMacMatchesThePublishedExample() {
|
||||
assertThat(HexFormat.of().formatHex(HmacSha256.sign(KEY, MESSAGE)))
|
||||
.isEqualTo("f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theMacOfTheMessageVerifies() {
|
||||
// given
|
||||
byte[] mac = HmacSha256.sign(KEY, MESSAGE);
|
||||
|
||||
// when
|
||||
boolean verified = HmacSha256.verify(KEY, MESSAGE, mac);
|
||||
|
||||
// then
|
||||
assertThat(verified).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMacOfAnotherMessageDoesNotVerify() {
|
||||
// given
|
||||
byte[] mac = HmacSha256.sign(KEY, MESSAGE + ".");
|
||||
|
||||
// when
|
||||
boolean verified = HmacSha256.verify(KEY, MESSAGE, mac);
|
||||
|
||||
// then
|
||||
assertThat(verified).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("길이가 32바이트가 아닌 MAC은 비교하지 않고 거부한다")
|
||||
void aMacOfTheWrongLengthDoesNotVerify() {
|
||||
// given
|
||||
byte[] truncated = new byte[HmacSha256.MAC_BYTES - 1];
|
||||
|
||||
// when
|
||||
boolean verified = HmacSha256.verify(KEY, MESSAGE, truncated);
|
||||
|
||||
// then
|
||||
assertThat(verified).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEmptyKeyIsRefused() {
|
||||
// when
|
||||
Throwable thrown = catchThrowable(() -> HmacSha256.sign(new byte[0], MESSAGE));
|
||||
|
||||
// then
|
||||
assertThat(thrown)
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("HMAC key must not be empty");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package dev.caskeleton.shared.digest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class Sha256Test {
|
||||
|
||||
private static final String ABC_HEX =
|
||||
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
|
||||
|
||||
@Test
|
||||
@DisplayName("\"abc\"의 digest는 FIPS 180-2 예제 값이다")
|
||||
void theUtf8DigestMatchesTheStandardTestVector() {
|
||||
assertThat(Sha256.ofUtf8("abc").hex()).isEqualTo(ABC_HEX);
|
||||
}
|
||||
|
||||
@Test
|
||||
void byteAndUtf8DigestsOfTheSameContentAreEqual() {
|
||||
assertThat(Sha256.of("abc".getBytes(StandardCharsets.UTF_8))).isEqualTo(Sha256.ofUtf8("abc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anIncrementalDigestMatchesTheOneShotDigest() {
|
||||
// given
|
||||
MessageDigest digest = Sha256.newMessageDigest();
|
||||
digest.update("a".getBytes(StandardCharsets.UTF_8));
|
||||
digest.update("bc".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
// when
|
||||
Sha256 incremental = new Sha256(digest.digest());
|
||||
|
||||
// then
|
||||
assertThat(incremental).isEqualTo(Sha256.ofUtf8("abc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theDigestBytesCannotBeChangedFromOutside() {
|
||||
// given
|
||||
Sha256 digest = Sha256.ofUtf8("abc");
|
||||
|
||||
// when
|
||||
digest.bytes()[0] = 0;
|
||||
|
||||
// then
|
||||
assertThat(digest.hex()).isEqualTo(ABC_HEX);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("32바이트가 아닌 digest는 거부한다")
|
||||
void aDigestOfTheWrongLengthIsRefused() {
|
||||
// when
|
||||
Throwable thrown = catchThrowable(() -> new Sha256(new byte[31]));
|
||||
|
||||
// then
|
||||
assertThat(thrown)
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("SHA-256 digest must contain exactly 32 bytes");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package dev.caskeleton.shared.retry;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.random.RandomGenerator;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
class BackoffTest {
|
||||
|
||||
private static final Backoff BACKOFF = new Backoff(Duration.ofMillis(100), Duration.ofSeconds(1));
|
||||
|
||||
/** Always draws the upper end of the requested range. */
|
||||
private static final RandomGenerator HIGHEST =
|
||||
new RandomGenerator() {
|
||||
@Override
|
||||
public long nextLong() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long nextLong(long bound) {
|
||||
return bound - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long nextLong(long origin, long bound) {
|
||||
return bound - 1;
|
||||
}
|
||||
};
|
||||
|
||||
/** Always draws the lower end of the requested range. */
|
||||
private static final RandomGenerator LOWEST =
|
||||
new RandomGenerator() {
|
||||
@Override
|
||||
public long nextLong() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long nextLong(long bound) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long nextLong(long origin, long bound) {
|
||||
return origin;
|
||||
}
|
||||
};
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"1, 100", "2, 200", "4, 800", "5, 1000", "64, 1000", "2147483647, 1000"})
|
||||
@DisplayName("ceiling은 base·2^(n-1)이고 max에서 멈추며 큰 n에서도 넘치지 않는다")
|
||||
void theCeilingDoublesPerAttemptUpToTheMax(int attempt, long expectedMillis) {
|
||||
assertThat(BACKOFF.ceiling(attempt)).isEqualTo(Duration.ofMillis(expectedMillis));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("full jitter의 위쪽 끝은 ceiling 자체다")
|
||||
void fullJitterReachesTheCeiling() {
|
||||
assertThat(BACKOFF.fullJitter(3, HIGHEST)).isEqualTo(Duration.ofMillis(400));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("full jitter의 아래쪽 끝은 0이다")
|
||||
void fullJitterReachesZero() {
|
||||
assertThat(BACKOFF.fullJitter(3, LOWEST)).isEqualTo(Duration.ZERO);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("decorrelated jitter는 이전 지연의 3배와 max 중 작은 값까지다")
|
||||
void decorrelatedJitterIsBoundedByThreeTimesThePreviousDelay() {
|
||||
assertThat(BACKOFF.decorrelatedJitter(Duration.ofMillis(200), HIGHEST))
|
||||
.isEqualTo(Duration.ofMillis(600));
|
||||
}
|
||||
|
||||
@Test
|
||||
void decorrelatedJitterNeverGoesBelowTheBase() {
|
||||
assertThat(BACKOFF.decorrelatedJitter(Duration.ofMillis(200), LOWEST))
|
||||
.isEqualTo(Duration.ofMillis(100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAttemptBelowOneIsRefused() {
|
||||
// when
|
||||
Throwable thrown = catchThrowable(() -> BACKOFF.ceiling(0));
|
||||
|
||||
// then
|
||||
assertThat(thrown)
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("attempt must be >= 1 (1-based), was 0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMaxBelowTheBaseIsRefused() {
|
||||
// when
|
||||
Throwable thrown =
|
||||
catchThrowable(() -> new Backoff(Duration.ofSeconds(2), Duration.ofSeconds(1)));
|
||||
|
||||
// then
|
||||
assertThat(thrown)
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("backoff max must not be smaller than base");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user