Deterministic Retry Test Harness
A test harness that drives retry logic with scripted outcomes and a fake clock, so retry behaviour is verified without real delays.
Code
export type Outcome<T> = { readonly ok: true; readonly value: T } | { readonly ok: false; readonly error: Error };
export function ok<T>(value: T): Outcome<T> {
return { ok: true, value };
}
export function fail(message: string): Outcome<never> {
return { ok: false, error: new Error(message) };
}
export interface AttemptLog {
readonly index: number;
readonly at: number;
readonly result: "ok" | "error";
}
export interface RetryHarness<T> {
readonly attempts: AttemptLog[];
readonly loader: () => Promise<T>;
readonly exhausted: () => boolean;
}
export function createHarness<T>(
script: readonly Outcome<T>[],
clock: { now: () => number; setTimeout: (fn: () => void, ms: number) => unknown },
): RetryHarness<T> {
const attempts: AttemptLog[] = [];
let index = 0;
const loader = async (): Promise<T> =>
new Promise<T>((resolve, reject) => {
const outcome = script[index];
attempts.push({
index,
at: clock.now(),
result: outcome.ok ? "ok" : "error",
});
index += 1;
if (outcome.ok) resolve(outcome.value);
else reject(outcome.error);
});
return {
attempts,
loader,
exhausted: () => index >= script.length,
};
}Testing retry with real timers makes the suite slow and non-deterministic, so retry bugs ship unnoticed.
Use case: engineers testing retry policies against flaky dependencies
Inputs / outputs
- Input: a scripted list of outcomes (ok or error) and a clock the policy should use
- Output: the call log and final result, letting assertions cover attempt count and delays
Error conditions
- script exhausted before the policy stops
- policy returns before consuming the script
Approach
a scripted loader returns outcomes in order and records calls; the policy receives the harness clock so delays are virtual and assertions are exact.
Implementation
export type Outcome<T> = { readonly ok: true; readonly value: T } | { readonly ok: false; readonly error: Error };
export function ok<T>(value: T): Outcome<T> {
return { ok: true, value };
}
export function fail(message: string): Outcome<never> {
return { ok: false, error: new Error(message) };
}
export interface AttemptLog {
readonly index: number;
readonly at: number;
readonly result: "ok" | "error";
}
export interface RetryHarness<T> {
readonly attempts: AttemptLog[];
readonly loader: () => Promise<T>;
readonly exhausted: () => boolean;
}
export function createHarness<T>(
script: readonly Outcome<T>[],
clock: { now: () => number; setTimeout: (fn: () => void, ms: number) => unknown },
): RetryHarness<T> {
const attempts: AttemptLog[] = [];
let index = 0;
const loader = async (): Promise<T> =>
new Promise<T>((resolve, reject) => {
const outcome = script[index];
attempts.push({
index,
at: clock.now(),
result: outcome.ok ? "ok" : "error",
});
index += 1;
if (outcome.ok) resolve(outcome.value);
else reject(outcome.error);
});
return {
attempts,
loader,
exhausted: () => index >= script.length,
};
}
Verification tests
import { test } from "node:test";
import assert from "node:assert/strict";
import { createHarness, fail, ok } from "./example.ts";
function fakeClock() {
let t = 0;
return {
now: () => t,
setTimeout: (_fn: () => void, ms: number) => {
t += ms;
return 0;
},
};
}
test("replays a success after two failures", async () => {
const harness = createHarness([fail("down"), fail("down"), ok("ready")], fakeClock());
const seen: string[] = [];
for (let i = 0; i < 3; i++) {
try {
seen.push(String(await harness.loader()));
} catch (err) {
seen.push("err:" + (err as Error).message);
}
}
assert.deepEqual(seen, ["err:down", "err:down", "ready"]);
assert.deepEqual(
harness.attempts.map((a) => a.result),
["error", "error", "ok"],
);
});
test("stops once the script is exhausted", async () => {
const harness = createHarness([ok(1)], fakeClock());
await harness.loader();
assert.equal(harness.exhausted(), true);
});
test("records attempt indices in order", async () => {
const harness = createHarness([fail("a"), fail("b"), ok(3)], fakeClock());
for (let i = 0; i < 3; i++) {
await harness.loader().catch(() => undefined);
}
assert.deepEqual(
harness.attempts.map((a) => a.index),
[0, 1, 2],
);
});
Dependencies and running
No external dependencies (standard library only).
Runtime: node >= 18 Entry point: node –experimental-strip-types example.ts
Limitations and error handling
synchronous script replay; no concurrency simulation; no randomised fuzzing.
Verification boundary
execution tests ran offline in an isolated temp dir with a sanitized environment; no network, no writes outside the sandbox
Interactive Tools for This Topic
Report a problem with this exampleOpens a pre-filled issue on GitHub.