Skip to content

Commit f99b284

Browse files
authored
Drain file watcher events during test setup (#24030)
1 parent 744c996 commit f99b284

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

crates/ty/tests/file_watching.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,24 @@ where
456456
root_dir: root_path,
457457
};
458458

459-
// Sometimes the file watcher reports changes for events that happened before the watcher was started.
460-
// Do a best effort at dropping these events.
461-
let _ =
462-
test_case.try_take_watch_changes(|_event: &ChangeEvent| true, Duration::from_millis(100));
459+
// Write a sentinel file to confirm the watcher is live and delivering events.
460+
// This
461+
// 1. ensures the watcher is working well, and not e.g. backed up with events unrelated to the current test
462+
// 2. flushes events that are unrelated to the current test
463+
let sentinel_path = project_path.join(".watcher_ready");
464+
std::fs::write(sentinel_path.as_std_path(), "ready")?;
465+
466+
test_case
467+
.try_take_watch_changes(event_for_file(".watcher_ready"), Duration::from_secs(30))
468+
.expect(
469+
"Watcher failed to deliver sentinel event within 30s \
470+
— file watching may not be operational",
471+
);
472+
473+
// Clean up the sentinel file and drain its deletion event.
474+
let _ = std::fs::remove_file(sentinel_path.as_std_path());
475+
let _ = test_case
476+
.try_take_watch_changes(event_for_file(".watcher_ready"), Duration::from_millis(500));
463477

464478
Ok(test_case)
465479
}

0 commit comments

Comments
 (0)