Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cache-keepalive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ async function firePing(entry: KeepaliveEntry): Promise<void> {
entry.pingInFlight = false;
if (registry.get(entry.sessionId) !== entry) return;
const text = await resp.text().catch(() => "");
if (registry.get(entry.sessionId) !== entry) return;
if (!resp.ok) {
if (resp.status === 429) {
disarm(entry.sessionId, "rate-limited(429)");
Expand Down
31 changes: 31 additions & 0 deletions tests/cache-keepalive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,34 @@ noteSuccessfulForward({
fs.rmSync(tmp, { recursive: true, force: true });
assert.equal(exitCode, 0, `an armed keepalive must not hold the event loop open (got ${exitCode === null ? "TIMEOUT — the cadence timer kept the process alive" : `exit code ${exitCode}`})\n--- child output ---\n${out}`);
});

for (const reply of [
{ label: "rate limit", status: 429, body: { error: "slow down" } },
{ label: "cache miss", status: 200, body: MISS_BODY },
{ label: "cache hit", status: 200, body: HIT_BODY },
]) {
test(`ping: a stale ${reply.label} body cannot change a replacement snapshot`, async (t) => {
let finishBody!: (text: string) => void;
let startedBody!: () => void;
const body = new Promise<string>((resolve) => { finishBody = resolve; });
const started = new Promise<void>((resolve) => { startedBody = resolve; });
const response = new Response(null, { status: reply.status });
t.mock.method(response, "text", () => {
startedBody();
return body;
});
t.mock.method(globalThis, "fetch", async () => response);

noteSuccessfulForward(armInput());
timers.advance(270_000);
await started;
noteSuccessfulForward(armInput({ wireBody: BODY_1H }));
const replacement = _keepaliveEntryForTest(sid);
assert.equal(replacement?.tierLabel, "1h");

finishBody(JSON.stringify(reply.body));
await new Promise<void>((resolve) => setImmediate(resolve));
assert.deepEqual(_keepaliveEntryForTest(sid), replacement);
assert.equal(timers.pendingCount(), 1, "only the replacement snapshot stays scheduled");
});
}
Loading