From 6e5e482410ae1d173c7df3ff63afa90c4ce01941 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 1 Dec 2026 02:20:22 +0800 Subject: [PATCH] test(memory): make the debounce test tolerate a loaded machine (#7098) memory::tests::a_burst_of_calls_runs_once_after_the_delay failed on a loaded macOS runner: with a 100 ms delay and 20 ms gaps, oversleeping let the debounced call fire before the burst ended. The test now uses a 500 ms delay, checks "nothing ran yet" only when less than the delay has passed since the last call, waits for the run, and asserts exactly one run only when no gap in the burst reached the delay. --- crates/ide-core/src/memory.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/ide-core/src/memory.rs b/crates/ide-core/src/memory.rs index 18d6651b08..61f59cc295 100644 --- a/crates/ide-core/src/memory.rs +++ b/crates/ide-core/src/memory.rs @@ -63,13 +63,30 @@ mod tests { #[test] fn a_burst_of_calls_runs_once_after_the_delay() { + // Timing-based: a loaded CI machine can oversleep, so each check allows for that. + let delay = Duration::from_millis(500); + let (mut last, mut widest_gap) = (None::, Duration::ZERO); for _ in 0..5 { - debounce(&DUE, Duration::from_millis(100), count); + debounce(&DUE, delay, count); + let now = Instant::now(); + widest_gap = widest_gap.max(last.map_or(Duration::ZERO, |l| now - l)); + last = Some(now); std::thread::sleep(Duration::from_millis(20)); } - assert_eq!(RUNS.load(SeqCst), 0); - std::thread::sleep(Duration::from_millis(400)); - assert_eq!(RUNS.load(SeqCst), 1); + if last.unwrap().elapsed() < delay { + assert_eq!(RUNS.load(SeqCst), 0, "ran before the delay passed"); + } + let deadline = Instant::now() + Duration::from_secs(10); + while RUNS.load(SeqCst) == 0 && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(10)); + } + // A second run would come within another delay. + std::thread::sleep(delay); + if widest_gap < delay { + assert_eq!(RUNS.load(SeqCst), 1); + } else { + assert!(RUNS.load(SeqCst) >= 1); + } release_free_pages_soon(); } }