Files
clean-architecture-frontend…/tests/unit/retry-policy.test.ts
T
DongHyeonkaandClaude Opus 5.5 db1bc1fc7e fix(http): retry 408/425 server failures and parse Retry-After with the kernel (R3-1)
- retry-policy: a SERVER_FAILURE with status 408/425 follows the
  502/503/504 rule (safe or keyed only; never idempotency "none" or
  retry "never"). Retry-After is honored and its 30s ceiling applies to
  any failure that carries it, not only RATE_LIMITED.
- client: Retry-After is parsed by platform/retry-after.ts for
  408/425/429, on every failure path (gateway bodies included).
  The local parseRetryAfterHeader is removed.

RED (before the fix, 15 failing):
- 408/425 gateway body on a safe GET: expected 3 fetches, got 1
- keyed command on 408/425: expected 3 fetches, got 1
- 408/425 with Retry-After: 2: expected ok after a 2000ms wait, got the
  first failure
- 408/425 with Retry-After: 31: retryAfterMs missing on the failure
- 429 Retry-After "1.5": slept 1500ms instead of the 125ms backoff;
  "Thursday, 01-Jan-70 00:00:05 GMT": slept 5000ms; "1e3", "0x10",
  "-1", "2099-01-01T00:00:00Z": accepted by the old parser, so the
  request was not retried with the 125ms backoff
- unit shouldRetry(safe, SERVER_FAILURE 408): false
Non-idempotent command on 408/425 stays 1 fetch (pinned, passed before
and after).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 15:16:56 +09:00

82 lines
2.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
calculateBackoff,
parseRetryAfter,
retryDelay,
shouldRetry,
} from "../../src/adapters/http/retry-policy.ts";
describe("HTTP retry policy", () => {
it("uses capped exponential full jitter", () => {
expect(calculateBackoff(0, () => 0.5)).toBe(125);
expect(calculateBackoff(8, () => 1)).toBe(2_000);
});
it("parses retry-after and chooses the longer bounded delay", () => {
expect(parseRetryAfter("2", 0)).toBe(2_000);
expect(parseRetryAfter(" 2 ", 0)).toBe(2_000);
expect(parseRetryAfter("Thu, 01 Jan 1970 00:00:05 GMT", 1_000)).toBe(
4_000,
);
expect(retryDelay({ kind: "RATE_LIMITED", retryAfterMs: 500 }, 0, () => 0)).toBe(
500,
);
});
it("rejects Retry-After values that are not delta-seconds or an HTTP date", () => {
for (const value of ["", " ", "1e3", "1.5", "-1", "0x10", "2024-01-01"]) {
expect(parseRetryAfter(value, 0)).toBeNull();
}
});
it("allows at most two retries for safe or keyed requests", () => {
const failure = { kind: "SERVER_FAILURE" };
expect(shouldRetry({ idempotency: "safe" }, failure, 0)).toBe(true);
expect(shouldRetry({ idempotency: "keyed" }, failure, 1)).toBe(true);
expect(shouldRetry({ idempotency: "safe" }, failure, 2)).toBe(false);
expect(shouldRetry({ idempotency: "none" }, failure, 0)).toBe(false);
expect(
shouldRetry(
{ idempotency: "safe" },
{ kind: "SERVER_FAILURE", httpStatus: 500 },
0,
),
).toBe(false);
});
// R3-1. 408/425 server failures share the 502/503/504 rule and honor
// Retry-After with the same 30s ceiling.
it("retries 408 and 425 server failures only for safe or keyed requests", () => {
for (const httpStatus of [408, 425]) {
const failure = { kind: "SERVER_FAILURE", httpStatus };
expect(shouldRetry({ idempotency: "safe" }, failure, 0)).toBe(true);
expect(shouldRetry({ idempotency: "keyed" }, failure, 0)).toBe(true);
expect(shouldRetry({ idempotency: "none" }, failure, 0)).toBe(false);
expect(
shouldRetry({ idempotency: "safe", retry: "never" }, failure, 0),
).toBe(false);
expect(
retryDelay({ ...failure, retryAfterMs: 1_500 }, 0, () => 0),
).toBe(1_500);
expect(
shouldRetry(
{ idempotency: "safe" },
{ ...failure, retryAfterMs: 31_000 },
0,
),
).toBe(false);
}
});
it("does not automatically wait beyond 30 seconds", () => {
expect(
shouldRetry(
{ idempotency: "safe" },
{ kind: "RATE_LIMITED", retryAfterMs: 31_000 },
0,
),
).toBe(false);
});
});