test(httpclient): backoff가 Retry-After 정책과 남은 deadline으로 정한 대기를 단언한다 #72
+93
@@ -0,0 +1,93 @@
|
||||
package dev.caskeleton.adapter.outbound.httpclient.resilience;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import dev.caskeleton.adapter.outbound.httpclient.profile.JitterStrategy;
|
||||
import dev.caskeleton.adapter.outbound.httpclient.profile.RetryAfterPolicy;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
import java.util.SplittableRandom;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
|
||||
/**
|
||||
* The wait {@link ExponentialFullJitterBackoff} returns for each {@link RetryAfterPolicy}, and the
|
||||
* remaining deadline that bounds all of them.
|
||||
*
|
||||
* <p>{@code delay} reads no clock: the remaining deadline is an argument, so every expectation here
|
||||
* is an exact duration. Jitter is {@code NONE}, so the computed wait is the ceiling {@code min(max,
|
||||
* base · 2^(attempt-1))}, 10ms after the first attempt; the generator is seeded only so the inputs
|
||||
* stay fixed.
|
||||
*/
|
||||
class ExponentialFullJitterBackoffTest {
|
||||
|
||||
private static final Duration BASE_BACKOFF = Duration.ofMillis(10);
|
||||
private static final Duration MAX_BACKOFF = Duration.ofMillis(40);
|
||||
private static final Duration UPSTREAM_RETRY_AFTER = Duration.ofSeconds(30);
|
||||
private static final Duration AMPLE_DEADLINE = Duration.ofMinutes(5);
|
||||
private static final int AFTER_FIRST_ATTEMPT = 1;
|
||||
|
||||
@Test
|
||||
@DisplayName("HONOR는 max backoff(40ms)보다 긴 Retry-After 30초를 줄이지 않고 기다린다")
|
||||
void honorWaitsTheWholeRetryAfterAboveMaxBackoff() {
|
||||
// given
|
||||
ExponentialFullJitterBackoff backoff = backoff(RetryAfterPolicy.HONOR);
|
||||
|
||||
// when
|
||||
Duration delay =
|
||||
backoff.delay(AFTER_FIRST_ATTEMPT, Optional.of(UPSTREAM_RETRY_AFTER), AMPLE_DEADLINE);
|
||||
|
||||
// then
|
||||
assertThat(delay).isEqualTo(Duration.ofSeconds(30));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("CAP은 Retry-After 30초를 max backoff(40ms)로 줄인다")
|
||||
void capShortensTheRetryAfterToMaxBackoff() {
|
||||
// given
|
||||
ExponentialFullJitterBackoff backoff = backoff(RetryAfterPolicy.CAP);
|
||||
|
||||
// when
|
||||
Duration delay =
|
||||
backoff.delay(AFTER_FIRST_ATTEMPT, Optional.of(UPSTREAM_RETRY_AFTER), AMPLE_DEADLINE);
|
||||
|
||||
// then
|
||||
assertThat(delay).isEqualTo(Duration.ofMillis(40));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("IGNORE는 Retry-After 30초를 버리고 계산한 대기(첫 시도 뒤 10ms)를 쓴다")
|
||||
void ignoreDiscardsTheRetryAfterForTheComputedWait() {
|
||||
// given
|
||||
ExponentialFullJitterBackoff backoff = backoff(RetryAfterPolicy.IGNORE);
|
||||
|
||||
// when
|
||||
Duration delay =
|
||||
backoff.delay(AFTER_FIRST_ATTEMPT, Optional.of(UPSTREAM_RETRY_AFTER), AMPLE_DEADLINE);
|
||||
|
||||
// then
|
||||
assertThat(delay).isEqualTo(Duration.ofMillis(10));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@EnumSource(RetryAfterPolicy.class)
|
||||
@DisplayName("남은 deadline 5ms가 어느 정책의 대기보다 짧으면 대기는 5ms다")
|
||||
void theRemainingDeadlineBoundsTheWaitUnderEveryPolicy(RetryAfterPolicy policy) {
|
||||
// given
|
||||
ExponentialFullJitterBackoff backoff = backoff(policy);
|
||||
|
||||
// when
|
||||
Duration delay =
|
||||
backoff.delay(AFTER_FIRST_ATTEMPT, Optional.of(UPSTREAM_RETRY_AFTER), Duration.ofMillis(5));
|
||||
|
||||
// then
|
||||
assertThat(delay).isEqualTo(Duration.ofMillis(5));
|
||||
}
|
||||
|
||||
private static ExponentialFullJitterBackoff backoff(RetryAfterPolicy policy) {
|
||||
return new ExponentialFullJitterBackoff(
|
||||
BASE_BACKOFF, MAX_BACKOFF, JitterStrategy.NONE, policy, new SplittableRandom(0L));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user