Files
clean-architecture-frontend…/tests/unit/retry-after-delay.test.ts
DongHyeonka 8d1c45c307 refactor(http): Retry-After 재시도 지연 규칙을 kernel로 옮긴다
문제: 힌트를 하한으로 두고 jitter를 더하는 규칙이 HTTP V3 안에만
있다. 다른 adapter 넷은 같은 계산을 따로 하고 jitter가 빠져 있다.

고른 방법: kernel retry-after.ts에 delayAfterRetryHint를 두고
retryDelayFor가 부른다. 상한·로컬 backoff·난수 검사는 부르는 쪽이
갖는다. 남은 예산을 건네면 jitter 창을 힌트 뒤 여유의 절반까지로
줄이는 칸을 둔다(HTTP는 쓰지 않는다).
버린 대안: adapter마다 jitter만 고친다. 여섯 번째 복사본이 생기고
상한 초과 때 포기와 자르기가 다시 갈릴 수 있다.

위험·한계: 동작은 같다. 상한을 넘는 힌트에서도 random을 한 번
부르는 것만 다르다.

검증: desktop에서 옛 구현과 retryIndex 0~5, 힌트 null·0~5100, 표본
101개, 모두 3,091,812 조합을 대조해 차이 0. kernel 새 시험과 HTTP
재시도 시험 87 tests 통과.

Refs: REV-FE-0024
2026-11-22 11:06:06 +09:00

51 lines
1.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { delayAfterRetryHint } from "../../src/adapters/platform/retry-after.ts";
const delay = (
overrides: Partial<Parameters<typeof delayAfterRetryHint>[0]>,
) =>
delayAfterRetryHint({
hintMs: 3_000,
ceilingMs: 60_000,
localDelayMs: 100,
localWindowMs: 250,
sample: 0,
...overrides,
});
describe("delay after a server retry hint", () => {
it("spreads the same hint without retrying before it", () => {
expect(delay({ sample: 0 })).toBe(3_000);
// The window is max(250, 3000 / 4).
expect(delay({ sample: 0.999 })).toBe(3_749);
});
it("uses the local window when it is wider than a quarter of the hint", () => {
expect(delay({ localWindowMs: 1_000, sample: 0.5 })).toBe(3_500);
});
it("bounds the window whatever the hint and the local backoff are", () => {
expect(
delay({ hintMs: 60_000, localWindowMs: 60_000, sample: 0.999 }),
).toBe(61_998);
});
it("refuses a hint past the ceiling instead of cutting it", () => {
expect(delay({ hintMs: 60_000 })).toBe(60_000);
expect(delay({ hintMs: 60_001 })).toBeNull();
});
it("keeps the local delay when there is no hint or it is later", () => {
expect(delay({ hintMs: null, sample: 0.999 })).toBe(100);
expect(delay({ hintMs: 0, localWindowMs: 100, sample: 0.5 })).toBe(100);
expect(delay({ localDelayMs: 9_000, sample: 0.999 })).toBe(9_000);
});
it("leaves at least half of the budget after the hint to the retry", () => {
expect(delay({ remainingMs: 3_100, sample: 0.999 })).toBe(3_049);
// A spent budget adds no negative jitter: the caller sees the hint and stops.
expect(delay({ remainingMs: 2_000, sample: 0.999 })).toBe(3_000);
});
});