Skip to content

Commit 177ccdb

Browse files
committed
test(daemon-watcher): harden debounce + symlink-sed against macOS fs.watch delivery races
The CI run on a05766d (Node 22 matrix) failed on the daemon-watcher "multiple rapid edits are debounced" assertion. Investigation: the test counted exactly one reload, but macOS fs.watch is free to deliver events at any time — including after a previous event's debounce timer already fired — so under runner load a logical "burst" can legitimately split across two reloads even though the daemon's debounce is working. The test itself acknowledged this race in a comment but still asserted the strict count. - Replace the fixed 3500ms drain with a positive-quiescence helper (waitForDaemonQuiet) that waits until no new reload lines have been emitted for a window — drains stale fs.watch events from the previous test reliably. - Fire the burst synchronously (no inter-write awaits) so the writes leave the test in one event-loop tick. - Assert coalescing (1-2 reloads), not the brittle absolute count of 1. A daemon with broken debounce would still emit one reload per write (5), so the assertion still catches the real bug it pins. The sibling 'sed -i' symlink-replacement test had the same pre-existing race: a fixed 2s sleep that was sometimes shorter than rename-delivery + debounce + reload work under load. Switched to waitForDaemonLog polling (existing helper) and added quiescence before measuring. Verified locally across multiple back-to-back runs: the formerly-flaky debounce assertion is now stable. Other tests in the file remain timing-sensitive and would need similar treatment as separate work.
1 parent 30f6267 commit 177ccdb

1 file changed

Lines changed: 69 additions & 20 deletions

File tree

‎tests/daemon-watcher.test.ts‎

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,36 @@ function sleep(ms: number): Promise<void> {
152152
return new Promise((resolve) => setTimeout(resolve, ms));
153153
}
154154

155+
/**
156+
* Wait until the daemon log has been quiet (no new lines matching
157+
* `pattern`) for `quietForMs`, or fail after `timeoutMs`. Used before
158+
* tests that need to measure a fresh burst without late-arriving
159+
* fs.watch events from a previous burst polluting the count.
160+
*/
161+
async function waitForDaemonQuiet(
162+
pattern: RegExp,
163+
quietForMs = 1500,
164+
timeoutMs = 10_000
165+
): Promise<void> {
166+
const started = Date.now();
167+
let lastSeenCount = (getDaemonLog().join('\n').match(pattern) || []).length;
168+
let quietSince = Date.now();
169+
while (Date.now() - started < timeoutMs) {
170+
await sleep(150);
171+
const count = (getDaemonLog().join('\n').match(pattern) || []).length;
172+
if (count !== lastSeenCount) {
173+
lastSeenCount = count;
174+
quietSince = Date.now();
175+
continue;
176+
}
177+
if (Date.now() - quietSince >= quietForMs) return;
178+
}
179+
throw new Error(
180+
`Daemon log did not go quiet on ${pattern} within ${timeoutMs}ms ` +
181+
`(needed ${quietForMs}ms of silence). Most recent count: ${lastSeenCount}.`
182+
);
183+
}
184+
155185
async function waitForDaemonLog(
156186
predicate: (log: string) => boolean,
157187
message: string,
@@ -863,28 +893,43 @@ async function testWatcherSurvivesReloadCycle() {
863893
);
864894
});
865895

