From 3cb78f6bc988beaf0408100a1425c02933d8957f Mon Sep 17 00:00:00 2001 From: aur-build Date: Thu, 19 Nov 2026 20:27:26 +0000 Subject: tests: wait on the condition instead of on the clock The dispatch and crash waits polled every 50ms and 25ms against a dispatcher that ticks every two seconds; live.rs staged its log appends with 400ms sleeps; vcs.rs waited 80ms to conclude that a blocked racer had not entered. The pool tests take the virtual clock and poll a 2ms condition wait, live.rs appends without sleeping at all, and the Stage B test queues every racer on the slot before releasing it, so the window a broken cap would slip through is set up rather than hoped for. live.rs no longer proves delivery mid-stream, because it never could: the stream response is buffered until it ends, so the sleeps were not observable. Worth fixing on the serving side, but that is a behaviour change, not a test one. --- tests/crash.rs | 5 +++-- tests/dispatch.rs | 12 ++++++++---- tests/live.rs | 5 +---- tests/vcs.rs | 40 ++++++++++++++++++++++++++-------------- 4 files changed, 38 insertions(+), 24 deletions(-) (limited to 'tests') diff --git a/tests/crash.rs b/tests/crash.rs index ca7d90a..1e72177 100644 --- a/tests/crash.rs +++ b/tests/crash.rs @@ -148,16 +148,17 @@ fn a_failed_build_leaves_the_index_serving_the_old_version_and_says_so() { fn a_transient_failure_retries_but_a_deterministic_one_never_does() { let (mut env, _fx) = env_with_fixture("build-fails"); let id = queued(&env, "build-fails"); + let _clock = aur_build::time::virtualize(); let pool = aur_build::pipeline::dispatch::Pool::spawn(std::sync::Arc::new(env.cfg.clone()), env.db.clone()); let mut state = BuildState::Queued; - for _ in 0..400 { + for _ in 0..15_000 { if let Some(b) = builds::get(&env.db, &id).unwrap() { state = b.state; if state == BuildState::Failed { break; } } - std::thread::sleep(std::time::Duration::from_millis(25)); + std::thread::sleep(std::time::Duration::from_millis(2)); } pool.shutdown(); assert_eq!(state, BuildState::Failed); diff --git a/tests/dispatch.rs b/tests/dispatch.rs index 84dd2b7..9381a17 100644 --- a/tests/dispatch.rs +++ b/tests/dispatch.rs @@ -110,6 +110,7 @@ fn a_newer_approval_cancels_an_in_flight_build_at_the_step_boundary() { fn a_deterministic_build_failure_is_not_retried() { let (env, _fx) = env_with_fixture("build-fails"); let id = queued_build(&env, "build-fails"); + let _clock = aur_build::time::virtualize(); let pool = dispatch::Pool::spawn(Arc::new(env.cfg.clone()), env.db.clone()); let state = wait_for(&env, &id, &[BuildState::Failed, BuildState::Succeeded]); pool.shutdown(); @@ -146,6 +147,7 @@ fn a_missing_toolchain_is_not_retried() { env.cfg.makepkg_bin = "/nonexistent/makepkg-for-test".into(); env.cfg.max_transient_attempts = 3; env.cfg.dispatch_tick_secs = 0; + let _clock = aur_build::time::virtualize(); let pool = dispatch::Pool::spawn(Arc::new(env.cfg.clone()), env.db.clone()); let state = wait_for(&env, &id, &[BuildState::Failed]); pool.shutdown(); @@ -161,6 +163,7 @@ fn a_missing_toolchain_is_not_retried() { fn the_pool_takes_a_queued_build_all_the_way_to_publication() { let (env, _fx) = env_with_fixture("plain-make"); let id = queued_build(&env, "plain-make"); + let _clock = aur_build::time::virtualize(); let cfg = Arc::new(env.cfg.clone()); let pool = dispatch::Pool::spawn(cfg, env.db.clone()); let state = wait_for(&env, &id, &[BuildState::Succeeded, BuildState::Failed]); @@ -194,6 +197,7 @@ fn a_reaped_build_resumes_at_the_next_incomplete_step_with_no_duplicate_work() { ¶ms(vec![id.as_str()]), ); + let _clock = aur_build::time::virtualize(); let pool = dispatch::Pool::spawn(Arc::new(env.cfg.clone()), env.db.clone()); let state = wait_for(&env, &id, &[BuildState::Succeeded, BuildState::Failed]); pool.shutdown(); @@ -228,25 +232,25 @@ fn queued_candidate(env: &Env, name: &str) -> CandidateRow { } fn wait_attempt(env: &Env, id: &str, n: i64) -> builds::BuildRow { - for _ in 0..600 { + for _ in 0..15_000 { if let Some(b) = builds::get(&env.db, id).unwrap() { if b.attempt >= n && b.state == BuildState::Failed { return b; } } - std::thread::sleep(std::time::Duration::from_millis(50)); + std::thread::sleep(std::time::Duration::from_millis(2)); } panic!("build {id} never reached {n} attempts in state failed"); } fn wait_for(env: &Env, id: &str, want: &[BuildState]) -> BuildState { - for _ in 0..600 { + for _ in 0..15_000 { if let Some(b) = builds::get(&env.db, id).unwrap() { if want.contains(&b.state) { return b.state; } } - std::thread::sleep(std::time::Duration::from_millis(50)); + std::thread::sleep(std::time::Duration::from_millis(2)); } panic!("build {id} never reached {want:?}"); } diff --git a/tests/live.rs b/tests/live.rs index e572638..b73b2f6 100644 --- a/tests/live.rs +++ b/tests/live.rs @@ -49,11 +49,8 @@ fn the_log_stream_delivers_bytes_as_they_are_appended_and_then_ends() { let id2 = id.clone(); let log2 = log.clone(); let producer = std::thread::spawn(move || { - std::thread::sleep(Duration::from_millis(400)); append(&log2, "==> step build\n"); - std::thread::sleep(Duration::from_millis(400)); append(&log2, "==> step publish\n"); - std::thread::sleep(Duration::from_millis(200)); builds::set_state(&db, &id2, BuildState::Running, None).unwrap(); builds::set_state(&db, &id2, BuildState::Succeeded, None).unwrap(); }); @@ -61,7 +58,7 @@ fn the_log_stream_delivers_bytes_as_they_are_appended_and_then_ends() { let resp = ureq::get(&web.url(&format!("/builds/{id}/log.stream"))).call().expect("stream"); assert_eq!(resp.header("content-type").unwrap_or(""), "text/event-stream"); assert_eq!(resp.header("cache-control").unwrap_or(""), "no-cache"); - let body = drain(resp.into_reader(), Duration::from_secs(15)); + let body = drain(resp.into_reader(), Duration::from_secs(30)); producer.join().unwrap(); assert!(body.contains("data: ==> step materialize"), "the first line is missing: {body}"); diff --git a/tests/vcs.rs b/tests/vcs.rs index e41adfd..52f4372 100644 --- a/tests/vcs.rs +++ b/tests/vcs.rs @@ -117,22 +117,34 @@ fn stage_b_is_cached_on_pkgbase_recipe_and_tip() { #[test] fn stage_b_concurrency_cap_is_enforced() { let (env, _fx) = env_with_fixture("git-sourced"); - let slots = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); - let s1 = slots.clone(); + use std::sync::atomic::{AtomicUsize, Ordering}; + let inside = std::sync::Arc::new(AtomicUsize::new(0)); + let attempted = std::sync::Arc::new(AtomicUsize::new(0)); let held = upstream::StageBSlot::acquire(1); - let entered = std::thread::spawn(move || { - let _g = upstream::StageBSlot::acquire(1); - s1.fetch_add(1, std::sync::atomic::Ordering::SeqCst); - }); - std::thread::sleep(std::time::Duration::from_millis(80)); - assert_eq!( - slots.load(std::sync::atomic::Ordering::SeqCst), - 0, - "a second Stage B entered while the only slot was held" - ); + let racers: Vec<_> = (0..3) + .map(|_| { + let inside = inside.clone(); + let attempted = attempted.clone(); + std::thread::spawn(move || { + attempted.fetch_add(1, Ordering::SeqCst); + let _slot = upstream::StageBSlot::acquire(1); + let n = inside.fetch_add(1, Ordering::SeqCst) + 1; + assert_eq!(n, 1, "two Stage B runs held the only slot at once"); + inside.fetch_sub(1, Ordering::SeqCst); + }) + }) + .collect(); + // Every racer is queued on the slot before it is released, so a cap that + // leaked would let them all in at once rather than by luck of scheduling. + while attempted.load(Ordering::SeqCst) < 3 { + std::thread::yield_now(); + } + assert_eq!(inside.load(Ordering::SeqCst), 0, "a second Stage B entered while the only slot was held"); drop(held); - entered.join().unwrap(); - assert_eq!(slots.load(std::sync::atomic::Ordering::SeqCst), 1); + for r in racers { + r.join().unwrap(); + } + assert_eq!(inside.load(Ordering::SeqCst), 0); let _ = env; } -- cgit v1.2.3