Keep fixed 30-second safety polling with separate recovery polling, confirm persistent heartbeat gaps, and refresh shared history after durable writes.
114 lines
3.2 KiB
TypeScript
114 lines
3.2 KiB
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
createAuctionResyncRequester,
|
|
type AuctionResyncReply,
|
|
} from '../src/api/auction-resync.ts';
|
|
import type { ApiAuction } from '../src/api/types.ts';
|
|
|
|
const state = (revision: number) =>
|
|
({ revision, auctionLifecycleId: 'life', phase: 'bidding' }) as ApiAuction;
|
|
const noWait = async () => {};
|
|
|
|
test('coalesces simultaneous gaps into one snapshot request', async () => {
|
|
let finish!: (reply: AuctionResyncReply) => void;
|
|
let calls = 0;
|
|
const received: number[] = [];
|
|
const sync = createAuctionResyncRequester(
|
|
() => {
|
|
calls++;
|
|
return new Promise((resolve) => {
|
|
finish = resolve;
|
|
});
|
|
},
|
|
(value) => received.push(value.revision!),
|
|
noWait,
|
|
);
|
|
const requests = Array.from({ length: 100 }, () => sync.request(12, 'life'));
|
|
assert.equal(calls, 1);
|
|
finish({ ok: true, state: state(12) });
|
|
await Promise.all(requests);
|
|
assert.deepEqual(received, [12]);
|
|
});
|
|
|
|
test('fetches again when a newer gap arrived while the snapshot was in flight', async () => {
|
|
let finish!: (reply: AuctionResyncReply) => void;
|
|
let calls = 0;
|
|
const received: number[] = [];
|
|
const sync = createAuctionResyncRequester(
|
|
() =>
|
|
++calls === 1
|
|
? new Promise((resolve) => {
|
|
finish = resolve;
|
|
})
|
|
: Promise.resolve({ ok: true, state: state(14) }),
|
|
(value) => received.push(value.revision!),
|
|
noWait,
|
|
);
|
|
const pending = sync.request(12, 'life');
|
|
void sync.request(14, 'life');
|
|
void sync.request(); // A generic refresh cannot lower the required revision.
|
|
finish({ ok: true, state: state(12) });
|
|
await pending;
|
|
assert.deepEqual(received, [12, 14]);
|
|
assert.equal(calls, 2);
|
|
});
|
|
|
|
test('bounds retries and honors retry delay without rejoining the room', async () => {
|
|
let calls = 0;
|
|
const waits: number[] = [];
|
|
const sync = createAuctionResyncRequester(
|
|
async () => {
|
|
calls++;
|
|
return { ok: false, retryAfterMS: 1500 };
|
|
},
|
|
() => assert.fail('must not apply'),
|
|
async (ms) => {
|
|
waits.push(ms);
|
|
},
|
|
);
|
|
await sync.request();
|
|
assert.equal(calls, 3);
|
|
assert.deepEqual(waits, [1500, 1500]);
|
|
});
|
|
|
|
test('reports failure when all replies remain behind the required revision', async () => {
|
|
let failed = 0;
|
|
let applied = 0;
|
|
const sync = createAuctionResyncRequester(
|
|
async () => ({ ok: true, state: state(10) }),
|
|
() => {
|
|
applied++;
|
|
},
|
|
noWait,
|
|
() => {
|
|
failed++;
|
|
},
|
|
);
|
|
await sync.request(12, 'life');
|
|
assert.equal(applied, 3);
|
|
assert.equal(failed, 1);
|
|
});
|
|
|
|
test('drops an old connection reply after cancellation and permits a new request', async () => {
|
|
let finish!: (reply: AuctionResyncReply) => void;
|
|
let calls = 0;
|
|
const received: number[] = [];
|
|
const sync = createAuctionResyncRequester(
|
|
() =>
|
|
++calls === 1
|
|
? new Promise((resolve) => {
|
|
finish = resolve;
|
|
})
|
|
: Promise.resolve({ ok: true, state: state(20) }),
|
|
(value) => received.push(value.revision!),
|
|
noWait,
|
|
);
|
|
const stale = sync.request();
|
|
sync.cancel();
|
|
await sync.request();
|
|
finish({ ok: true, state: state(10) });
|
|
await stale;
|
|
assert.deepEqual(received, [20]);
|
|
});
|