Skip to content

Computer use CLI for AI agents

test: serialize every windows capture test on the single-flight slot (#327)

Lahfir392388dd4b9b
diff --git a/crates/windows/src/system/capture/capture_envelope_parity_tests.rs b/crates/windows/src/system/capture/capture_envelope_parity_tests.rs
index 6dced5f1d..63799f7b7 100644
--- a/crates/windows/src/system/capture/capture_envelope_parity_tests.rs
+++ b/crates/windows/src/system/capture/capture_envelope_parity_tests.rs
@@ -51,6 +51,7 @@ fn assert_disposition_wire(error: &agent_desktop_core::AdapterError, expected: D
#[test]
fn screenshot_failure_disposition_serializes_not_delivered() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let adapter = WindowsAdapter::new();
let fixture = LocalPatternFixture::create().expect("pattern fixture");
@@ -226,6 +227,7 @@ fn clipboard_get_set_clear_data_shapes_match_core_serialization() {
#[test]
fn non_one_scale_factor_capture_skipped_on_96dpi_only_hosts() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let displays = crate::system::display::list_displays_live(deadline(10_000)).expect("displays");
let Some(scaled) = displays
diff --git a/crates/windows/src/system/capture/capture_live_breadth_tests.rs b/crates/windows/src/system/capture/capture_live_breadth_tests.rs
index 0e9db08e7..01ba760d5 100644
--- a/crates/windows/src/system/capture/capture_live_breadth_tests.rs
+++ b/crates/windows/src/system/capture/capture_live_breadth_tests.rs
@@ -58,6 +58,7 @@ fn assert_pattern_samples(bgra: &[u8], width: u32, fixture: &LocalPatternFixture
#[test]
fn live_legacy_pattern_fixture_matches_and_invert_fails_when_stageable() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
if skip_unless_live_staging("legacy pattern capture") {
return;
diff --git a/crates/windows/src/system/capture/capture_test_support.rs b/crates/windows/src/system/capture/capture_test_support.rs
index a0e0a9054..1a390e7c5 100644
--- a/crates/windows/src/system/capture/capture_test_support.rs
+++ b/crates/windows/src/system/capture/capture_test_support.rs
@@ -24,16 +24,27 @@ use crate::tree::fixture::fixture_clipboard::clipboard_test_lock;
static HOME_ENV_LOCK: Mutex<()> = Mutex::new(());
static LIVE_GRAPHICS_CAPTURE_LOCK: Mutex<()> = Mutex::new(());
+const CAPTURE_IDLE_PATIENCE: std::time::Duration = std::time::Duration::from_secs(30);
+const CAPTURE_IDLE_POLL: std::time::Duration = std::time::Duration::from_millis(20);
/// Serializes every test that opens a Windows.Graphics.Capture session.
///
/// Concurrent sessions each create their own D3D11 device and frame pool on
/// one shared compositor; on a CI runner they stalled together for minutes,
/// so one session at a time keeps each test's deadline about its own work.
+///
+/// It also waits for a capture an earlier test abandoned at its deadline to
+/// release the process-wide single-flight slot, which otherwise refuses the
+/// next capture with `AppUnresponsive`.
pub(crate) fn live_graphics_capture_lock() -> MutexGuard<'static, ()> {
- LIVE_GRAPHICS_CAPTURE_LOCK
+ let guard = LIVE_GRAPHICS_CAPTURE_LOCK
.lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ let give_up = std::time::Instant::now() + CAPTURE_IDLE_PATIENCE;
+ while !super::native_call::capture_is_idle() && std::time::Instant::now() < give_up {
+ std::thread::sleep(CAPTURE_IDLE_POLL);
+ }
+ guard
}
pub(crate) struct HomeIsolation {
diff --git a/crates/windows/src/system/capture/capture_window_tests.rs b/crates/windows/src/system/capture/capture_window_tests.rs
index 41232bc38..6b976ef08 100644
--- a/crates/windows/src/system/capture/capture_window_tests.rs
+++ b/crates/windows/src/system/capture/capture_window_tests.rs
@@ -8,10 +8,12 @@ use std::time::Duration;
use crate::system::fixture::test_time::deadline;
+use crate::system::capture::capture_test_support::live_graphics_capture_lock;
use crate::system::capture::capture_test_support::sample_rgb;
#[test]
fn pattern_fixture_capture_matches_sampled_colours() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let fixture = LocalPatternFixture::create().expect("pattern fixture starts");
let image = capture_window(fixture.handle() as _, 1.0, deadline(10_000))
@@ -35,6 +37,7 @@ fn pattern_fixture_capture_matches_sampled_colours() {
#[test]
fn stalled_fixture_returns_app_unresponsive_without_hanging() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let stalled = StalledFixture::create().expect("stalled fixture starts");
let started = std::time::Instant::now();
@@ -53,6 +56,7 @@ fn stalled_fixture_returns_app_unresponsive_without_hanging() {
/// reported as a stale-identity style miss rather than `APP_UNRESPONSIVE`.
#[test]
fn destroyed_handle_is_not_reported_unresponsive() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let fixture = LocalPatternFixture::create().expect("pattern fixture starts");
let handle = fixture.handle();
@@ -87,6 +91,7 @@ fn printwindow_access_denied_maps_to_perm_denied() {
#[test]
fn minimized_windows_are_rejected_before_bitmap_alloc() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
gdi_balance::reset();
let fixture = LocalPatternFixture::create().expect("pattern fixture starts");
@@ -123,6 +128,7 @@ fn oversized_dimensions_are_rejected_while_ordinary_dimensions_are_accepted() {
#[test]
fn gdi_objects_balance_across_success_deadline_and_forced_failure() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
gdi_balance::reset();
let fixture = LocalPatternFixture::create().expect("pattern fixture starts");
@@ -157,6 +163,7 @@ fn gdi_objects_balance_across_success_deadline_and_forced_failure() {
#[test]
fn printwindow_retries_with_bare_flags_when_fullcontent_path_is_skipped() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let fixture = LocalPatternFixture::create().expect("pattern fixture starts");
bare_retry_observed::reset();
diff --git a/crates/windows/src/system/capture/native_call.rs b/crates/windows/src/system/capture/native_call.rs
index 3747fde8f..1bcf0faec 100644
--- a/crates/windows/src/system/capture/native_call.rs
+++ b/crates/windows/src/system/capture/native_call.rs
@@ -7,6 +7,12 @@ use std::thread;
static CAPTURE_ACTIVE: AtomicBool = AtomicBool::new(false);
+/// Whether no window capture holds the process-wide single-flight slot.
+#[cfg(test)]
+pub(crate) fn capture_is_idle() -> bool {
+ !CAPTURE_ACTIVE.load(Ordering::Acquire)
+}
+
pub(crate) fn capture<T: Send + 'static>(
deadline: Deadline,
label: &'static str,
diff --git a/crates/windows/src/system/capture/screenshot_tests.rs b/crates/windows/src/system/capture/screenshot_tests.rs
index 7cd0d4144..1eaf64493 100644
--- a/crates/windows/src/system/capture/screenshot_tests.rs
+++ b/crates/windows/src/system/capture/screenshot_tests.rs
@@ -31,6 +31,7 @@ fn live_token() -> String {
.expect("live token")
}
+use crate::system::capture::capture_test_support::live_graphics_capture_lock;
use crate::system::capture::capture_test_support::sample_rgb;
fn assert_png_metadata(image: &agent_desktop_core::ImageBuffer) {
@@ -42,6 +43,7 @@ fn assert_png_metadata(image: &agent_desktop_core::ImageBuffer) {
#[test]
fn four_targets_produce_png_buffers_via_legacy_fallback() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
backend_hooks::with_force_unsupported(|| {
let displays = list_displays_live(deadline(10_000)).expect("enumerate displays");
@@ -93,6 +95,7 @@ fn four_targets_produce_png_buffers_via_legacy_fallback() {
#[test]
fn exact_window_without_process_instance_is_invalid_args_not_delivered() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let fixture = LocalPatternFixture::create().expect("pattern fixture");
let info = window_info_for(fixture.handle(), None);
@@ -112,6 +115,7 @@ fn exact_window_without_process_instance_is_invalid_args_not_delivered() {
#[test]
fn post_capture_window_identity_failure_discards_bytes() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let fixture = LocalPatternFixture::create().expect("pattern fixture");
let info = window_info_for(fixture.handle(), Some(live_token()));
@@ -140,6 +144,7 @@ fn post_capture_window_identity_failure_discards_bytes() {
#[test]
fn modern_forced_fail_after_available_falls_back_to_legacy() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
backend_hooks::with_force_fail_after_available(|| {
let image = screenshot(ScreenshotTarget::Screen(0), deadline(10_000))
@@ -150,6 +155,7 @@ fn modern_forced_fail_after_available_falls_back_to_legacy() {
#[test]
fn modern_slice_reserves_floor_so_legacy_still_succeeds() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let fixture = LocalPatternFixture::create().expect("pattern fixture");
let info = window_info_for(fixture.handle(), Some(live_token()));
@@ -182,6 +188,7 @@ fn modern_slice_reserves_floor_so_legacy_still_succeeds() {
#[test]
fn window_scale_factor_comes_from_owning_display() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let fixture = LocalPatternFixture::create().expect("pattern fixture");
let info = window_info_for(fixture.handle(), Some(live_token()));
@@ -209,6 +216,7 @@ fn window_scale_factor_comes_from_owning_display() {
#[test]
fn window_identity_mismatch_is_not_delivered_through_the_entry_point() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let fixture = LocalPatternFixture::create().expect("pattern fixture");
let mut info = window_info_for(fixture.handle(), Some(live_token()));
@@ -225,6 +233,7 @@ fn window_identity_mismatch_is_not_delivered_through_the_entry_point() {
#[test]
fn display_identity_mismatch_is_not_delivered_through_the_entry_point() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let primary = display_at(0, deadline(10_000)).expect("primary");
let mut stale = primary.clone();
@@ -274,6 +283,7 @@ fn screenshot_entry_upgrades_only_unknown_disposition_to_not_delivered() {
}
#[test]
fn invalid_display_index_is_not_delivered_through_the_entry_point() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let error = screenshot(ScreenshotTarget::Screen(9_999), deadline(10_000))
.expect_err("an out-of-range display index must fail");
@@ -286,6 +296,7 @@ fn invalid_display_index_is_not_delivered_through_the_entry_point() {
#[test]
fn minimized_window_capture_is_not_delivered_through_the_entry_point() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let fixture = LocalPatternFixture::create().expect("pattern fixture");
let handle = fixture.handle() as windows_sys::Win32::Foundation::HWND;
@@ -311,6 +322,7 @@ fn minimized_window_capture_is_not_delivered_through_the_entry_point() {
#[test]
fn stalled_window_capture_is_not_delivered_through_the_entry_point() {
+ let _capture = live_graphics_capture_lock();
bootstrap();
let stalled = StalledFixture::create().expect("stalled fixture");
let info = window_info_for(stalled.handle(), Some(live_token()));
@@ -328,6 +340,7 @@ fn stalled_window_capture_is_not_delivered_through_the_entry_point() {
#[test]
fn adapter_screenshot_delegates_to_orchestration() {
+ let _capture = live_graphics_capture_lock();
use agent_desktop_core::SystemOps;
bootstrap();