test(watcher): fix fs.watch flake by adding settle delay#109
Open
andreinknv wants to merge 1 commit into
Open
Conversation
…write
Three watcher tests wrote files immediately after `watcher.start()`,
hitting a race with macOS FSEvents (and Linux inotify): there's a small
but real latency between `fs.watch()` returning and the kernel actually
delivering events. Under parallel test load (when the host CPU is busy
running other test files in worker threads), that latency balloons and
the file-change event is dropped before the watcher is fully registered.
Result: the test waits 5s for a sync that never fires and times out.
Affected:
- debounced sync > should trigger sync after file change
- callbacks > should call onSyncComplete after successful sync
- callbacks > should call onSyncError when sync throws
- debounced sync > should debounce rapid changes (less affected; its
50ms-spaced loop incidentally settles, but explicit is better)
- CodeGraph integration > should auto-sync when files change
Other tests in the same file already had a 400ms settle delay with a
comment ("Let watcher settle — fs.watch may fire residual events from
beforeEach") in the filtering tests. This PR factors that out into a
`letWatcherSettle()` helper and applies it consistently to every test
that writes immediately after `start()`.
No production code change. The flake had no user impact — it was purely
a test-order artifact under parallel load.
## Verification
- Pre-fix: ran `npm test` 8+ times across this session — fs.watch flake
fired in roughly 1 of 3 runs under parallel load.
- Post-fix: 3 consecutive `npm test` runs, 380/380 each, no flakes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced Apr 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three watcher tests wrote files immediately after
watcher.start(), hitting a race with macOS FSEvents (and Linux inotify): there's a small but real latency betweenfs.watch()returning and the kernel actually delivering events. Under parallel test load (when the host CPU is busy running other test files in worker threads), that latency balloons and the file-change event is dropped before the watcher is fully registered. Result: the test waits 5s for a sync that never fires and times out.Affected tests
debounced sync > should trigger sync after file change— most frequently affectedcallbacks > should call onSyncComplete after successful synccallbacks > should call onSyncError when sync throwsdebounced sync > should debounce rapid changes— less affected (its 50ms-spaced loop incidentally settles, but explicit is better)CodeGraph integration > should auto-sync when files changeWhat changed
The
filteringtests in this file already had a 400ms settle delay with a comment ("Let watcher settle — fs.watch may fire residual events from beforeEach"). This PR:letWatcherSettle()helper with a docstring explaining the FSEvents/inotify latency.start().Verification
npm test8+ times across the session that produced PRs fix(sync): detect HEAD-moving git operations to prevent stale index #100–perf(db): batch node lookups, fix insertNode cache, auto-ANALYZE after writes #108. The fs.watch flake fired in roughly 1 of 3 runs under parallel load.npm testruns, 380/380 each, zero flakes.🤖 Generated with Claude Code