866-
await test('multiple rapid edits are debounced into single reload', async () => {
867-
// Let delayed fs.watch delivery from the previous edit drain before
868-
// measuring this burst. Under full-suite load macOS can deliver prior
869-
// changes well after the reload log, which makes the log count race the
870-
// debounce behavior this assertion is meant to pin down.
871-
await sleep(3500);
896+
await test('multiple rapid edits coalesce via the debounce', async () => {
897+
// The daemon uses a trailing debounce, but macOS fs.watch is free to
898+
// deliver each event whenever it likes — including after a previous
899+
// event's debounce timer has already fired. Under CI load that can
900+
// legitimately split one logical "burst" into two reloads, even
901+
// though no daemon code is misbehaving. So:
902+
//
903+
// 1. Wait for genuine quiescence (the log truly stopped emitting
904+
// reload lines) before measuring, not just a fixed sleep.
905+
// 2. Fire the burst synchronously — back-to-back writes, no awaits
906+
// between them, so they leave the test in one event-loop tick.
907+
// 3. Assert coalescing happened (count is << burst size). A daemon
908+
// with broken debounce would emit one reload per write (NUM_EDITS);
909+
// a working one collapses to 1 — but allow up to 2 to absorb the
910+
// OS-level event-delivery race, which is what was making this
911+
// test flake on Node 22 macOS runners.
912+
913+
const NUM_EDITS = 5;
914+
await waitForDaemonQuiet(/File changed, auto-reloading/);
872915
clearDaemonLog();
873916

874-
// Rapid-fire 5 edits inside the debounce window
875-
for (let i = 0; i < 5; i++) {
917+
for (let i = 0; i < NUM_EDITS; i++) {
876918
fs.appendFileSync(PHOTON_FILE, `\n// rapid-edit-${i}\n`);
877-
await new Promise((r) => setTimeout(r, 10));
878919
}
879920

880921
await waitForDaemonLog(
881922
(current) => current.includes('File changed, auto-reloading'),
882-
'Expected one debounced reload after rapid edits'
923+
'Expected at least one debounced reload after rapid edits'
883924
);
884-
await sleep(1500);
925+
// Give any trailing reload from a late-delivered event time to land.
926+
await sleep(2000);
885927
const log = getDaemonLog().join('\n');
886928
const reloadCount = (log.match(/File changed, auto-reloading/g) || []).length;
887-
assert.equal(reloadCount, 1, `Expected one debounced reload, got ${reloadCount}`);
929+
assert.ok(
930+
reloadCount >= 1 && reloadCount <= 2,
931+
`Expected the ${NUM_EDITS}-write burst to coalesce to 1-2 reloads, got ${reloadCount}`
932+
);
888933
});
889934
}
890935

@@ -987,20 +1032,24 @@ async function testSymlinkWatching() {
9871032
});
9881033

9891034
await test('file replacement via symlink target (sed -i) re-establishes watcher', async () => {
1035+
// Drain late events from the previous test before measuring this one,
1036+
// otherwise its stale "File changed" log can satisfy the assertion
1037+
// before our inode replacement is even delivered.
1038+
await waitForDaemonQuiet(/File changed, auto-reloading/);
9901039
clearDaemonLog();
9911040

992-
// Simulate sed -i on the REAL file: delete + recreate (new inode)
1041+
// Simulate sed -i on the REAL file: delete + recreate (new inode).
9931042
const content = fs.readFileSync(REAL_PHOTON_FILE, 'utf-8');
9941043
fs.unlinkSync(REAL_PHOTON_FILE);
9951044
fs.writeFileSync(REAL_PHOTON_FILE, content + '\n// inode-replaced-via-symlink\n');
9961045

997-
// macOS fs.watch rename events can be slow — give extra time
998-
await new Promise((r) => setTimeout(r, 2000));
999-
1000-
const log = getDaemonLog().join('\n');
1001-
assert.ok(
1002-
log.includes('File changed, auto-reloading'),
1003-
'Expected auto-reload after inode replacement of symlink target'
1046+
// macOS fs.watch rename delivery + 1s debounce + 250ms settle + reload
1047+
// work can comfortably exceed 2s under CI load. Poll instead of sleeping
1048+
// a fixed window.
1049+
await waitForDaemonLog(
1050+
(current) => current.includes('File changed, auto-reloading'),
1051+
'Expected auto-reload after inode replacement of symlink target',
1052+
10_000
10041053
);
10051054
});
10061055
}

0 commit comments

Comments
 (0)