Skip to content

Commit 53653a7

Browse files
claude[bot]github-actions[bot]claude
authored
test: replace fixed-delay sleep-then-assert races in subscriptionReplay (#1219)
Convert the "await delay(N); assert(side effect landed)" timing races in unitTests/resources/subscriptionReplay.test.js to condition-waits via the shared waitFor() helper. These fixed waits raced loaded CI runners and are an instance of the flakiness tracked in #1138. Legitimate sleeps that model elapsed time (subscribe-then-write gaps, collect() quiet periods) are left as-is. Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
1 parent a1493e8 commit 53653a7

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

unitTests/resources/subscriptionReplay.test.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require('../testUtils');
22
const assert = require('assert');
33
const { setTimeout: delay } = require('timers/promises');
44
const { setupTestDBPath } = require('../testUtils');
5+
const { waitFor } = require('../waitFor.js');
56
const { table } = require('#src/resources/databases');
67
const { setMainIsWorker } = require('#js/server/threads/manageThreads');
78
require('#src/server/serverHelpers/serverUtilities');
@@ -326,7 +327,14 @@ describe('Subscription replay', () => {
326327
const events = [];
327328
subscription.on('data', (e) => events.push(e));
328329
await Promise.all(inFlight);
329-
await delay(300);
330+
// Wait for every in-flight write to be delivered rather than guessing a fixed
331+
// duration — the fixed wait raced loaded runners and was the source of the
332+
// intermittent "missing id" failures.
333+
await waitFor(() => {
334+
const seen = new Set(events.map((e) => e.id));
335+
for (let i = 0; i < 200; i++) if (!seen.has(20000 + i)) return false;
336+
return true;
337+
});
330338
subscription.return?.();
331339

332340
const ids = new Set(events.map((e) => e.id));
@@ -493,7 +501,12 @@ describe('Subscription replay', () => {
493501
const events = [];
494502
subscription.on('data', (e) => events.push(e));
495503
await Promise.all(inFlight);
496-
await delay(300);
504+
// Wait for the actual delivery condition instead of a fixed sleep.
505+
await waitFor(() => {
506+
const seen = new Set(events.map((e) => e.id));
507+
for (let i = 0; i < 200; i++) if (!seen.has(13000 + i)) return false;
508+
return true;
509+
});
497510
subscription.return?.();
498511

499512
const ids = new Set(events.map((e) => e.id));
@@ -549,7 +562,7 @@ describe('Subscription replay', () => {
549562
// the production timing: subscribe is established, then a live write arrives
550563
await delay(50);
551564
await T.put(50000, { name: 'single' });
552-
await delay(300);
565+
await waitFor(() => events.length >= 1);
553566
subscription.return?.();
554567

555568
assert.equal(events.length, 1, `expected 1 event for the post-subscribe write, got ${events.length}`);
@@ -714,7 +727,7 @@ describe('Subscription replay', () => {
714727
for (let i = 0; i < 5; i++) {
715728
await T.put(100 + i, { name: 'live' + i });
716729
}
717-
await delay(150);
730+
await waitFor(() => events.length >= 5);
718731
subscription.return?.();
719732

720733
assert.equal(events.length, 5, `expected 5 live events, got ${events.length}`);
@@ -776,7 +789,7 @@ describe('Subscription replay', () => {
776789
for (let i = 0; i < 3; i++) {
777790
await Ours.put(30000 + i, { name: 'real' + i });
778791
}
779-
await delay(200);
792+
await waitFor(() => events.length >= 3);
780793
subscription.return?.();
781794

782795
// the cursor iterated 50 audit records, all skipped (different table); history is empty.
@@ -972,8 +985,9 @@ describe('Subscription replay', () => {
972985
resolved = true;
973986
});
974987
await T.put(40000, { name: 'wake_me' });
975-
// give the committed listener + microtask a tick to resolve
976-
await delay(50);
988+
// Wait for the committed listener to resolve the waiter rather than sleeping a fixed
989+
// amount and hoping it landed.
990+
await waitFor(() => resolved);
977991
assert.equal(
978992
resolved,
979993
true,

0 commit comments

Comments
 (0)