isReconnectAttemptResetEligible returned true on either a valid heartbeat/event or stableOpenMs elapsed. A server or proxy that sends one `:` comment and drops kept attemptIndex at 0, so the delay stayed in [0, baseDelay) and maxAttempts/maxElapsed were never reached (many clients reconnecting in lockstep). Both conditions are now required. The reconnect coordinator uses this helper at both close paths, so no coordinator change is needed. The two design docs that stated "or" now say "and". Existing tests encoded the old "either" rule (policy unit test and the coordinator "resets a spent retry" test) and are rewritten. RED: the policy test (heartbeat-only returned true) and the coordinator test (heartbeat on an instant close still reset: 3 attempts, expected 2) failed before the fix. Gates: architecture, types app/test, lint, adapter-inventory, test:unit, test:component pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
4.1 KiB
TypeScript
149 lines
4.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
calculateReconnectDelay,
|
|
defineReconnectPolicy,
|
|
isReconnectAttemptResetEligible,
|
|
parseRetryAfterDelay,
|
|
REALTIME_RECONNECT_CEILINGS,
|
|
reconnectBudgetRemaining,
|
|
type ReconnectPolicy,
|
|
} from "../../../src/adapters/realtime/reconnect-policy.ts";
|
|
|
|
const policy = defineReconnectPolicy({
|
|
baseDelayMs: 1_000,
|
|
maxDelayMs: 60_000,
|
|
maxAttempts: 10,
|
|
maxElapsedMs: 300_000,
|
|
stableOpenMs: 30_000,
|
|
});
|
|
|
|
describe("realtime reconnect policy", () => {
|
|
it("uses full jitter and treats a server hint as a not-before floor", () => {
|
|
expect(
|
|
calculateReconnectDelay({
|
|
policy,
|
|
attemptIndex: 2,
|
|
remainingElapsedMs: 10_000,
|
|
random: () => 0.5,
|
|
}),
|
|
).toBe(2_000);
|
|
expect(
|
|
calculateReconnectDelay({
|
|
policy,
|
|
attemptIndex: 2,
|
|
remainingElapsedMs: 10_000,
|
|
random: () => 0.5,
|
|
serverNotBeforeMs: 3_000,
|
|
}),
|
|
).toBe(3_000);
|
|
});
|
|
|
|
it("stops instead of clamping a hint past a hard or remaining budget", () => {
|
|
expect(
|
|
calculateReconnectDelay({
|
|
policy,
|
|
attemptIndex: 0,
|
|
remainingElapsedMs: 100_000,
|
|
random: () => 0,
|
|
serverNotBeforeMs: 60_001,
|
|
}),
|
|
).toBeNull();
|
|
expect(
|
|
calculateReconnectDelay({
|
|
policy,
|
|
attemptIndex: 0,
|
|
remainingElapsedMs: 3_000,
|
|
random: () => 0,
|
|
serverNotBeforeMs: 3_000,
|
|
}),
|
|
).toBeNull();
|
|
expect(
|
|
calculateReconnectDelay({
|
|
policy,
|
|
attemptIndex: 10,
|
|
remainingElapsedMs: 100_000,
|
|
random: () => 0,
|
|
}),
|
|
).toBeNull();
|
|
});
|
|
|
|
it("parses only bounded-shape Retry-After syntax for caller validation", () => {
|
|
expect(parseRetryAfterDelay("12", 0)).toBe(12_000);
|
|
expect(
|
|
parseRetryAfterDelay(
|
|
"Thu, 01 Jan 1970 00:00:20 GMT",
|
|
5_000,
|
|
),
|
|
).toBe(15_000);
|
|
expect(parseRetryAfterDelay("1.5", 0)).toBeNull();
|
|
expect(parseRetryAfterDelay("-1", 0)).toBeNull();
|
|
});
|
|
|
|
it("resets attempts only after a valid signal on a stable open", () => {
|
|
const eligible = (nowMs: number, observed: boolean) =>
|
|
isReconnectAttemptResetEligible({
|
|
policy,
|
|
openedAtMs: 10,
|
|
nowMs,
|
|
observedValidHeartbeatOrEvent: observed,
|
|
});
|
|
expect(eligible(29_000, false)).toBe(false);
|
|
expect(eligible(30_010, false)).toBe(false);
|
|
expect(eligible(11, true)).toBe(false);
|
|
expect(eligible(30_009, true)).toBe(false);
|
|
expect(eligible(30_010, true)).toBe(true);
|
|
expect(reconnectBudgetRemaining(policy, 1_000, 2_000)).toBe(
|
|
299_000,
|
|
);
|
|
expect(reconnectBudgetRemaining(policy, 2_000, 1_000)).toBe(0);
|
|
});
|
|
|
|
it("rejects policies above the implementation ceiling", () => {
|
|
expect(() =>
|
|
defineReconnectPolicy({
|
|
...policy,
|
|
maxAttempts: 11,
|
|
}),
|
|
).toThrow(TypeError);
|
|
});
|
|
|
|
it("keeps aborted-task drain below the absolute implementation ceiling", () => {
|
|
expect(REALTIME_RECONNECT_CEILINGS.drainTimeoutMs).toBe(2_000);
|
|
expect(
|
|
REALTIME_RECONNECT_CEILINGS.drainTimeoutMs,
|
|
).toBeLessThanOrEqual(
|
|
REALTIME_RECONNECT_CEILINGS.maxDrainTimeoutMs,
|
|
);
|
|
expect(
|
|
REALTIME_RECONNECT_CEILINGS.maxDrainTimeoutMs,
|
|
).toBe(30_000);
|
|
});
|
|
|
|
it("snapshots only exact own data properties", () => {
|
|
const inherited = Object.create(policy) as ReconnectPolicy;
|
|
const accessor = Object.defineProperties(
|
|
{},
|
|
{
|
|
baseDelayMs: { enumerable: true, value: 1_000 },
|
|
maxDelayMs: {
|
|
enumerable: true,
|
|
get: () => 60_000,
|
|
},
|
|
maxAttempts: { enumerable: true, value: 10 },
|
|
maxElapsedMs: { enumerable: true, value: 300_000 },
|
|
stableOpenMs: { enumerable: true, value: 30_000 },
|
|
},
|
|
) as ReconnectPolicy;
|
|
|
|
expect(() => defineReconnectPolicy(inherited)).toThrow(TypeError);
|
|
expect(() => defineReconnectPolicy(accessor)).toThrow(TypeError);
|
|
expect(() =>
|
|
defineReconnectPolicy({
|
|
...policy,
|
|
extra: true,
|
|
} as ReconnectPolicy),
|
|
).toThrow(TypeError);
|
|
});
|
|
});
|