From e972d52d793ceb86889c07b15cf1547e48c5449a Mon Sep 17 00:00:00 2001 From: flash2do Date: Sun, 8 Nov 2026 08:29:36 -0800 Subject: [PATCH] test: run two trace budget tests on the virtual clock so a slow runner cannot flake them Both tests asserted budgets that real time could spend. On the Windows CI runner the real-time budget was consumed by file I/O and oversleeping, not by the property under test. slow_pre_capture_preserves_dispatch_budget gave the whole action 500 ms of wall clock and slept 50 ms for real inside the pre capture. It now freezes the virtual clock, makes the capture consume its entire capped deadline (400 ms) and asserts the dispatch receives exactly the 100 ms reserve, which is stricter than before. artifact_capture_budget_supports_deadline_bound_screenshot_backends lost a capture when opening the artifact lock file took longer than the fixed 50 ms lock wait, which is real time on a slow runner. It now runs on the virtual clock, so the lock wait cannot expire and the 250 ms backend minimum is checked against exact remaining budgets. --- .../trace_artifacts_dispatch_budget_tests.rs | 25 +++++++++++++------ .../src/trace/trace_artifacts_more_tests.rs | 1 + 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/crates/core/src/trace/trace_artifacts_dispatch_budget_tests.rs b/crates/core/src/trace/trace_artifacts_dispatch_budget_tests.rs index 624caf17ee..511614fe17 100644 --- a/crates/core/src/trace/trace_artifacts_dispatch_budget_tests.rs +++ b/crates/core/src/trace/trace_artifacts_dispatch_budget_tests.rs @@ -12,6 +12,8 @@ use std::time::Duration; const ACTION_BUDGET_MS: u64 = 3_000; const CAPTURE_LOCK_WAIT_MS: u64 = 50; const CAPTURES_PER_ACTION: u64 = 2; +const SLOW_CAPTURE_ACTION_BUDGET_MS: u64 = 500; +const PRE_CAPTURE_DISPATCH_RESERVE_MS: u64 = 100; struct DeadlineConsumingScreenshotAdapter { dispatch_calls: AtomicU32, @@ -73,12 +75,7 @@ impl SystemOps for DeadlineConsumingScreenshotAdapter { deadline: crate::Deadline, ) -> Result { if self.consume_pre_deadline && self.screenshot_calls.fetch_add(1, Ordering::SeqCst) == 0 { - let delay = if deadline.was_capped() { - std::time::Duration::from_millis(50) - } else { - deadline.remaining() - }; - std::thread::sleep(delay); + crate::wait::deadline::sleep(deadline.remaining()); return Err(deadline.timeout_error()); } Ok(ImageBuffer { @@ -91,9 +88,14 @@ impl SystemOps for DeadlineConsumingScreenshotAdapter { } } +/// The pre capture spends every millisecond its own deadline allows, on a +/// virtual clock so no scheduler delay can move the numbers. What must survive +/// is the dispatch reserve: an uncapped capture deadline would spend the whole +/// action budget and the dispatch would time out. #[test] fn slow_pre_capture_preserves_dispatch_budget() { let (_home, _lock) = setup_artifacts_test(); + let clock = VirtualClock::freeze(); let manifest = artifacts_session(); let context = CommandContext::new(Some(manifest.id), None, false).unwrap(); let adapter = DeadlineConsumingScreenshotAdapter::new(true); @@ -106,7 +108,7 @@ fn slow_pre_capture_preserves_dispatch_budget() { ref_id: "@e1", context: &context, }, - ActionRequest::headless(Action::Click).with_timeout_ms(Some(500)), + ActionRequest::headless(Action::Click).with_timeout_ms(Some(SLOW_CAPTURE_ACTION_BUDGET_MS)), crate::ref_action::ref_action_single::dispatch_resolved, ); @@ -115,6 +117,15 @@ fn slow_pre_capture_preserves_dispatch_budget() { "slow trace capture blocked dispatch: {result:?}" ); assert_eq!(adapter.dispatch_calls.load(Ordering::SeqCst), 1); + assert_eq!( + adapter.dispatch_budget_ms.load(Ordering::SeqCst), + PRE_CAPTURE_DISPATCH_RESERVE_MS, + "the dispatch must get exactly the reserve the slow capture left it" + ); + assert_eq!( + clock.elapsed(), + Duration::from_millis(SLOW_CAPTURE_ACTION_BUDGET_MS - PRE_CAPTURE_DISPATCH_RESERVE_MS) + ); } fn trace_body(trace_dir: &std::path::Path) -> String { diff --git a/crates/core/src/trace/trace_artifacts_more_tests.rs b/crates/core/src/trace/trace_artifacts_more_tests.rs index 351c08f99d..0c6b87a2c1 100644 --- a/crates/core/src/trace/trace_artifacts_more_tests.rs +++ b/crates/core/src/trace/trace_artifacts_more_tests.rs @@ -49,6 +49,7 @@ fn artifacts_full_captures_pre_and_post_pngs() { #[test] fn artifact_capture_budget_supports_deadline_bound_screenshot_backends() { let (_home, _lock) = setup_artifacts_test(); + let _clock = crate::wait::deadline::VirtualClock::freeze(); let manifest = artifacts_session(); let context = CommandContext::new(Some(manifest.id.clone()), None, false).unwrap(); run_ref_action(&context, &deadline_png_adapter(250), 42).unwrap();