Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

Fix flaky spacesd tests: mesh metering probe and 30 Hz presence timing (#5960)

cua-agents[bot]03eda7b130e8
diff --git a/libs/cua-spacesd/crates/cua-spacesd-desktop/Cargo.toml b/libs/cua-spacesd/crates/cua-spacesd-desktop/Cargo.toml
index 848832e73e..b35f0b945d 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-desktop/Cargo.toml
+++ b/libs/cua-spacesd/crates/cua-spacesd-desktop/Cargo.toml
@@ -110,6 +110,8 @@ windows = { version = "0.58", features = [
async-trait = "0.1"
tonic = { version = "0.14", features = ["transport"] }
tokio-tungstenite.workspace = true
+# Paused (virtual) time for timing tests.
+tokio = { workspace = true, features = ["test-util"] }
[target.'cfg(target_os = "linux")'.dev-dependencies]
# Independent decoder for the conformance tests' pixel oracle.
diff --git a/libs/cua-spacesd/crates/cua-spacesd-desktop/src/grpc/presence.rs b/libs/cua-spacesd/crates/cua-spacesd-desktop/src/grpc/presence.rs
index 6c10502063..53a7ede875 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-desktop/src/grpc/presence.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-desktop/src/grpc/presence.rs
@@ -26,7 +26,11 @@
use std::collections::{BTreeMap, HashMap};
use std::pin::Pin;
use std::sync::{Arc, Mutex, Weak};
-use std::time::{Duration, Instant, SystemTime};
+use std::time::{Duration, SystemTime};
+
+// Tokio's clock, so tests can run the hub on paused (virtual) time. Outside
+// a paused runtime it is the system monotonic clock.
+use tokio::time::Instant;
use cua_proto::env::v1::{
join_response, presence_service_server::PresenceService, CursorBatch, CursorMoved,
@@ -1407,14 +1411,18 @@ mod tests {
/// A 30 Hz sender reaches others about every cursor interval (50 ms),
/// not every other tick: a move that was not due is sent as soon as its
/// interval passes.
- #[tokio::test]
+ ///
+ /// Runs on tokio's paused clock, so the gaps are exact and do not depend
+ /// on how loaded the host is: the flusher and the test advance together
+ /// in virtual time.
+ #[tokio::test(start_paused = true)]
async fn a_30_hz_sender_is_relayed_at_the_cursor_interval() {
let hub = PresenceHub::new();
let (alice, _, _a) = hub.join(principal("alice", ""));
let (_, _, mut rx) = hub.join(principal("bob", ""));
let mut arrivals = Vec::new();
let started = Instant::now();
- let mut next = tokio::time::Instant::now();
+ let mut next = Instant::now();
for step in 0..45 {
hub.update_cursor(&alice.participant_id, at(f64::from(step) / 100.0, 0.5));
next += Duration::from_millis(33);
@@ -1429,30 +1437,15 @@ mod tests {
assert!(gaps.len() >= 20, "{} moves", arrivals.len());
let mean = gaps.iter().sum::<Duration>() / gaps.len() as u32;
// A due move waits at most one flusher step (a fifth of the
- // interval, or the host's timer granularity when that is coarser:
- // about 15.6 ms on Windows) past the interval. Whole-tick flushing
- // waited up to a whole interval (a ~67 ms mean on a 1 ms timer).
- let step = (CURSOR_INTERVAL / 5).max(timer_granularity().await);
- let bound = CURSOR_INTERVAL + step + Duration::from_millis(2);
+ // interval) past the interval. Whole-tick flushing waited up to a
+ // whole interval (a ~67 ms mean).
+ let bound = CURSOR_INTERVAL + CURSOR_INTERVAL / 5;
assert!(
mean < bound,
"mean gap {mean:?}, bound {bound:?} (was ~67 ms with whole-tick flushing)"
);
}
- /// How long this host actually sleeps for a 1 ms timer (the median of a
- /// few tries): about 1 ms on Linux and macOS, about 15.6 ms on Windows.
- async fn timer_granularity() -> Duration {
- let mut samples = Vec::new();
- for _ in 0..9 {
- let t = Instant::now();
- tokio::time::sleep(Duration::from_millis(1)).await;
- samples.push(t.elapsed());
- }
- samples.sort();
- samples[samples.len() / 2]
- }
-
fn flusher_running(hub: &PresenceHub) -> bool {
hub.flusher.lock().unwrap().running
}
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_metering.rs b/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_metering.rs
index e3c92928d4..05b7cc4add 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_metering.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_metering.rs
@@ -309,14 +309,28 @@ async fn mesh_sessions_count_against_the_allowance() {
.await
.unwrap();
// One call sets the path up; then wait for it to go direct, and for
- // the relayed handshake to fall out of the hours window.
- let r = w
- .http
- .get(format!("http://{}/probe", fwd.local))
- .send()
- .await
- .unwrap();
- assert_eq!(r.text().await.unwrap(), "ok");
+ // the relayed handshake to fall out of the hours window. Being in the
+ // map does not mean the machine's mesh node is serving yet: until it is,
+ // the forward drops the connection (a reset), so the first call retries
+ // until the machine answers. The answer itself is still asserted.
+ let probe = format!("http://{}/probe", fwd.local);
+ let mut answer = None;
+ for _ in 0..300 {
+ // A fresh client each try: never reuse a connection that was reset.
+ let fresh = reqwest::Client::builder()
+ .pool_max_idle_per_host(0)
+ .timeout(Duration::from_secs(5))
+ .build()
+ .unwrap();
+ if let Ok(r) = fresh.get(&probe).send().await {
+ if let Ok(text) = r.text().await {
+ answer = Some(text);
+ break;
+ }
+ }
+ tokio::time::sleep(Duration::from_millis(100)).await;
+ }
+ assert_eq!(answer.as_deref(), Some("ok"));
wait_until("a direct path", || {
let direct = matches!(ada.path(&peer.node_key), Some(Path::Direct(_)));
async move { direct }