Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions crates/core/src/trace/trace_artifacts_dispatch_budget_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -73,12 +75,7 @@ impl SystemOps for DeadlineConsumingScreenshotAdapter {
deadline: crate::Deadline,
) -> Result<ImageBuffer, AdapterError> {
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 {
Expand All @@ -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);
Expand All @@ -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,
);

Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/trace/trace_artifacts_more_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading