From e390a62614f7308fa5268ed0c49a6111cee4b9f6 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:46:19 -0500 Subject: [PATCH 01/33] feat(testkit): #3062 deadline helpers and a test watchdog wait_until and recv_within give a test wait a limit and fail the test with what it waited for; time_scale (LIGHTCRAFT_TIME_SCALE) stretches every limit on a slow machine, and scaled applies it to a hand-made one. watch() registers the running test with a per-process watchdog that lists the watched tests still running and ends the binary once one has run past LIGHTCRAFT_TEST_WATCHDOG_MIN minutes (default 30, 0 = off). Co-Authored-By: Claude Opus 5.5 --- crates/testkit/src/deadline.rs | 196 +++++++++++++++++++++ crates/testkit/src/lib.rs | 17 ++ crates/testkit/src/watchdog.rs | 309 +++++++++++++++++++++++++++++++++ 3 files changed, 522 insertions(+) create mode 100644 crates/testkit/src/deadline.rs create mode 100644 crates/testkit/src/watchdog.rs diff --git a/crates/testkit/src/deadline.rs b/crates/testkit/src/deadline.rs new file mode 100644 index 00000000000..d1c93bfcb3f --- /dev/null +++ b/crates/testkit/src/deadline.rs @@ -0,0 +1,196 @@ +//! Deadlines for test waits. Stable libtest has no per-test timeout, so a wait that never ends +//! hangs its test binary with no message (and keeps the app's workers alive). These helpers give +//! every wait a limit and fail the test with what it was waiting for. +//! +//! Every limit is stretched by [`time_scale`] (`LIGHTCRAFT_TIME_SCALE`), so a slow or shared +//! machine can give all of them more time at once. + +use std::sync::OnceLock; +use std::sync::mpsc::{Receiver, RecvTimeoutError}; +use std::time::{Duration, Instant}; + +/// The environment variable that stretches (or shrinks) every test time limit: a factor, `1` by +/// default. `LIGHTCRAFT_TIME_SCALE=3` gives each wait three times as long. +pub const TIME_SCALE_ENV: &str = "LIGHTCRAFT_TIME_SCALE"; + +/// The smallest factor [`time_scale`] returns: shrinking limits further only makes tests flaky. +pub const MIN_TIME_SCALE: f64 = 0.1; +/// The largest factor [`time_scale`] returns: past it a limit is as good as none. +pub const MAX_TIME_SCALE: f64 = 1000.0; + +/// The longest a poll of [`wait_until`] sleeps between two checks of its condition. +const MAX_POLL: Duration = Duration::from_millis(50); + +/// The factor every test time limit is multiplied by: `LIGHTCRAFT_TIME_SCALE`, read once per +/// process. Unset or empty means `1`. A number is clamped to [`MIN_TIME_SCALE`]..=[`MAX_TIME_SCALE`]; +/// anything else (not a number, NaN, infinite, zero or negative) is ignored with a note on stderr, +/// and the factor stays `1`. +pub fn time_scale() -> f64 { + static SCALE: OnceLock = OnceLock::new(); + *SCALE.get_or_init(|| { + let value = std::env::var(TIME_SCALE_ENV).ok(); + let (scale, note) = parse_time_scale(value.as_deref()); + if let Some(note) = note { + eprintln!("{TIME_SCALE_ENV}: {note}"); + } + scale + }) +} + +/// `d` stretched by [`time_scale`]. Saturates at [`Duration::MAX`] instead of overflowing. +pub fn scaled(d: Duration) -> Duration { + scale_by(d, time_scale()) +} + +/// `d × scale`, saturating; a factor that isn't a positive number leaves `d` as it is. +fn scale_by(d: Duration, scale: f64) -> Duration { + if !scale.is_finite() || scale <= 0.0 { + return d; + } + Duration::try_from_secs_f64(d.as_secs_f64() * scale).unwrap_or(Duration::MAX) +} + +/// The factor `LIGHTCRAFT_TIME_SCALE=value` means, and a note when the value was ignored or +/// clamped. +fn parse_time_scale(value: Option<&str>) -> (f64, Option) { + let Some(text) = value.map(str::trim).filter(|v| !v.is_empty()) else { return (1.0, None) }; + match text.parse::() { + Ok(s) if s.is_finite() && s > 0.0 => { + let clamped = s.clamp(MIN_TIME_SCALE, MAX_TIME_SCALE); + let note = (clamped != s).then(|| format!("{text} is outside {MIN_TIME_SCALE}..={MAX_TIME_SCALE}; using {clamped}")); + (clamped, note) + } + _ => (1.0, Some(format!("{text:?} is not a positive number; using 1"))), + } +} + +/// Wait until `cond` holds, checking it with a growing pause (1 ms up to 50 ms). Fails the test +/// (panics) with `"{what}" not reached after {elapsed}` once [`scaled`]`(timeout)` has passed. +/// `what` names the state waited for, as the failure should read: `"the scan's first row"`. +/// +/// Also bounds waits on other threads (a gated worker job, an injected probe): the panic ends +/// that thread's job instead of leaving it spinning after the test is gone. +#[allow(clippy::panic)] // failing the test with what it waited for is the point +#[track_caller] +pub fn wait_until(timeout: Duration, what: &str, mut cond: impl FnMut() -> bool) { + let limit = scaled(timeout); + let start = Instant::now(); + let mut pause = Duration::from_millis(1); + loop { + if cond() { + return; + } + let elapsed = start.elapsed(); + if elapsed >= limit { + panic!("\"{what}\" not reached after {elapsed:.2?} (limit {limit:?}: {timeout:?} × {TIME_SCALE_ENV} {})", time_scale()); + } + std::thread::sleep(pause.min(limit.saturating_sub(elapsed))); + pause = (pause * 2).min(MAX_POLL); + } +} + +/// Receive the next message from `rx`, waiting at most [`scaled`]`(timeout)`. Fails the test +/// (panics) with `"{what}" not received after {elapsed}` when it doesn't come, or when every +/// sender is gone first. +#[allow(clippy::panic)] // failing the test with what it waited for is the point +#[track_caller] +pub fn recv_within(rx: &Receiver, timeout: Duration, what: &str) -> T { + let limit = scaled(timeout); + let start = Instant::now(); + match rx.recv_timeout(limit) { + Ok(v) => v, + Err(RecvTimeoutError::Timeout) => { + panic!("\"{what}\" not received after {:.2?} (limit {limit:?}: {timeout:?} × {TIME_SCALE_ENV} {})", start.elapsed(), time_scale()) + } + Err(RecvTimeoutError::Disconnected) => panic!("\"{what}\" never sent: the sender hung up after {:.2?}", start.elapsed()), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + + #[test] + fn the_time_scale_reads_a_positive_factor_and_ignores_the_rest() { + assert_eq!(parse_time_scale(None), (1.0, None)); + assert_eq!(parse_time_scale(Some("")), (1.0, None)); + assert_eq!(parse_time_scale(Some(" ")), (1.0, None)); + assert_eq!(parse_time_scale(Some("2.5")), (2.5, None)); + assert_eq!(parse_time_scale(Some(" 3 ")), (3.0, None)); + for bad in ["0", "-2", "NaN", "nan", "inf", "-inf", "fast", "1e400", "0x10"] { + let (scale, note) = parse_time_scale(Some(bad)); + assert_eq!(scale, 1.0, "{bad}"); + assert!(note.is_some_and(|n| n.contains("not a positive number")), "{bad}"); + } + let (scale, note) = parse_time_scale(Some("1e9")); + assert_eq!(scale, MAX_TIME_SCALE); + assert!(note.is_some_and(|n| n.contains("outside"))); + let (scale, note) = parse_time_scale(Some("0.0001")); + assert_eq!(scale, MIN_TIME_SCALE); + assert!(note.is_some()); + } + + #[test] + fn scaling_saturates_instead_of_overflowing() { + assert_eq!(scale_by(Duration::from_secs(10), 2.0), Duration::from_secs(20)); + assert_eq!(scale_by(Duration::from_secs(10), 0.5), Duration::from_secs(5)); + assert_eq!(scale_by(Duration::MAX, MAX_TIME_SCALE), Duration::MAX); + assert_eq!(scale_by(Duration::from_secs(10), f64::NAN), Duration::from_secs(10)); + assert_eq!(scale_by(Duration::from_secs(10), -1.0), Duration::from_secs(10)); + assert_eq!(scale_by(Duration::ZERO, 3.0), Duration::ZERO); + } + + #[test] + fn wait_until_returns_once_the_condition_holds() { + let checks = AtomicUsize::new(0); + let start = Instant::now(); + wait_until(Duration::from_secs(30), "the third check", || checks.fetch_add(1, Ordering::SeqCst) >= 2); + assert_eq!(checks.load(Ordering::SeqCst), 3); + assert!(start.elapsed() < Duration::from_secs(10), "it doesn't wait out the limit"); + } + + #[test] + fn wait_until_sees_a_change_made_by_another_thread() { + let flag = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let f = flag.clone(); + let t = std::thread::spawn(move || { + std::thread::sleep(Duration::from_millis(30)); + f.store(true, Ordering::SeqCst); + }); + wait_until(Duration::from_secs(30), "the flag", || flag.load(Ordering::SeqCst)); + t.join().unwrap(); + } + + #[test] + #[should_panic(expected = "\"the gate opens\" not reached after")] + fn wait_until_fails_the_test_with_what_it_waited_for() { + wait_until(Duration::from_millis(20), "the gate opens", || false); + } + + #[test] + fn recv_within_returns_the_message() { + let (tx, rx) = std::sync::mpsc::channel(); + let t = std::thread::spawn(move || { + std::thread::sleep(Duration::from_millis(20)); + tx.send(7).unwrap(); + }); + assert_eq!(recv_within(&rx, Duration::from_secs(30), "the job's result"), 7); + t.join().unwrap(); + } + + #[test] + #[should_panic(expected = "\"the job starts\" not received after")] + fn recv_within_fails_the_test_when_nothing_comes() { + let (_tx, rx) = std::sync::mpsc::channel::<()>(); + recv_within(&rx, Duration::from_millis(20), "the job starts"); + } + + #[test] + #[should_panic(expected = "\"the job starts\" never sent: the sender hung up")] + fn recv_within_fails_the_test_when_the_sender_is_gone() { + let (tx, rx) = std::sync::mpsc::channel::<()>(); + drop(tx); + recv_within(&rx, Duration::from_secs(30), "the job starts"); + } +} diff --git a/crates/testkit/src/lib.rs b/crates/testkit/src/lib.rs index 1715dee9438..a8983c15d5e 100644 --- a/crates/testkit/src/lib.rs +++ b/crates/testkit/src/lib.rs @@ -20,6 +20,17 @@ //! `cargo xtask ci` lists the skips by need and reason in its summary and in //! `target/ci/report.json`. A test that runs part of its checks (e.g. only the CPU half) records //! the part it could not run the same way. +//! +//! **Deadlines.** Stable libtest has no per-test timeout, so a wait with no limit hangs its binary +//! with no message. Waits go through [`wait_until`] (a condition) and [`recv_within`] (a channel), +//! which fail the test with what they waited for; a hand-made limit (a condvar's +//! `wait_timeout_while`) uses [`scaled`]. Every limit is stretched by [`time_scale`] +//! (`LIGHTCRAFT_TIME_SCALE`, default 1) on a slow or shared machine. +//! +//! **Watchdog.** [`watch`] registers the running test; a test still registered after +//! `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30, × the time scale; `0` turns it off) makes +//! the watchdog print the watched tests still running and end the binary (exit status 124). ui-egui's +//! `Headless::new` calls it in that crate's unit tests. See [`watchdog`] for the defaults' reasons. #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] @@ -27,6 +38,12 @@ use std::fmt::Write as _; use std::io::Write as _; use std::path::Path; +mod deadline; +pub mod watchdog; + +pub use deadline::{MAX_TIME_SCALE, MIN_TIME_SCALE, TIME_SCALE_ENV, recv_within, scaled, time_scale, wait_until}; +pub use watchdog::{DEFAULT_WATCHDOG_MINUTES, WATCHDOG_ENV, Watch, watch}; + /// The environment variable naming the file skips are appended to (JSON lines). pub const SKIP_LOG_ENV: &str = "LIGHTCRAFT_SKIP_LOG"; diff --git a/crates/testkit/src/watchdog.rs b/crates/testkit/src/watchdog.rs new file mode 100644 index 00000000000..085dcd00e93 --- /dev/null +++ b/crates/testkit/src/watchdog.rs @@ -0,0 +1,309 @@ +//! A watchdog for test binaries. Stable libtest has no per-test timeout: a test stuck in a wait +//! keeps its binary (and the app's render workers) alive for good, and the run only looks busy. +//! A test that calls [`watch`] (ui-egui's `Headless::new` does it for every headless test) is +//! registered under its thread's name, which libtest sets to the test's path. One thread per +//! process checks the registered tests; once one of them has run longer than the limit it prints +//! every registered test still running, with how long each has run, and ends the process with +//! [`WATCHDOG_EXIT_CODE`]. A +//! stuck run then ends with a list of names instead of hanging. +//! +//! **The limit** is `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes per test (fractions allowed), +//! [`DEFAULT_WATCHDOG_MINUTES`] when unset, stretched by [`crate::time_scale`]. `0` or `off` +//! turns the watchdog off (for a test paused in a debugger). Anything else that isn't a positive +//! number is ignored with a note on stderr, and the default applies. +//! +//! **Why 30 minutes by default, and on.** Off unless set would leave exactly the runs that need it +//! unguarded: an ad-hoc `cargo test` that hangs. The limit is per test, not per binary, so a long +//! binary (the ui-egui unit tests take minutes at 4 threads, far more on a cold debug build on a +//! loaded machine) never trips it; the slowest single test takes about a minute under load, so 30 +//! minutes is far past anything but a test that will never finish, and `LIGHTCRAFT_TIME_SCALE` +//! stretches it on a machine that is slower still. +//! +//! **How it ends the process.** `std::process::exit` with [`WATCHDOG_EXIT_CODE`], so a stuck run +//! leaves no crash report or core dump behind. The process is stuck by definition, and an exit +//! handler (a GPU driver's) could get stuck too: if the exit hasn't finished +//! [`ABORT_AFTER_EXIT`] later, the watchdog aborts the process instead. +//! +//! The report goes straight to the process's stderr: libtest captures the output of a test's +//! threads (the watchdog's thread starts from the first watched test's), and a captured report +//! would be lost with the process. + +use std::fmt::Write as _; +use std::io::Write as _; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, Once, OnceLock, PoisonError, Weak}; +use std::time::{Duration, Instant}; + +/// The environment variable holding the per-test limit in minutes (`0` or `off`: no watchdog). +pub const WATCHDOG_ENV: &str = "LIGHTCRAFT_TEST_WATCHDOG_MIN"; + +/// The per-test limit in minutes when [`WATCHDOG_ENV`] is unset, before [`crate::time_scale`]. +pub const DEFAULT_WATCHDOG_MINUTES: f64 = 30.0; + +/// The exit status of a test binary the watchdog ended: 124, as `timeout(1)` uses. +pub const WATCHDOG_EXIT_CODE: i32 = 124; + +/// How long the exit may take before the watchdog aborts the process instead. +pub const ABORT_AFTER_EXIT: Duration = Duration::from_secs(10); + +/// One registered test. +struct Entry { + id: u64, + test: String, + since: Instant, + /// Alive while the registering thread runs: libtest runs each test on a thread of its own, so + /// a registration whose guard was leaked (or kept in a static) is dropped with its thread + /// instead of tripping the watchdog later in the run. + thread: Weak<()>, +} + +static WATCHED: Mutex> = Mutex::new(Vec::new()); +static NEXT_ID: AtomicU64 = AtomicU64::new(0); + +thread_local! { + static THREAD_ALIVE: Arc<()> = Arc::new(()); +} + +/// A test's registration with the watchdog; dropping it (at the end of the test, also when the +/// test panics) ends the registration. +#[must_use = "the test is watched only while the guard lives: `let _watch = watch();`"] +#[derive(Debug)] +pub struct Watch { + id: u64, +} + +impl Drop for Watch { + fn drop(&mut self) { + let mut watched = WATCHED.lock().unwrap_or_else(PoisonError::into_inner); + watched.retain(|e| e.id != self.id); + } +} + +/// Register the current test (by its thread's name) with the watchdog until the returned guard +/// is dropped, and start the watchdog thread on the first call. Hold the guard for the whole test: +/// `let _watch = lightcraft_testkit::watch();`. +pub fn watch() -> Watch { + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); + let thread = std::thread::current(); + let test = thread.name().unwrap_or("").to_owned(); + let alive = THREAD_ALIVE.try_with(Arc::downgrade).unwrap_or_default(); + WATCHED.lock().unwrap_or_else(PoisonError::into_inner).push(Entry { id, test, since: Instant::now(), thread: alive }); + if let Some(limit) = limit() { + start(limit); + } + Watch { id } +} + +/// The per-test limit ([`WATCHDOG_ENV`] × [`crate::time_scale`]), read once; `None` when off. +fn limit() -> Option { + static LIMIT: OnceLock> = OnceLock::new(); + *LIMIT.get_or_init(|| { + let value = std::env::var(WATCHDOG_ENV).ok(); + let (minutes, note) = parse_minutes(value.as_deref()); + if let Some(note) = note { + eprintln!("{WATCHDOG_ENV}: {note}"); + } + let minutes = minutes?; + // a limit too large for a Duration is no limit + Duration::try_from_secs_f64(minutes * 60.0).ok().map(crate::scaled) + }) +} + +/// The minutes `LIGHTCRAFT_TEST_WATCHDOG_MIN=value` means (`None`: off) and a note when the value +/// was ignored. +fn parse_minutes(value: Option<&str>) -> (Option, Option) { + let Some(text) = value.map(str::trim).filter(|v| !v.is_empty()) else { return (Some(DEFAULT_WATCHDOG_MINUTES), None) }; + if text.eq_ignore_ascii_case("off") { + return (None, None); + } + match text.parse::() { + Ok(0.0) => (None, None), + Ok(m) if m.is_finite() && m > 0.0 => (Some(m), None), + _ => (Some(DEFAULT_WATCHDOG_MINUTES), Some(format!("{text:?} is not a number of minutes (or 0 / off); using {DEFAULT_WATCHDOG_MINUTES}"))), + } +} + +/// Start the watchdog thread, once per process. +fn start(limit: Duration) { + static STARTED: Once = Once::new(); + STARTED.call_once(|| { + // check often enough that a short limit (tests of the watchdog) isn't overshot by much + let poll = (limit / 20).clamp(Duration::from_millis(10), Duration::from_secs(5)); + let spawned = std::thread::Builder::new().name("lightcraft-testkit-watchdog".into()).spawn(move || { + loop { + std::thread::sleep(poll); + let report = overdue(&WATCHED.lock().unwrap_or_else(PoisonError::into_inner), Instant::now(), limit); + if let Some(report) = report { + // straight to stderr: `eprintln!` would go to the capture of the test that started this thread + let _ = writeln!(std::io::stderr(), "{report}"); + end_process(); + } + } + }); + if let Err(e) = spawned { + eprintln!("{WATCHDOG_ENV}: can't start the test watchdog ({e}); hung tests won't be stopped"); + } + }); +} + +/// Exit with [`WATCHDOG_EXIT_CODE`], or abort when an exit handler holds the exit up for +/// [`ABORT_AFTER_EXIT`]. +fn end_process() -> ! { + let _ = std::thread::Builder::new().name("lightcraft-testkit-watchdog-abort".into()).spawn(|| { + std::thread::sleep(ABORT_AFTER_EXIT); + let _ = writeln!(std::io::stderr(), "lightcraft-testkit watchdog: the exit is stuck too; aborting"); + std::process::abort(); + }); + std::process::exit(WATCHDOG_EXIT_CODE) +} + +/// When a live registered test has run for `limit` or longer: the report printed before the +/// process ends, naming every live registered test, longest-running first. +fn overdue(entries: &[Entry], now: Instant, limit: Duration) -> Option { + let mut live: Vec<(&str, Duration)> = + entries.iter().filter(|e| e.thread.strong_count() > 0).map(|e| (e.test.as_str(), now.saturating_duration_since(e.since))).collect(); + live.sort_by_key(|&(_, ran)| std::cmp::Reverse(ran)); + let &(stuck, ran) = live.first().filter(|(_, ran)| *ran >= limit)?; + let mut report = format!( + "lightcraft-testkit watchdog: `{stuck}` has run for {}, past the limit of {} ({WATCHDOG_ENV} × {}); \ + ending the test binary. Watched tests still running:", + minutes(ran), + minutes(limit), + crate::TIME_SCALE_ENV, + ); + for (test, ran) in &live { + let _ = write!(report, "\n {test} ({})", minutes(*ran)); + } + Some(report) +} + +/// `95.5 s` → `1m 35.5s`. +fn minutes(d: Duration) -> String { + let secs = d.as_secs_f64(); + let whole = (secs / 60.0).floor(); + format!("{whole}m {:.1}s", secs - whole * 60.0) +} + +/// The names of the live registered tests (for the tests). +#[cfg(test)] +fn running() -> Vec { + let watched = WATCHED.lock().unwrap_or_else(PoisonError::into_inner); + watched.iter().filter(|e| e.thread.strong_count() > 0).map(|e| e.test.clone()).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Set for the child process [`a_stuck_test_ends_its_binary_with_the_tests_still_running`] starts. + const CHILD_ENV: &str = "LIGHTCRAFT_TESTKIT_WATCHDOG_CHILD"; + + fn entry(test: &str, since: Instant, thread: &Arc<()>) -> Entry { + Entry { id: 0, test: test.into(), since, thread: Arc::downgrade(thread) } + } + + #[test] + fn the_limit_reads_minutes_and_off() { + assert_eq!(parse_minutes(None), (Some(DEFAULT_WATCHDOG_MINUTES), None)); + assert_eq!(parse_minutes(Some(" ")), (Some(DEFAULT_WATCHDOG_MINUTES), None)); + assert_eq!(parse_minutes(Some("45")), (Some(45.0), None)); + assert_eq!(parse_minutes(Some("0.5")), (Some(0.5), None)); + assert_eq!(parse_minutes(Some("0")), (None, None)); + assert_eq!(parse_minutes(Some("OFF")), (None, None)); + for bad in ["-1", "NaN", "inf", "ten", "1e400"] { + let (minutes, note) = parse_minutes(Some(bad)); + assert_eq!(minutes, Some(DEFAULT_WATCHDOG_MINUTES), "{bad}"); + assert!(note.is_some(), "{bad}"); + } + } + + #[test] + fn a_test_past_the_limit_is_reported_with_everything_still_running() { + let base = Instant::now(); + let (alive, gone) = (Arc::new(()), Arc::new(())); + let entries = vec![ + entry("ui::quick", base + Duration::from_secs(50), &alive), + entry("ui::stuck", base, &alive), + entry("ui::leaked_by_a_finished_thread", base, &gone), + ]; + drop(gone); + let limit = Duration::from_secs(60); + assert_eq!(overdue(&entries, base + Duration::from_secs(59), limit), None, "nothing is past the limit yet"); + let report = overdue(&entries, base + Duration::from_secs(60), limit).unwrap(); + assert!(report.contains("`ui::stuck` has run for 1m 0.0s, past the limit of 1m 0.0s"), "{report}"); + let stuck = report.find("\n ui::stuck (1m 0.0s)").unwrap(); + let quick = report.find("\n ui::quick (0m 10.0s)").unwrap(); + assert!(stuck < quick, "longest first: {report}"); + assert!(!report.contains("leaked"), "a finished thread's registration doesn't count: {report}"); + } + + #[test] + fn nothing_is_overdue_without_live_tests() { + let base = Instant::now(); + assert_eq!(overdue(&[], base + Duration::from_secs(3600), Duration::from_secs(1)), None); + let gone = Arc::new(()); + let entries = vec![entry("t", base, &gone)]; + drop(gone); + assert_eq!(overdue(&entries, base + Duration::from_secs(3600), Duration::from_secs(1)), None); + } + + #[test] + fn a_watch_registers_the_test_until_it_ends() { + let me = std::thread::current().name().unwrap_or("").to_owned(); + let count = || running().iter().filter(|t| **t == me).count(); + let before = count(); + let w = watch(); + assert_eq!(count(), before + 1); + drop(w); + assert_eq!(count(), before); + } + + #[test] + fn a_test_that_panics_or_leaks_its_watch_is_unregistered() { + let named = |name: &str| running().iter().filter(|t| *t == name).count(); + let panicked = std::thread::Builder::new() + .name("testkit::panicking_test".into()) + .spawn(|| { + let _watch = watch(); + panic!("the test fails"); + }) + .unwrap() + .join(); + assert!(panicked.is_err()); + assert_eq!(named("testkit::panicking_test"), 0, "unwinding drops the guard"); + std::thread::Builder::new().name("testkit::leaking_test".into()).spawn(|| std::mem::forget(watch())).unwrap().join().unwrap(); + assert_eq!(named("testkit::leaking_test"), 0, "the thread ended with the test"); + } + + /// Run as a child by the next test: a test stuck in a wait, under a watchdog of a fraction of a + /// second. Ends on its own after a minute, so a watchdog that never fires fails the parent + /// rather than hanging it. + #[test] + #[ignore = "a stuck test: run as a child process by a_stuck_test_ends_its_binary_with_the_tests_still_running"] + fn stuck_child() { + if std::env::var_os(CHILD_ENV).is_none() { + return; + } + let _watch = watch(); + std::thread::sleep(Duration::from_secs(60)); + } + + #[test] + fn a_stuck_test_ends_its_binary_with_the_tests_still_running() { + let exe = std::env::current_exe().unwrap(); + let start = Instant::now(); + let out = std::process::Command::new(exe) + // captured, as in a normal run: the report must get out anyway + .args(["--ignored", "--exact", "watchdog::tests::stuck_child", "--test-threads", "1"]) + .env(CHILD_ENV, "1") + .env(WATCHDOG_ENV, "0.005") // 0.3 s + .env(crate::TIME_SCALE_ENV, "1") + .output() + .unwrap(); + let stderr = String::from_utf8_lossy(&out.stderr); + assert_eq!(out.status.code(), Some(WATCHDOG_EXIT_CODE), "the watchdog ends the run: {stderr}"); + assert!(start.elapsed() < Duration::from_secs(50), "it ended the stuck test early: {stderr}"); + assert!(stderr.contains("lightcraft-testkit watchdog: `watchdog::tests::stuck_child` has run for"), "{stderr}"); + assert!(stderr.contains("\n watchdog::tests::stuck_child ("), "{stderr}"); + } +} From 0dfab5762c63489adf7cc9bf9d16efbd6ce9fbbf Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:57:54 -0500 Subject: [PATCH 02/33] test(ui-egui): #3062 every headless test is watched Headless::new registers the test with the testkit watchdog in the crate's unit tests, so a stuck headless test ends the binary with its name. testkit::watched lists the watched tests, for the check. Also routes the two gated file probes in headless.rs through wait_until. Co-Authored-By: Claude Opus 5.5 --- crates/testkit/src/lib.rs | 2 +- crates/testkit/src/watchdog.rs | 10 +++++----- crates/ui-egui/src/headless.rs | 6 ++++++ crates/ui-egui/src/headless/tests.rs | 22 ++++++++++++++++------ 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/crates/testkit/src/lib.rs b/crates/testkit/src/lib.rs index a8983c15d5e..559dfb48425 100644 --- a/crates/testkit/src/lib.rs +++ b/crates/testkit/src/lib.rs @@ -42,7 +42,7 @@ mod deadline; pub mod watchdog; pub use deadline::{MAX_TIME_SCALE, MIN_TIME_SCALE, TIME_SCALE_ENV, recv_within, scaled, time_scale, wait_until}; -pub use watchdog::{DEFAULT_WATCHDOG_MINUTES, WATCHDOG_ENV, Watch, watch}; +pub use watchdog::{DEFAULT_WATCHDOG_MINUTES, WATCHDOG_ENV, WATCHDOG_EXIT_CODE, Watch, watch, watched}; /// The environment variable naming the file skips are appended to (JSON lines). pub const SKIP_LOG_ENV: &str = "LIGHTCRAFT_SKIP_LOG"; diff --git a/crates/testkit/src/watchdog.rs b/crates/testkit/src/watchdog.rs index 085dcd00e93..e4445175516 100644 --- a/crates/testkit/src/watchdog.rs +++ b/crates/testkit/src/watchdog.rs @@ -184,9 +184,9 @@ fn minutes(d: Duration) -> String { format!("{whole}m {:.1}s", secs - whole * 60.0) } -/// The names of the live registered tests (for the tests). -#[cfg(test)] -fn running() -> Vec { +/// The names of the watched tests still running, as the watchdog would list them (a test can +/// check that it is watched). +pub fn watched() -> Vec { let watched = WATCHED.lock().unwrap_or_else(PoisonError::into_inner); watched.iter().filter(|e| e.thread.strong_count() > 0).map(|e| e.test.clone()).collect() } @@ -250,7 +250,7 @@ mod tests { #[test] fn a_watch_registers_the_test_until_it_ends() { let me = std::thread::current().name().unwrap_or("").to_owned(); - let count = || running().iter().filter(|t| **t == me).count(); + let count = || watched().iter().filter(|t| **t == me).count(); let before = count(); let w = watch(); assert_eq!(count(), before + 1); @@ -260,7 +260,7 @@ mod tests { #[test] fn a_test_that_panics_or_leaks_its_watch_is_unregistered() { - let named = |name: &str| running().iter().filter(|t| *t == name).count(); + let named = |name: &str| watched().iter().filter(|t| *t == name).count(); let panicked = std::thread::Builder::new() .name("testkit::panicking_test".into()) .spawn(|| { diff --git a/crates/ui-egui/src/headless.rs b/crates/ui-egui/src/headless.rs index 48a1161bfdc..aa3fc43407c 100644 --- a/crates/ui-egui/src/headless.rs +++ b/crates/ui-egui/src/headless.rs @@ -269,6 +269,10 @@ pub struct Headless { /// create a prompt after the host input was already filtered for the previous frame. #[cfg(test)] after_raw_input_hook: Option>, + /// The test driving this session is watched while it lives: a test stuck past the watchdog's + /// limit ends the binary with the names of the tests still running (`lightcraft_testkit::watch`). + #[cfg(test)] + _watch: lightcraft_testkit::Watch, } /// A headless run (`lightcraft-cli snapshot`, tests) is a one-shot process: however it ends, it @@ -311,6 +315,8 @@ impl Headless { last_cursor: egui::CursorIcon::Default, #[cfg(test)] after_raw_input_hook: None, + #[cfg(test)] + _watch: lightcraft_testkit::watch(), } } diff --git a/crates/ui-egui/src/headless/tests.rs b/crates/ui-egui/src/headless/tests.rs index 157f3893343..1d1facfb469 100644 --- a/crates/ui-egui/src/headless/tests.rs +++ b/crates/ui-egui/src/headless/tests.rs @@ -102,6 +102,20 @@ fn a_rejected_model_file_says_why_in_settings() { use super::*; use std::sync::Arc; +/// Every headless test is watched (issue #3062): one stuck past the watchdog's limit ends the +/// test binary with its name instead of hanging it. +#[test] +fn a_headless_session_watches_its_test() { + let me = std::thread::current().name().unwrap_or_default().to_owned(); + let mine = || lightcraft_testkit::watched().iter().filter(|t| **t == me).count(); + assert_eq!(mine(), 0); + let app = LightcraftApp::new(lightcraft_engine::Session::new(), crate::Services { png: None, ..Default::default() }); + let h = Headless::new(app, [400.0, 300.0], 1.0); + assert_eq!(mine(), 1); + drop(h); + assert_eq!(mine(), 0, "the watch ends with the session"); +} + /// The pretend host has a clipboard, as a desktop does: text the UI copies lands in it, and a /// cut, copy or paste request (from a context menu) comes back as that event on the next frame. #[test] @@ -3388,9 +3402,7 @@ fn browsing_the_same_folder_again_does_not_restart() { let (g, n) = (gate.clone(), started.clone()); h.app.session.media.file_probe = Some(std::sync::Arc::new(move |_: &str| { n.fetch_add(1, std::sync::atomic::Ordering::Relaxed); - while !g.load(std::sync::atomic::Ordering::Relaxed) { - std::thread::sleep(Duration::from_millis(5)); - } + lightcraft_testkit::wait_until(Duration::from_secs(60), "the test opens the probe's gate", || g.load(std::sync::atomic::Ordering::Relaxed)); Ok(lightcraft_engine::media::ProbeInfo { format: "PNG".into(), ..Default::default() }) })); let browse = |h: &mut Headless| h.request("engine.execute", json!({"command": "library.browse", "params": {"path": dir.to_string_lossy()}}), t); @@ -3516,9 +3528,7 @@ fn folder_scan_cancel_and_busy() { let gate = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); let g = gate.clone(); h.app.session.media.file_probe = Some(std::sync::Arc::new(move |_: &str| { - while !g.load(std::sync::atomic::Ordering::Relaxed) { - std::thread::sleep(Duration::from_millis(5)); - } + lightcraft_testkit::wait_until(Duration::from_secs(60), "the test opens the probe's gate", || g.load(std::sync::atomic::Ordering::Relaxed)); Ok(lightcraft_engine::media::ProbeInfo { format: "PNG".into(), ..Default::default() }) })); let r = h.request("engine.execute", json!({"command": "file.addFolder", "params": {"path": dir.to_string_lossy()}}), t); From c609da95ddab4f236e06eccad1346b858afa1b3e Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:57:54 -0500 Subject: [PATCH 03/33] test: #3062 give the unbounded test waits in ui-egui, gpu and preview a limit The gated probe in tests_sync, the first-render wait in gpu's shutdown test, the sleeping drive in tests_offline and the gate job, spins and recv() calls of the preview pool tests now fail with what they waited for instead of hanging the test binary. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/gpu/tests/shutdown.rs | 6 +++--- crates/preview/Cargo.toml | 3 +++ crates/preview/src/tests.rs | 28 +++++++++++----------------- crates/ui-egui/src/tests/offline.rs | 15 ++++++++++----- crates/ui-egui/src/tests/sync.rs | 4 +--- 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c7412025a14..22a0f8e19a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4247,6 +4247,7 @@ version = "0.21.1" dependencies = [ "craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)", "lightcraft-raster", + "lightcraft-testkit", "log", "siphasher", ] diff --git a/crates/gpu/tests/shutdown.rs b/crates/gpu/tests/shutdown.rs index a8d5cbd2309..00283e5c163 100644 --- a/crates/gpu/tests/shutdown.rs +++ b/crates/gpu/tests/shutdown.rs @@ -40,9 +40,9 @@ fn nothing_renders_on_the_gpu_once_the_process_is_ending() { }) }; if has_gpu { - while rendered.load(Ordering::SeqCst) == 0 && !refused.load(Ordering::SeqCst) { - std::thread::yield_now(); - } + lightcraft_testkit::wait_until(Duration::from_secs(120), "the worker's first GPU render (or the GPU refusing it)", || { + rendered.load(Ordering::SeqCst) > 0 || refused.load(Ordering::SeqCst) + }); assert!(rendered.load(Ordering::SeqCst) > 0, "the GPU renders before the shutdown ({:?})", lightcraft_gpu::last_fallback()); } diff --git a/crates/preview/Cargo.toml b/crates/preview/Cargo.toml index 853f1e9c645..d40f7907792 100644 --- a/crates/preview/Cargo.toml +++ b/crates/preview/Cargo.toml @@ -13,3 +13,6 @@ lightcraft-raster = { workspace = true } siphasher = { workspace = true } craft-image-codecs = { workspace = true } log = { workspace = true } + +[dev-dependencies] +lightcraft-testkit = { workspace = true } diff --git a/crates/preview/src/tests.rs b/crates/preview/src/tests.rs index 079eb74e5f3..d667229f671 100644 --- a/crates/preview/src/tests.rs +++ b/crates/preview/src/tests.rs @@ -182,17 +182,14 @@ fn pool_priorities_dedupe_and_drop() { 1000, Box::new(move || { let (m, cv) = &*g; - let mut open = m.lock().unwrap(); - while !*open { - open = cv.wait(open).unwrap(); - } + let limit = lightcraft_testkit::scaled(std::time::Duration::from_secs(60)); + let (_open, waited) = cv.wait_timeout_while(m.lock().unwrap(), limit, |open| !*open).unwrap(); + assert!(!waited.timed_out(), "the test never opened the gate"); 999 }), ); // wait until the worker took the gate job - while p.queued() > 0 { - std::thread::yield_now(); - } + lightcraft_testkit::wait_until(std::time::Duration::from_secs(30), "the worker takes the gate job", || p.queued() == 0); p.submit(1, 1, 5, Box::new(|| 1)); // off-screen thumb p.submit(2, 1, 10, Box::new(|| 2)); // visible thumb p.submit(3, 1, 100, Box::new(|| 3)); // loupe @@ -207,13 +204,10 @@ fn pool_priorities_dedupe_and_drop() { cv.notify_all(); } let mut order = Vec::new(); - while order.len() < 4 { - if let Some(d) = p.try_recv() { - order.push((d.slot, d.key, d.result)); - } else { - std::thread::yield_now(); - } - } + lightcraft_testkit::wait_until(std::time::Duration::from_secs(30), "four jobs done", || { + order.extend(std::iter::from_fn(|| p.try_recv()).map(|d| (d.slot, d.key, d.result))); + order.len() >= 4 + }); assert_eq!(order, vec![(999, 0, 999), (3, 1, 3), (2, 2, 22), (1, 1, 1)]); } @@ -243,7 +237,7 @@ fn pool_with_a_running_job() -> (JobPool, std::sync::mpsc::Sender<()>) { let _ = released.recv_timeout(std::time::Duration::from_secs(60)); }), ); - has_started.recv().unwrap(); + lightcraft_testkit::recv_within(&has_started, std::time::Duration::from_secs(30), "the job starts"); (p, release) } @@ -265,7 +259,7 @@ fn pool_drop_waits_for_running_job() { f.store(true, Ordering::SeqCst); }), ); - has_started.recv().unwrap(); + lightcraft_testkit::recv_within(&has_started, std::time::Duration::from_secs(30), "the job starts"); let q = queued_ran.clone(); p.submit(2, 0, 1, Box::new(move || q.store(true, Ordering::SeqCst))); drop(p); @@ -732,7 +726,7 @@ fn a_slot_runs_one_job_at_a_time() { }) }; p.submit(1, 1, 9, job(1, Some(released))); - assert_eq!(has_started.recv().unwrap(), 1); + assert_eq!(lightcraft_testkit::recv_within(&has_started, std::time::Duration::from_secs(30), "slot 1's first job starts"), 1); // two newer requests for the busy slot: the second replaces the first, neither starts p.submit(1, 2, 9, job(2, None)); p.submit(1, 3, 9, job(3, None)); diff --git a/crates/ui-egui/src/tests/offline.rs b/crates/ui-egui/src/tests/offline.rs index 5327412d1bc..baa262b106b 100644 --- a/crates/ui-egui/src/tests/offline.rs +++ b/crates/ui-egui/src/tests/offline.rs @@ -42,8 +42,8 @@ impl Calls { } /// The sleeping drive: every file-system call waits until [`Nas::wake`] (or takes one of the -/// calls [`Nas::let_through`] allows). A watchdog wakes it after [`WATCHDOG`] and records that it -/// had to (some frame was waiting for the drive). +/// calls [`Nas::let_through`] allows). A watchdog wakes it after [`WATCHDOG`] (× the test time +/// scale) and records that it had to (some frame was waiting for the drive). #[derive(Default)] struct Nas { /// (awake, calls let through while asleep) @@ -58,7 +58,7 @@ impl Nas { let dog = nas.clone(); std::thread::spawn(move || { let state = dog.state.lock().unwrap(); - let (mut state, _) = dog.changed.wait_timeout_while(state, WATCHDOG, |s| !s.0).unwrap(); + let (mut state, _) = dog.changed.wait_timeout_while(state, lightcraft_testkit::scaled(WATCHDOG), |s| !s.0).unwrap(); if !state.0 { dog.forced.store(true, Ordering::SeqCst); state.0 = true; @@ -69,8 +69,13 @@ impl Nas { } fn wait(&self) { let state = self.state.lock().unwrap(); - let mut state = self.changed.wait_while(state, |s| !s.0 && s.1 == 0).unwrap(); - if !state.0 { + // bounded without the watchdog thread too, should it be gone: it wakes the drive after + // WATCHDOG, so only a call nothing will ever wake gets to twice that + let limit = lightcraft_testkit::scaled(WATCHDOG * 2); + let (mut state, waited) = self.changed.wait_timeout_while(state, limit, |s| !s.0 && s.1 == 0).unwrap(); + if waited.timed_out() { + self.forced.store(true, Ordering::SeqCst); + } else if !state.0 { state.1 -= 1; } } diff --git a/crates/ui-egui/src/tests/sync.rs b/crates/ui-egui/src/tests/sync.rs index 84cda586021..c98756f9f6d 100644 --- a/crates/ui-egui/src/tests/sync.rs +++ b/crates/ui-egui/src/tests/sync.rs @@ -349,9 +349,7 @@ fn the_folders_scan_shows_a_row_and_its_cross_closes_the_dialog() { let gate = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); let g = gate.clone(); h.app.session.media.file_probe = Some(std::sync::Arc::new(move |_: &str| { - while !g.load(std::sync::atomic::Ordering::Relaxed) { - std::thread::sleep(Duration::from_millis(5)); - } + lightcraft_testkit::wait_until(Duration::from_secs(60), "the test opens the probe's gate", || g.load(std::sync::atomic::Ordering::Relaxed)); Ok(lightcraft_engine::media::ProbeInfo { format: "PNG".into(), ..Default::default() }) })); crate::sync::open(&mut h.app, &dir.path("trip"), "trip", false).unwrap(); From 5437b5d7787518b83379e9205006db8315065308 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:57:54 -0500 Subject: [PATCH 04/33] docs: #3062 test deadlines, LIGHTCRAFT_TIME_SCALE and the test watchdog Co-Authored-By: Claude Opus 5.5 --- README.md | 7 +++++++ docs/architecture.md | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/README.md b/README.md index a3e49813607..4a2b8f71748 100644 --- a/README.md +++ b/README.md @@ -489,6 +489,13 @@ adapters per backend, corpus, fonts) and every skip by reason. It writes `target `target/ci/summary.md`, a block to paste into a PR (`--summary-md` prints it); `--hardware` also runs the GPU tests on every backend that has a hardware adapter. Nothing is uploaded. +libtest has no per-test timeout either, so test waits go through `lightcraft_testkit::wait_until` and +`recv_within`, which fail the test with what it waited for. `LIGHTCRAFT_TIME_SCALE=3` gives every such +limit three times as long on a slow or shared machine. A watchdog ends a test binary (exit status 124) +when a watched test (every ui-egui headless test, or one that calls `lightcraft_testkit::watch()`) has +run for `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30, times the time scale; `0` turns it off), +printing the watched tests still running. + **Chinese and Japanese text** need the shared font repo, an optional build input (official releases always include it): ```sh diff --git a/docs/architecture.md b/docs/architecture.md index 9437896b2d9..a06d69b982c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -83,10 +83,19 @@ time, skips) and `target/ci/summary.md` (Markdown for a PR; `--summary-md` print no host name, user name or absolute path, and nothing is uploaded. `--hardware` adds a step that runs the `lightcraft-gpu` tests once per backend with a hardware adapter (`LIGHTCRAFT_GPU_BACKEND`). +libtest has no per-test timeout, so a test wait never goes without a limit: `lightcraft_testkit::wait_until` +(a condition, polled with backoff) and `recv_within` (a channel) fail the test with what they waited for, +and a hand-made limit uses `scaled`. `LIGHTCRAFT_TIME_SCALE` (default 1, clamped to 0.1–1000) stretches +every limit at once on a slow machine. `lightcraft_testkit::watch()` registers the running test with a +per-process watchdog (ui-egui's `Headless::new` does it in that crate's unit tests): once a watched test +has run for `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30 × the time scale; `0` or `off` disables +it) it prints the watched tests still running and exits with status 124, so a hung run ends with names. + ## Revision history | Date | Change | Summary | |---|---|---| +| 2026-10-26 | minor | Quality gates: test deadlines (`wait_until`, `recv_within`, `LIGHTCRAFT_TIME_SCALE`) and the test watchdog | | 2026-10-23 | minor | Quality gates: one integration-test binary per crate, doctests apart, nextest on request, one WASM check | | 2026-10-11 | minor | Quality gates: the CI machine probe, visible skips (`testkit`) and `target/ci/report.json` | | 2026-10-10 | major | Created from the code (crate sizes, layers, data model, rendering, control, gates) | From 05471f3eddbbc9f8a14a2b5637e10bb7526c9126 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 12:33:21 -0500 Subject: [PATCH 05/33] fix(testkit): #3062 the watchdog's thread owns the hard deadline A helper thread now prints the report and exits; the watchdog thread does no I/O, and aborts the process once ABORT_AFTER_EXIT has passed, or at once when the helper can't start. Before, a stuck test holding stderr's lock blocked the report, so the process never ended, and a failed spawn of the abort thread left a hanging exit unbounded. Co-Authored-By: Claude Opus 5.5 --- crates/testkit/src/watchdog.rs | 103 ++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 27 deletions(-) diff --git a/crates/testkit/src/watchdog.rs b/crates/testkit/src/watchdog.rs index e4445175516..30c1e89df12 100644 --- a/crates/testkit/src/watchdog.rs +++ b/crates/testkit/src/watchdog.rs @@ -19,10 +19,12 @@ //! minutes is far past anything but a test that will never finish, and `LIGHTCRAFT_TIME_SCALE` //! stretches it on a machine that is slower still. //! -//! **How it ends the process.** `std::process::exit` with [`WATCHDOG_EXIT_CODE`], so a stuck run -//! leaves no crash report or core dump behind. The process is stuck by definition, and an exit -//! handler (a GPU driver's) could get stuck too: if the exit hasn't finished -//! [`ABORT_AFTER_EXIT`] later, the watchdog aborts the process instead. +//! **How it ends the process.** A helper thread prints the report and calls `std::process::exit` +//! with [`WATCHDOG_EXIT_CODE`], so a stuck run normally leaves no crash report or core dump +//! behind. The process is stuck by definition, so that can get stuck too: a stuck test may hold +//! stderr's lock, an exit handler (a GPU driver's) may hang. The watchdog thread itself owns the +//! hard deadline and does no I/O: it aborts the process if it still runs [`ABORT_AFTER_EXIT`] +//! later, or at once if the helper thread can't be started. //! //! The report goes straight to the process's stderr: libtest captures the output of a test's //! threads (the watchdog's thread starts from the first watched test's), and a captured report @@ -43,7 +45,7 @@ pub const DEFAULT_WATCHDOG_MINUTES: f64 = 30.0; /// The exit status of a test binary the watchdog ended: 124, as `timeout(1)` uses. pub const WATCHDOG_EXIT_CODE: i32 = 124; -/// How long the exit may take before the watchdog aborts the process instead. +/// How long printing the report and exiting may take before the watchdog aborts the process. pub const ABORT_AFTER_EXIT: Duration = Duration::from_secs(10); /// One registered test. @@ -134,9 +136,7 @@ fn start(limit: Duration) { std::thread::sleep(poll); let report = overdue(&WATCHED.lock().unwrap_or_else(PoisonError::into_inner), Instant::now(), limit); if let Some(report) = report { - // straight to stderr: `eprintln!` would go to the capture of the test that started this thread - let _ = writeln!(std::io::stderr(), "{report}"); - end_process(); + end_process(report); } } }); @@ -146,15 +146,20 @@ fn start(limit: Duration) { }); } -/// Exit with [`WATCHDOG_EXIT_CODE`], or abort when an exit handler holds the exit up for -/// [`ABORT_AFTER_EXIT`]. -fn end_process() -> ! { - let _ = std::thread::Builder::new().name("lightcraft-testkit-watchdog-abort".into()).spawn(|| { - std::thread::sleep(ABORT_AFTER_EXIT); - let _ = writeln!(std::io::stderr(), "lightcraft-testkit watchdog: the exit is stuck too; aborting"); - std::process::abort(); +/// End the process for a stuck test, on the watchdog thread. A helper thread prints `report` and +/// exits with [`WATCHDOG_EXIT_CODE`]; this thread does no I/O (stderr's lock may be held by the +/// stuck test) and aborts the process if it is still running [`ABORT_AFTER_EXIT`] later, or at +/// once when the helper can't be started. +fn end_process(report: String) -> ! { + let reporter = std::thread::Builder::new().name("lightcraft-testkit-watchdog-exit".into()).spawn(move || { + // straight to stderr: `eprintln!` would go to the capture of the test that started the watchdog + let _ = writeln!(std::io::stderr(), "{report}"); + std::process::exit(WATCHDOG_EXIT_CODE) }); - std::process::exit(WATCHDOG_EXIT_CODE) + if reporter.is_ok() { + std::thread::sleep(ABORT_AFTER_EXIT); + } + std::process::abort() } /// When a live registered test has run for `limit` or longer: the report printed before the @@ -275,8 +280,8 @@ mod tests { assert_eq!(named("testkit::leaking_test"), 0, "the thread ended with the test"); } - /// Run as a child by the next test: a test stuck in a wait, under a watchdog of a fraction of a - /// second. Ends on its own after a minute, so a watchdog that never fires fails the parent + /// Run as a child by [`a_stuck_test_ends_its_binary_with_the_tests_still_running`]: a test stuck + /// in a wait. Ends on its own after a minute, so a watchdog that never fires fails the parent /// rather than hanging it. #[test] #[ignore = "a stuck test: run as a child process by a_stuck_test_ends_its_binary_with_the_tests_still_running"] @@ -288,22 +293,66 @@ mod tests { std::thread::sleep(Duration::from_secs(60)); } + /// Run as a child by [`a_stuck_test_holding_stderr_still_ends_its_binary`]: a test stuck while + /// it holds stderr's lock, so the report can't be printed. #[test] - fn a_stuck_test_ends_its_binary_with_the_tests_still_running() { - let exe = std::env::current_exe().unwrap(); + #[ignore = "a stuck test: run as a child process by a_stuck_test_holding_stderr_still_ends_its_binary"] + fn stuck_child_holding_stderr() { + if std::env::var_os(CHILD_ENV).is_none() { + return; + } + let _watch = watch(); + let _stderr = std::io::stderr().lock(); + std::thread::sleep(Duration::from_secs(60)); + } + + /// Run the ignored test `name` of this binary as a child under a watchdog of 0.3 s. Returns its + /// exit status (`None`: it was still running after `deadline` and was killed), its stderr and + /// how long it ran. + fn run_stuck_child(name: &str, deadline: Duration) -> (Option, String, Duration) { let start = Instant::now(); - let out = std::process::Command::new(exe) + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) // captured, as in a normal run: the report must get out anyway - .args(["--ignored", "--exact", "watchdog::tests::stuck_child", "--test-threads", "1"]) + .args(["--ignored", "--exact", name, "--test-threads", "1"]) .env(CHILD_ENV, "1") - .env(WATCHDOG_ENV, "0.005") // 0.3 s + .env(WATCHDOG_ENV, "0.005") .env(crate::TIME_SCALE_ENV, "1") - .output() + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .spawn() .unwrap(); - let stderr = String::from_utf8_lossy(&out.stderr); - assert_eq!(out.status.code(), Some(WATCHDOG_EXIT_CODE), "the watchdog ends the run: {stderr}"); - assert!(start.elapsed() < Duration::from_secs(50), "it ended the stuck test early: {stderr}"); + let ended = loop { + if child.try_wait().unwrap().is_some() { + break true; + } + if start.elapsed() > deadline { + let _ = child.kill(); + break false; + } + std::thread::sleep(Duration::from_millis(20)); + }; + let ran = start.elapsed(); + let out = child.wait_with_output().unwrap(); + (ended.then_some(out.status), String::from_utf8_lossy(&out.stderr).into_owned(), ran) + } + + #[test] + fn a_stuck_test_ends_its_binary_with_the_tests_still_running() { + let (status, stderr, ran) = run_stuck_child("watchdog::tests::stuck_child", crate::scaled(Duration::from_secs(45))); + let status = status.unwrap_or_else(|| panic!("the watchdog didn't end the stuck test in {ran:?}: {stderr}")); + assert_eq!(status.code(), Some(WATCHDOG_EXIT_CODE), "the watchdog ends the run: {stderr}"); assert!(stderr.contains("lightcraft-testkit watchdog: `watchdog::tests::stuck_child` has run for"), "{stderr}"); assert!(stderr.contains("\n watchdog::tests::stuck_child ("), "{stderr}"); } + + /// The report can't be printed while the stuck test holds stderr's lock: the watchdog thread + /// aborts the process once [`ABORT_AFTER_EXIT`] has passed instead of waiting for it. + #[test] + fn a_stuck_test_holding_stderr_still_ends_its_binary() { + let (status, stderr, ran) = run_stuck_child("watchdog::tests::stuck_child_holding_stderr", crate::scaled(Duration::from_secs(45))); + let status = status.unwrap_or_else(|| panic!("the stuck test still ran after {ran:?}: {stderr}")); + assert!(!status.success(), "{status:?}"); + assert_ne!(status.code(), Some(WATCHDOG_EXIT_CODE), "it can't have printed and exited: {stderr}"); + assert!(ran >= ABORT_AFTER_EXIT, "aborted after the grace period, not before: {ran:?}"); + } } From bfe7fac3100199b12f7afcff61fff266513a7b68 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 12:45:55 -0500 Subject: [PATCH 06/33] fix(testkit): #3062 a shorter grace period for the watchdog's own abort test LIGHTCRAFT_TEST_WATCHDOG_GRACE_MS (testkit's tests only; whole milliseconds, 100 ms..=10 min, else 10 s) replaces the grace period before the abort, so the stderr-held test takes half a second. On Linux the stuck children run under prlimit --core=1, which leaves no core dump even with a core_pattern pipe such as WSL's crash capture. Co-Authored-By: Claude Opus 5.5 --- crates/testkit/src/watchdog.rs | 113 +++++++++++++++++++++++++-------- 1 file changed, 88 insertions(+), 25 deletions(-) diff --git a/crates/testkit/src/watchdog.rs b/crates/testkit/src/watchdog.rs index 30c1e89df12..7199d720364 100644 --- a/crates/testkit/src/watchdog.rs +++ b/crates/testkit/src/watchdog.rs @@ -26,6 +26,10 @@ //! hard deadline and does no I/O: it aborts the process if it still runs [`ABORT_AFTER_EXIT`] //! later, or at once if the helper thread can't be started. //! +//! For testkit's own tests only, `LIGHTCRAFT_TEST_WATCHDOG_GRACE_MS` replaces that grace period +//! (whole milliseconds, held to 100 ms..=10 min; anything else keeps [`ABORT_AFTER_EXIT`]), so +//! the test of the abort doesn't wait ten seconds. +//! //! The report goes straight to the process's stderr: libtest captures the output of a test's //! threads (the watchdog's thread starts from the first watched test's), and a captured report //! would be lost with the process. @@ -48,6 +52,18 @@ pub const WATCHDOG_EXIT_CODE: i32 = 124; /// How long printing the report and exiting may take before the watchdog aborts the process. pub const ABORT_AFTER_EXIT: Duration = Duration::from_secs(10); +/// Replaces [`ABORT_AFTER_EXIT`], for testkit's own tests (milliseconds). +const GRACE_ENV: &str = "LIGHTCRAFT_TEST_WATCHDOG_GRACE_MS"; + +/// The grace period `LIGHTCRAFT_TEST_WATCHDOG_GRACE_MS=value` means: whole milliseconds held to +/// 100 ms..=10 min; unset, empty or not a whole number keeps [`ABORT_AFTER_EXIT`]. +fn parse_grace(value: Option<&str>) -> Duration { + match value.map(str::trim).and_then(|v| v.parse::().ok()) { + Some(ms) => Duration::from_millis(ms.clamp(100, 600_000)), + None => ABORT_AFTER_EXIT, + } +} + /// One registered test. struct Entry { id: u64, @@ -129,6 +145,8 @@ fn parse_minutes(value: Option<&str>) -> (Option, Option) { fn start(limit: Duration) { static STARTED: Once = Once::new(); STARTED.call_once(|| { + // read here, not when the watchdog fires: a stuck process may hold the environment's lock + let grace = parse_grace(std::env::var(GRACE_ENV).ok().as_deref()); // check often enough that a short limit (tests of the watchdog) isn't overshot by much let poll = (limit / 20).clamp(Duration::from_millis(10), Duration::from_secs(5)); let spawned = std::thread::Builder::new().name("lightcraft-testkit-watchdog".into()).spawn(move || { @@ -136,7 +154,7 @@ fn start(limit: Duration) { std::thread::sleep(poll); let report = overdue(&WATCHED.lock().unwrap_or_else(PoisonError::into_inner), Instant::now(), limit); if let Some(report) = report { - end_process(report); + end_process(report, grace); } } }); @@ -148,16 +166,16 @@ fn start(limit: Duration) { /// End the process for a stuck test, on the watchdog thread. A helper thread prints `report` and /// exits with [`WATCHDOG_EXIT_CODE`]; this thread does no I/O (stderr's lock may be held by the -/// stuck test) and aborts the process if it is still running [`ABORT_AFTER_EXIT`] later, or at -/// once when the helper can't be started. -fn end_process(report: String) -> ! { +/// stuck test) and aborts the process if it is still running `grace` ([`ABORT_AFTER_EXIT`]) later, +/// or at once when the helper can't be started. +fn end_process(report: String, grace: Duration) -> ! { let reporter = std::thread::Builder::new().name("lightcraft-testkit-watchdog-exit".into()).spawn(move || { // straight to stderr: `eprintln!` would go to the capture of the test that started the watchdog let _ = writeln!(std::io::stderr(), "{report}"); std::process::exit(WATCHDOG_EXIT_CODE) }); if reporter.is_ok() { - std::thread::sleep(ABORT_AFTER_EXIT); + std::thread::sleep(grace); } std::process::abort() } @@ -306,21 +324,46 @@ mod tests { std::thread::sleep(Duration::from_secs(60)); } - /// Run the ignored test `name` of this binary as a child under a watchdog of 0.3 s. Returns its - /// exit status (`None`: it was still running after `deadline` and was killed), its stderr and - /// how long it ran. - fn run_stuck_child(name: &str, deadline: Duration) -> (Option, String, Duration) { + /// How a child test run by [`run_stuck_child`] ended. + struct Child { + /// `None`: still running after the deadline, and killed. + status: Option, + stderr: String, + ran: Duration, + /// Run under `prlimit --core=1`, so an abort leaves no core dump. + no_core: bool, + } + + /// Run the ignored test `name` of this binary as a child under a watchdog of 0.3 s (and `env`), + /// killing it if it still runs after `deadline`. + fn run_stuck_child(name: &str, env: &[(&str, &str)], deadline: Duration) -> Child { let start = Instant::now(); - let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + let exe = std::env::current_exe().unwrap(); + let configure = |mut c: std::process::Command| { // captured, as in a normal run: the report must get out anyway - .args(["--ignored", "--exact", name, "--test-threads", "1"]) - .env(CHILD_ENV, "1") - .env(WATCHDOG_ENV, "0.005") - .env(crate::TIME_SCALE_ENV, "1") - .stdout(std::process::Stdio::null()) - .stderr(std::process::Stdio::piped()) - .spawn() - .unwrap(); + c.args(["--ignored", "--exact", name, "--test-threads", "1"]) + .env(CHILD_ENV, "1") + .env(WATCHDOG_ENV, "0.005") + .env(crate::TIME_SCALE_ENV, "1") + .envs(env.iter().copied()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()); + c + }; + // Linux: an aborted child leaves no core dump. A limit of 1 byte (not 0) also stops a + // core_pattern pipe (systemd-coredump, WSL's crash capture); without prlimit, run it as is. + let limited = if cfg!(target_os = "linux") { + let mut c = std::process::Command::new("prlimit"); + c.arg("--core=1:1").arg(&exe); + configure(c).spawn().ok() + } else { + None + }; + let no_core = limited.is_some(); + let mut child = match limited { + Some(c) => c, + None => configure(std::process::Command::new(&exe)).spawn().unwrap(), + }; let ended = loop { if child.try_wait().unwrap().is_some() { break true; @@ -333,26 +376,46 @@ mod tests { }; let ran = start.elapsed(); let out = child.wait_with_output().unwrap(); - (ended.then_some(out.status), String::from_utf8_lossy(&out.stderr).into_owned(), ran) + Child { status: ended.then_some(out.status), stderr: String::from_utf8_lossy(&out.stderr).into_owned(), ran, no_core } + } + + #[test] + fn the_grace_period_reads_whole_milliseconds_within_bounds() { + assert_eq!(parse_grace(None), ABORT_AFTER_EXIT); + assert_eq!(parse_grace(Some("")), ABORT_AFTER_EXIT); + assert_eq!(parse_grace(Some(" 500 ")), Duration::from_millis(500)); + assert_eq!(parse_grace(Some("0")), Duration::from_millis(100)); + assert_eq!(parse_grace(Some("99999999999999999")), Duration::from_secs(600)); + for bad in ["-5", "1.5", "NaN", "soon", "18446744073709551616"] { + assert_eq!(parse_grace(Some(bad)), ABORT_AFTER_EXIT, "{bad}"); + } } #[test] fn a_stuck_test_ends_its_binary_with_the_tests_still_running() { - let (status, stderr, ran) = run_stuck_child("watchdog::tests::stuck_child", crate::scaled(Duration::from_secs(45))); - let status = status.unwrap_or_else(|| panic!("the watchdog didn't end the stuck test in {ran:?}: {stderr}")); + let c = run_stuck_child("watchdog::tests::stuck_child", &[], crate::scaled(Duration::from_secs(45))); + let (ran, stderr) = (c.ran, &c.stderr); + let status = c.status.unwrap_or_else(|| panic!("the watchdog didn't end the stuck test in {ran:?}: {stderr}")); assert_eq!(status.code(), Some(WATCHDOG_EXIT_CODE), "the watchdog ends the run: {stderr}"); assert!(stderr.contains("lightcraft-testkit watchdog: `watchdog::tests::stuck_child` has run for"), "{stderr}"); assert!(stderr.contains("\n watchdog::tests::stuck_child ("), "{stderr}"); } /// The report can't be printed while the stuck test holds stderr's lock: the watchdog thread - /// aborts the process once [`ABORT_AFTER_EXIT`] has passed instead of waiting for it. + /// aborts the process once the grace period has passed instead of waiting for it. #[test] fn a_stuck_test_holding_stderr_still_ends_its_binary() { - let (status, stderr, ran) = run_stuck_child("watchdog::tests::stuck_child_holding_stderr", crate::scaled(Duration::from_secs(45))); - let status = status.unwrap_or_else(|| panic!("the stuck test still ran after {ran:?}: {stderr}")); + let grace = Duration::from_millis(500); + let c = run_stuck_child("watchdog::tests::stuck_child_holding_stderr", &[(GRACE_ENV, "500")], crate::scaled(Duration::from_secs(45))); + let (ran, stderr) = (c.ran, &c.stderr); + let status = c.status.unwrap_or_else(|| panic!("the stuck test still ran after {ran:?}: {stderr}")); assert!(!status.success(), "{status:?}"); assert_ne!(status.code(), Some(WATCHDOG_EXIT_CODE), "it can't have printed and exited: {stderr}"); - assert!(ran >= ABORT_AFTER_EXIT, "aborted after the grace period, not before: {ran:?}"); + assert!(ran >= grace, "aborted after the grace period, not before: {ran:?}"); + #[cfg(unix)] + if c.no_core { + use std::os::unix::process::ExitStatusExt as _; + assert!(!status.core_dumped(), "the abort left a core dump: {status:?}"); + } } } From 08e1dc1422ffded77f7d9974635af00b12b07daf Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:55:49 -0500 Subject: [PATCH 07/33] docs: #3062 the README says which tests the watchdog covers Headless::new registers the watch under cfg(test), so only the headless tests in ui-egui's unit tests are watched; its integration tests in crates/ui-egui/tests/ link the library without cfg(test) and need lightcraft_testkit::watch(). Co-Authored-By: Claude Opus 5.5 --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4a2b8f71748..b46fd113c93 100644 --- a/README.md +++ b/README.md @@ -492,9 +492,10 @@ GPU tests on every backend that has a hardware adapter. Nothing is uploaded. libtest has no per-test timeout either, so test waits go through `lightcraft_testkit::wait_until` and `recv_within`, which fail the test with what it waited for. `LIGHTCRAFT_TIME_SCALE=3` gives every such limit three times as long on a slow or shared machine. A watchdog ends a test binary (exit status 124) -when a watched test (every ui-egui headless test, or one that calls `lightcraft_testkit::watch()`) has -run for `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30, times the time scale; `0` turns it off), -printing the watched tests still running. +when a watched test has run for `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30, times the time scale; +`0` turns it off), printing the watched tests still running. The headless tests in ui-egui's unit tests are +watched; any other test, including ui-egui's integration tests in `crates/ui-egui/tests/`, is watched only when +it calls `lightcraft_testkit::watch()`. **Chinese and Japanese text** need the shared font repo, an optional build input (official releases always include it): From 07af1ef98bbe1d357d0db3f856da194b72ed7f21 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:55:49 -0500 Subject: [PATCH 08/33] test(ui-egui): #3062 the rejected-model test records a skip without the denoise runtime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It returned early without running its assertions and passed silently; it now calls lightcraft_testkit::skip!(Feature, …) first, so ci lists it. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/headless/tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/ui-egui/src/headless/tests.rs b/crates/ui-egui/src/headless/tests.rs index 1d1facfb469..758acab1ddf 100644 --- a/crates/ui-egui/src/headless/tests.rs +++ b/crates/ui-egui/src/headless/tests.rs @@ -73,6 +73,7 @@ fn a_rejected_model_file_says_why_in_settings() { h.app.session.set_denoise_models_dir(Some(dir.join("denoise-models"))); // without the runtime the button is disabled (the workspace build turns it on through the desktop app) if h.app.session.execute("denoise.models.list", &json!({})).unwrap()["runtime"] != true { + lightcraft_testkit::skip!(Feature, "denoise runtime unavailable: the workspace build turns it on"); let _ = std::fs::remove_dir_all(&dir); return; } From 12b25090e9a32cc2c3b3f4d34d8c56a93385edee Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:57:31 -0500 Subject: [PATCH 09/33] fix(testkit): #3062 the watchdog tests' kill deadline never shrinks below 45 s The parent killed the stuck child after scaled(45 s), 4.5 s at LIGHTCRAFT_TIME_SCALE=0.1, while the child's own watchdog runs at scale 1 and the child still has to start: on a loaded machine the parent could kill it first and report that the watchdog didn't work. Co-Authored-By: Claude Opus 5.5 --- crates/testkit/src/watchdog.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/testkit/src/watchdog.rs b/crates/testkit/src/watchdog.rs index 7199d720364..5f2989bb25c 100644 --- a/crates/testkit/src/watchdog.rs +++ b/crates/testkit/src/watchdog.rs @@ -334,6 +334,13 @@ mod tests { no_core: bool, } + /// How long a stuck child may run before the parent kills it: 45 s stretched by the time scale, + /// never shrunk, since the child's watchdog runs at scale 1 and the child still has to start. + fn child_deadline() -> Duration { + let d = Duration::from_secs(45); + crate::scaled(d).max(d) + } + /// Run the ignored test `name` of this binary as a child under a watchdog of 0.3 s (and `env`), /// killing it if it still runs after `deadline`. fn run_stuck_child(name: &str, env: &[(&str, &str)], deadline: Duration) -> Child { @@ -393,7 +400,7 @@ mod tests { #[test] fn a_stuck_test_ends_its_binary_with_the_tests_still_running() { - let c = run_stuck_child("watchdog::tests::stuck_child", &[], crate::scaled(Duration::from_secs(45))); + let c = run_stuck_child("watchdog::tests::stuck_child", &[], child_deadline()); let (ran, stderr) = (c.ran, &c.stderr); let status = c.status.unwrap_or_else(|| panic!("the watchdog didn't end the stuck test in {ran:?}: {stderr}")); assert_eq!(status.code(), Some(WATCHDOG_EXIT_CODE), "the watchdog ends the run: {stderr}"); @@ -406,7 +413,7 @@ mod tests { #[test] fn a_stuck_test_holding_stderr_still_ends_its_binary() { let grace = Duration::from_millis(500); - let c = run_stuck_child("watchdog::tests::stuck_child_holding_stderr", &[(GRACE_ENV, "500")], crate::scaled(Duration::from_secs(45))); + let c = run_stuck_child("watchdog::tests::stuck_child_holding_stderr", &[(GRACE_ENV, "500")], child_deadline()); let (ran, stderr) = (c.ran, &c.stderr); let status = c.status.unwrap_or_else(|| panic!("the stuck test still ran after {ran:?}: {stderr}")); assert!(!status.success(), "{status:?}"); From ee68988df5470a5e1ef849c3f86db09e84ae8b4e Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Fri, 30 Oct 2026 03:38:57 -0500 Subject: [PATCH 10/33] fix(testkit): #3062 setting notes reach stderr even when the test that read them passes A mistyped LIGHTCRAFT_TIME_SCALE or LIGHTCRAFT_TEST_WATCHDOG_MIN, and a watchdog thread that couldn't start, were reported with eprintln! on the thread of the first test to read the setting: libtest captured it and dropped it when that test passed. They go straight to the process's stderr now, as the watchdog's report already did. Co-Authored-By: Claude Opus 5.5 --- crates/testkit/src/deadline.rs | 2 +- crates/testkit/src/lib.rs | 7 +++++++ crates/testkit/src/watchdog.rs | 28 ++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/crates/testkit/src/deadline.rs b/crates/testkit/src/deadline.rs index d1c93bfcb3f..4925bc77a39 100644 --- a/crates/testkit/src/deadline.rs +++ b/crates/testkit/src/deadline.rs @@ -31,7 +31,7 @@ pub fn time_scale() -> f64 { let value = std::env::var(TIME_SCALE_ENV).ok(); let (scale, note) = parse_time_scale(value.as_deref()); if let Some(note) = note { - eprintln!("{TIME_SCALE_ENV}: {note}"); + crate::note(&format!("{TIME_SCALE_ENV}: {note}")); } scale }) diff --git a/crates/testkit/src/lib.rs b/crates/testkit/src/lib.rs index 559dfb48425..a774cde2ca2 100644 --- a/crates/testkit/src/lib.rs +++ b/crates/testkit/src/lib.rs @@ -92,6 +92,13 @@ macro_rules! skip { }; } +/// Print a configuration note (`LIGHTCRAFT_TIME_SCALE`, the watchdog's settings) straight to the +/// process's stderr. `eprintln!` would go to libtest's capture of the test that happened to read +/// the setting first, shown only if that test fails. +pub(crate) fn note(line: &str) { + let _ = writeln!(std::io::stderr(), "{line}"); +} + /// What [`skip!`] calls: print the note and append it to the skip log, if one is set. Never fails /// the test: a log that can't be written loses the entry, not the run. pub fn record(need: Need, reason: &str, file: &str, line: u32) { diff --git a/crates/testkit/src/watchdog.rs b/crates/testkit/src/watchdog.rs index 5f2989bb25c..774a1357c80 100644 --- a/crates/testkit/src/watchdog.rs +++ b/crates/testkit/src/watchdog.rs @@ -119,7 +119,7 @@ fn limit() -> Option { let value = std::env::var(WATCHDOG_ENV).ok(); let (minutes, note) = parse_minutes(value.as_deref()); if let Some(note) = note { - eprintln!("{WATCHDOG_ENV}: {note}"); + crate::note(&format!("{WATCHDOG_ENV}: {note}")); } let minutes = minutes?; // a limit too large for a Duration is no limit @@ -159,7 +159,7 @@ fn start(limit: Duration) { } }); if let Err(e) = spawned { - eprintln!("{WATCHDOG_ENV}: can't start the test watchdog ({e}); hung tests won't be stopped"); + crate::note(&format!("{WATCHDOG_ENV}: can't start the test watchdog ({e}); hung tests won't be stopped")); } }); } @@ -324,6 +324,30 @@ mod tests { std::thread::sleep(Duration::from_secs(60)); } + /// Run as a child by [`a_mistyped_setting_is_reported_by_a_passing_test`]: a watched test that + /// passes, so libtest shows none of its captured output. + #[test] + #[ignore = "run as a child process by a_mistyped_setting_is_reported_by_a_passing_test"] + fn passing_child() { + if std::env::var_os(CHILD_ENV).is_none() { + return; + } + let _watch = watch(); + } + + /// A mistyped `LIGHTCRAFT_TEST_WATCHDOG_MIN` or `LIGHTCRAFT_TIME_SCALE` is read on the first + /// watched test's thread; its note reaches the process's stderr even though that test passes + /// (with `eprintln!` it went to the test's capture, which libtest drops). + #[test] + fn a_mistyped_setting_is_reported_by_a_passing_test() { + let c = run_stuck_child("watchdog::tests::passing_child", &[(WATCHDOG_ENV, "soon"), (crate::TIME_SCALE_ENV, "fast")], child_deadline()); + let (ran, stderr) = (c.ran, &c.stderr); + let status = c.status.unwrap_or_else(|| panic!("the passing child still ran after {ran:?}: {stderr}")); + assert!(status.success(), "{status:?}: {stderr}"); + assert!(stderr.contains(&format!("{WATCHDOG_ENV}: \"soon\" is not a number of minutes")), "{stderr}"); + assert!(stderr.contains(&format!("{}: \"fast\" is not a positive number", crate::TIME_SCALE_ENV)), "{stderr}"); + } + /// How a child test run by [`run_stuck_child`] ended. struct Child { /// `None`: still running after the deadline, and killed. From 9d4471f7d80a4dce854a782aa6ac9e6b230491fd Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Fri, 30 Oct 2026 04:35:04 -0500 Subject: [PATCH 11/33] fix(testkit): #3062 a configuration note never keeps the watchdog from starting note() wrote to stderr on the caller's thread: had a stuck test held stderr's lock while LIGHTCRAFT_TEST_WATCHDOG_MIN was mistyped, the first watched test would block in limit(), before start(), and the watchdog meant to end the stuck run never started. The write now runs on a thread of its own and the caller waits for it at most 200 ms. Co-Authored-By: Claude Opus 5.5 --- crates/testkit/src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/crates/testkit/src/lib.rs b/crates/testkit/src/lib.rs index a774cde2ca2..ccb3559d92a 100644 --- a/crates/testkit/src/lib.rs +++ b/crates/testkit/src/lib.rs @@ -94,11 +94,25 @@ macro_rules! skip { /// Print a configuration note (`LIGHTCRAFT_TIME_SCALE`, the watchdog's settings) straight to the /// process's stderr. `eprintln!` would go to libtest's capture of the test that happened to read -/// the setting first, shown only if that test fails. +/// the setting first, shown only if that test fails. The write happens on a thread of its own and +/// the caller waits for it at most [`NOTE_WAIT`]: a stuck test may hold stderr's lock, and the +/// watchdog starting up must not wait for it. A note whose thread can't start is lost. pub(crate) fn note(line: &str) { - let _ = writeln!(std::io::stderr(), "{line}"); + let line = line.to_owned(); + let (done, written) = std::sync::mpsc::channel(); + let spawned = std::thread::Builder::new().name("lightcraft-testkit-note".into()).spawn(move || { + let _ = writeln!(std::io::stderr(), "{line}"); + let _ = done.send(()); + }); + if spawned.is_ok() { + // long enough for an uncontended write, so the note isn't lost when the process ends soon after + let _ = written.recv_timeout(NOTE_WAIT); + } } +/// How long [`note`] waits for its write. +const NOTE_WAIT: std::time::Duration = std::time::Duration::from_millis(200); + /// What [`skip!`] calls: print the note and append it to the skip log, if one is set. Never fails /// the test: a log that can't be written loses the entry, not the run. pub fn record(need: Need, reason: &str, file: &str, line: u32) { @@ -164,6 +178,29 @@ fn escape(s: &str) -> String { mod tests { use super::*; + /// A note doesn't wait for a stderr whose lock another thread holds (a stuck test): the + /// watchdog prints its configuration notes before it starts. + #[test] + fn a_note_does_not_wait_for_a_held_stderr() { + let (locked, is_locked) = std::sync::mpsc::channel(); + let (release, released) = std::sync::mpsc::channel::<()>(); + let holder = std::thread::spawn(move || { + let _stderr = std::io::stderr().lock(); + let _ = locked.send(()); + // until the test is done (or fails: its sender is dropped then) + let _ = released.recv(); + }); + recv_within(&is_locked, std::time::Duration::from_secs(30), "the other thread holds stderr"); + let (done, returned) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + note("lightcraft-testkit: a note written while another thread holds stderr"); + let _ = done.send(()); + }); + recv_within(&returned, std::time::Duration::from_secs(30), "note() returns while stderr is held"); + drop(release); + holder.join().unwrap(); + } + #[test] fn an_entry_is_one_json_line_with_its_text_escaped() { let line = entry(Need::Corpus, "skip: C:\\corpus\\raw \"absent\"\n\u{1}", "corpus::decodes", "it", "crates\\raw\\tests\\it\\corpus.rs", 28); From 0ee3beb21281587802565a655928e5359f82035e Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:12:27 -0600 Subject: [PATCH 12/33] docs: #3062 name the limits LIGHTCRAFT_TIME_SCALE stretches and who reads the grace override The architecture note said the scale stretches every limit at once, but the headless harness's request, settle and step_until compare elapsed time with the Duration they get and don't scale it. The note and the README now name what scales (wait_until, recv_within, scaled, the watchdog) and what doesn't (#3653). The watchdog's module doc said LIGHTCRAFT_TEST_WATCHDOG_GRACE_MS was for testkit's own tests, but start() reads it in every watched binary. The doc now says so; the behaviour stays. Co-Authored-By: Claude Opus 5.5 --- README.md | 13 +++++++------ crates/testkit/src/watchdog.rs | 9 +++++---- docs/architecture.md | 15 +++++++++------ 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b46fd113c93..f61b4e2fa52 100644 --- a/README.md +++ b/README.md @@ -490,12 +490,13 @@ adapters per backend, corpus, fonts) and every skip by reason. It writes `target GPU tests on every backend that has a hardware adapter. Nothing is uploaded. libtest has no per-test timeout either, so test waits go through `lightcraft_testkit::wait_until` and -`recv_within`, which fail the test with what it waited for. `LIGHTCRAFT_TIME_SCALE=3` gives every such -limit three times as long on a slow or shared machine. A watchdog ends a test binary (exit status 124) -when a watched test has run for `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30, times the time scale; -`0` turns it off), printing the watched tests still running. The headless tests in ui-egui's unit tests are -watched; any other test, including ui-egui's integration tests in `crates/ui-egui/tests/`, is watched only when -it calls `lightcraft_testkit::watch()`. +`recv_within`, which fail the test with what it waited for. `LIGHTCRAFT_TIME_SCALE=3` gives their limits, and +those built with `lightcraft_testkit::scaled`, three times as long on a slow or shared machine; the headless +harness's own timeouts (`Headless::request`, `settle`, `step_until`) don't scale. A watchdog ends a test +binary (exit status 124) when a watched test has run for `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30, +times the time scale; `0` turns it off), printing the watched tests still running. The headless tests in +ui-egui's unit tests are watched; any other test, including ui-egui's integration tests in +`crates/ui-egui/tests/`, is watched only when it calls `lightcraft_testkit::watch()`. **Chinese and Japanese text** need the shared font repo, an optional build input (official releases always include it): diff --git a/crates/testkit/src/watchdog.rs b/crates/testkit/src/watchdog.rs index 774a1357c80..6a8a8c9c879 100644 --- a/crates/testkit/src/watchdog.rs +++ b/crates/testkit/src/watchdog.rs @@ -26,9 +26,10 @@ //! hard deadline and does no I/O: it aborts the process if it still runs [`ABORT_AFTER_EXIT`] //! later, or at once if the helper thread can't be started. //! -//! For testkit's own tests only, `LIGHTCRAFT_TEST_WATCHDOG_GRACE_MS` replaces that grace period -//! (whole milliseconds, held to 100 ms..=10 min; anything else keeps [`ABORT_AFTER_EXIT`]), so -//! the test of the abort doesn't wait ten seconds. +//! `LIGHTCRAFT_TEST_WATCHDOG_GRACE_MS` replaces that grace period (whole milliseconds, held to +//! 100 ms..=10 min; anything else keeps [`ABORT_AFTER_EXIT`]). It exists so testkit's own test of +//! the abort doesn't wait ten seconds, but the watchdog reads it in every watched binary: set in +//! the environment of another test run, it shortens or lengthens that run's grace period too. //! //! The report goes straight to the process's stderr: libtest captures the output of a test's //! threads (the watchdog's thread starts from the first watched test's), and a captured report @@ -52,7 +53,7 @@ pub const WATCHDOG_EXIT_CODE: i32 = 124; /// How long printing the report and exiting may take before the watchdog aborts the process. pub const ABORT_AFTER_EXIT: Duration = Duration::from_secs(10); -/// Replaces [`ABORT_AFTER_EXIT`], for testkit's own tests (milliseconds). +/// Replaces [`ABORT_AFTER_EXIT`] in any watched binary (milliseconds); testkit's own tests set it. const GRACE_ENV: &str = "LIGHTCRAFT_TEST_WATCHDOG_GRACE_MS"; /// The grace period `LIGHTCRAFT_TEST_WATCHDOG_GRACE_MS=value` means: whole milliseconds held to diff --git a/docs/architecture.md b/docs/architecture.md index a06d69b982c..83ffd617628 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -84,12 +84,15 @@ no host name, user name or absolute path, and nothing is uploaded. `--hardware` the `lightcraft-gpu` tests once per backend with a hardware adapter (`LIGHTCRAFT_GPU_BACKEND`). libtest has no per-test timeout, so a test wait never goes without a limit: `lightcraft_testkit::wait_until` -(a condition, polled with backoff) and `recv_within` (a channel) fail the test with what they waited for, -and a hand-made limit uses `scaled`. `LIGHTCRAFT_TIME_SCALE` (default 1, clamped to 0.1–1000) stretches -every limit at once on a slow machine. `lightcraft_testkit::watch()` registers the running test with a -per-process watchdog (ui-egui's `Headless::new` does it in that crate's unit tests): once a watched test -has run for `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30 × the time scale; `0` or `off` disables -it) it prints the watched tests still running and exits with status 124, so a hung run ends with names. +(a condition, polled with backoff) and `recv_within` (a channel) fail the test with what they waited for, and +a hand-made limit uses `scaled`. `LIGHTCRAFT_TIME_SCALE` (default 1, clamped to 0.1–1000) stretches the limits +these three set, and the watchdog's below, on a slow machine. The headless harness's own timeouts +(`Headless::request`, `settle`, `step_until`) take a `Duration` and don't scale it; a caller that wants them +stretched passes a `scaled` one (#3653 tracks the rest). `lightcraft_testkit::watch()` registers the running +test with a per-process watchdog (ui-egui's `Headless::new` does it in that crate's unit tests): once a +watched test has run for `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30 × the time scale; `0` or `off` +disables it) it prints the watched tests still running and exits with status 124, so a hung run ends with +names. ## Revision history From 4cb99f4ee8e3a4898b240d4199457d0ca554d50c Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:26:15 -0600 Subject: [PATCH 13/33] test: #3062 a held probe's gate wait never shrinks below 60 s wait_until scales its limit, so at LIGHTCRAFT_TIME_SCALE=0.1 the probes held until the test opens their gate gave up after 6 s, while the test was still working towards the gate. testkit gains wait_until_at_least, whose limit is scaled(timeout) but never less than timeout, and the three gated probes (two in the headless tests, one in the sync tests) use it. Co-Authored-By: Claude Opus 5.5 --- crates/testkit/src/deadline.rs | 52 +++++++++++++++++++++++++--- crates/testkit/src/lib.rs | 2 +- crates/ui-egui/src/headless/tests.rs | 8 +++-- crates/ui-egui/src/tests/sync.rs | 4 ++- docs/architecture.md | 15 ++++---- 5 files changed, 66 insertions(+), 15 deletions(-) diff --git a/crates/testkit/src/deadline.rs b/crates/testkit/src/deadline.rs index 4925bc77a39..7155d2d0780 100644 --- a/crates/testkit/src/deadline.rs +++ b/crates/testkit/src/deadline.rs @@ -3,7 +3,8 @@ //! every wait a limit and fail the test with what it was waiting for. //! //! Every limit is stretched by [`time_scale`] (`LIGHTCRAFT_TIME_SCALE`), so a slow or shared -//! machine can give all of them more time at once. +//! machine can give all of them more time at once; [`wait_until_at_least`]'s never shrinks below +//! the limit it is given. use std::sync::OnceLock; use std::sync::mpsc::{Receiver, RecvTimeoutError}; @@ -70,10 +71,31 @@ fn parse_time_scale(value: Option<&str>) -> (f64, Option) { /// /// Also bounds waits on other threads (a gated worker job, an injected probe): the panic ends /// that thread's job instead of leaving it spinning after the test is gone. +#[track_caller] +pub fn wait_until(timeout: Duration, what: &str, cond: impl FnMut() -> bool) { + poll(scaled(timeout), &format!("{timeout:?} × {TIME_SCALE_ENV} {}", time_scale()), what, cond); +} + +/// [`wait_until`] whose limit never shrinks below `timeout`: [`scaled`]`(timeout)` or `timeout`, +/// whichever is longer. For a wait the test itself ends, such as a probe a worker holds until the +/// test opens its gate: a `LIGHTCRAFT_TIME_SCALE` under 1 shortens the test's own limits, and must +/// not make the worker give up while the test is still on its way to the gate. +#[track_caller] +pub fn wait_until_at_least(timeout: Duration, what: &str, cond: impl FnMut() -> bool) { + let rule = format!("{timeout:?} × {TIME_SCALE_ENV} {}, never less than {timeout:?}", time_scale()); + poll(at_least(timeout, time_scale()), &rule, what, cond); +} + +/// `timeout × scale`, never less than `timeout`. +fn at_least(timeout: Duration, scale: f64) -> Duration { + scale_by(timeout, scale).max(timeout) +} + +/// Poll `cond` until it holds, failing the test once `limit` has passed (`rule` says how the +/// limit was reached). #[allow(clippy::panic)] // failing the test with what it waited for is the point #[track_caller] -pub fn wait_until(timeout: Duration, what: &str, mut cond: impl FnMut() -> bool) { - let limit = scaled(timeout); +fn poll(limit: Duration, rule: &str, what: &str, mut cond: impl FnMut() -> bool) { let start = Instant::now(); let mut pause = Duration::from_millis(1); loop { @@ -82,7 +104,7 @@ pub fn wait_until(timeout: Duration, what: &str, mut cond: impl FnMut() -> bool) } let elapsed = start.elapsed(); if elapsed >= limit { - panic!("\"{what}\" not reached after {elapsed:.2?} (limit {limit:?}: {timeout:?} × {TIME_SCALE_ENV} {})", time_scale()); + panic!("\"{what}\" not reached after {elapsed:.2?} (limit {limit:?}: {rule})"); } std::thread::sleep(pause.min(limit.saturating_sub(elapsed))); pause = (pause * 2).min(MAX_POLL); @@ -168,6 +190,28 @@ mod tests { wait_until(Duration::from_millis(20), "the gate opens", || false); } + #[test] + fn an_at_least_limit_stretches_but_never_shrinks() { + let minute = Duration::from_secs(60); + assert_eq!(at_least(minute, MIN_TIME_SCALE), minute); + assert_eq!(at_least(minute, 1.0), minute); + assert_eq!(at_least(minute, 3.0), Duration::from_secs(180)); + assert_eq!(at_least(Duration::MAX, MAX_TIME_SCALE), Duration::MAX); + } + + #[test] + fn wait_until_at_least_returns_once_the_condition_holds() { + let checks = AtomicUsize::new(0); + wait_until_at_least(Duration::from_secs(30), "the third check", || checks.fetch_add(1, Ordering::SeqCst) >= 2); + assert_eq!(checks.load(Ordering::SeqCst), 3); + } + + #[test] + #[should_panic(expected = "\"the gate opens\" not reached after")] + fn wait_until_at_least_fails_the_test_with_what_it_waited_for() { + wait_until_at_least(Duration::from_millis(20), "the gate opens", || false); + } + #[test] fn recv_within_returns_the_message() { let (tx, rx) = std::sync::mpsc::channel(); diff --git a/crates/testkit/src/lib.rs b/crates/testkit/src/lib.rs index ccb3559d92a..62fddbd3b90 100644 --- a/crates/testkit/src/lib.rs +++ b/crates/testkit/src/lib.rs @@ -41,7 +41,7 @@ use std::path::Path; mod deadline; pub mod watchdog; -pub use deadline::{MAX_TIME_SCALE, MIN_TIME_SCALE, TIME_SCALE_ENV, recv_within, scaled, time_scale, wait_until}; +pub use deadline::{MAX_TIME_SCALE, MIN_TIME_SCALE, TIME_SCALE_ENV, recv_within, scaled, time_scale, wait_until, wait_until_at_least}; pub use watchdog::{DEFAULT_WATCHDOG_MINUTES, WATCHDOG_ENV, WATCHDOG_EXIT_CODE, Watch, watch, watched}; /// The environment variable naming the file skips are appended to (JSON lines). diff --git a/crates/ui-egui/src/headless/tests.rs b/crates/ui-egui/src/headless/tests.rs index 758acab1ddf..96a75ded421 100644 --- a/crates/ui-egui/src/headless/tests.rs +++ b/crates/ui-egui/src/headless/tests.rs @@ -3403,7 +3403,9 @@ fn browsing_the_same_folder_again_does_not_restart() { let (g, n) = (gate.clone(), started.clone()); h.app.session.media.file_probe = Some(std::sync::Arc::new(move |_: &str| { n.fetch_add(1, std::sync::atomic::Ordering::Relaxed); - lightcraft_testkit::wait_until(Duration::from_secs(60), "the test opens the probe's gate", || g.load(std::sync::atomic::Ordering::Relaxed)); + lightcraft_testkit::wait_until_at_least(Duration::from_secs(60), "the test opens the probe's gate", || { + g.load(std::sync::atomic::Ordering::Relaxed) + }); Ok(lightcraft_engine::media::ProbeInfo { format: "PNG".into(), ..Default::default() }) })); let browse = |h: &mut Headless| h.request("engine.execute", json!({"command": "library.browse", "params": {"path": dir.to_string_lossy()}}), t); @@ -3529,7 +3531,9 @@ fn folder_scan_cancel_and_busy() { let gate = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); let g = gate.clone(); h.app.session.media.file_probe = Some(std::sync::Arc::new(move |_: &str| { - lightcraft_testkit::wait_until(Duration::from_secs(60), "the test opens the probe's gate", || g.load(std::sync::atomic::Ordering::Relaxed)); + lightcraft_testkit::wait_until_at_least(Duration::from_secs(60), "the test opens the probe's gate", || { + g.load(std::sync::atomic::Ordering::Relaxed) + }); Ok(lightcraft_engine::media::ProbeInfo { format: "PNG".into(), ..Default::default() }) })); let r = h.request("engine.execute", json!({"command": "file.addFolder", "params": {"path": dir.to_string_lossy()}}), t); diff --git a/crates/ui-egui/src/tests/sync.rs b/crates/ui-egui/src/tests/sync.rs index c98756f9f6d..9494bd30aca 100644 --- a/crates/ui-egui/src/tests/sync.rs +++ b/crates/ui-egui/src/tests/sync.rs @@ -349,7 +349,9 @@ fn the_folders_scan_shows_a_row_and_its_cross_closes_the_dialog() { let gate = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); let g = gate.clone(); h.app.session.media.file_probe = Some(std::sync::Arc::new(move |_: &str| { - lightcraft_testkit::wait_until(Duration::from_secs(60), "the test opens the probe's gate", || g.load(std::sync::atomic::Ordering::Relaxed)); + lightcraft_testkit::wait_until_at_least(Duration::from_secs(60), "the test opens the probe's gate", || { + g.load(std::sync::atomic::Ordering::Relaxed) + }); Ok(lightcraft_engine::media::ProbeInfo { format: "PNG".into(), ..Default::default() }) })); crate::sync::open(&mut h.app, &dir.path("trip"), "trip", false).unwrap(); diff --git a/docs/architecture.md b/docs/architecture.md index 83ffd617628..5fadbd0b28e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -86,13 +86,14 @@ the `lightcraft-gpu` tests once per backend with a hardware adapter (`LIGHTCRAFT libtest has no per-test timeout, so a test wait never goes without a limit: `lightcraft_testkit::wait_until` (a condition, polled with backoff) and `recv_within` (a channel) fail the test with what they waited for, and a hand-made limit uses `scaled`. `LIGHTCRAFT_TIME_SCALE` (default 1, clamped to 0.1–1000) stretches the limits -these three set, and the watchdog's below, on a slow machine. The headless harness's own timeouts -(`Headless::request`, `settle`, `step_until`) take a `Duration` and don't scale it; a caller that wants them -stretched passes a `scaled` one (#3653 tracks the rest). `lightcraft_testkit::watch()` registers the running -test with a per-process watchdog (ui-egui's `Headless::new` does it in that crate's unit tests): once a -watched test has run for `LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30 × the time scale; `0` or `off` -disables it) it prints the watched tests still running and exits with status 124, so a hung run ends with -names. +these three set, and the watchdog's below, on a slow machine. A wait the test itself ends (a worker held until +the test opens a gate) uses `wait_until_at_least`, whose limit stretches the same way but never drops below +the one given. The headless harness's own timeouts (`Headless::request`, `settle`, `step_until`) take a +`Duration` and don't scale it; a caller that wants them stretched passes a `scaled` one (#3653 tracks the +rest). `lightcraft_testkit::watch()` registers the running test with a per-process watchdog (ui-egui's +`Headless::new` does it in that crate's unit tests): once a watched test has run for +`LIGHTCRAFT_TEST_WATCHDOG_MIN` minutes (default 30 × the time scale; `0` or `off` disables it) it prints the +watched tests still running and exits with status 124, so a hung run ends with names. ## Revision history From ceb19d3b1bbcc2dc8f7eb1f613aeb6dd98ce14d2 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Tue, 3 Nov 2026 03:01:17 -0600 Subject: [PATCH 14/33] fix(testkit): #3062 a watchdog that couldn't start is tried again at the next watch(); wait_until's limit is checked between calls to cond Co-Authored-By: Claude Opus 5.5 --- crates/testkit/src/deadline.rs | 3 +++ crates/testkit/src/watchdog.rs | 40 +++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/crates/testkit/src/deadline.rs b/crates/testkit/src/deadline.rs index 7155d2d0780..7b3e18d2ffc 100644 --- a/crates/testkit/src/deadline.rs +++ b/crates/testkit/src/deadline.rs @@ -71,6 +71,9 @@ fn parse_time_scale(value: Option<&str>) -> (f64, Option) { /// /// Also bounds waits on other threads (a gated worker job, an injected probe): the panic ends /// that thread's job instead of leaving it spinning after the test is gone. +/// +/// The limit is checked between calls to `cond`, so `cond` must return promptly: one that blocks +/// (on a lock a deadlocked worker holds, say) isn't interrupted, and the wait runs past its limit. #[track_caller] pub fn wait_until(timeout: Duration, what: &str, cond: impl FnMut() -> bool) { poll(scaled(timeout), &format!("{timeout:?} × {TIME_SCALE_ENV} {}", time_scale()), what, cond); diff --git a/crates/testkit/src/watchdog.rs b/crates/testkit/src/watchdog.rs index 6a8a8c9c879..0fc2ea28777 100644 --- a/crates/testkit/src/watchdog.rs +++ b/crates/testkit/src/watchdog.rs @@ -142,27 +142,33 @@ fn parse_minutes(value: Option<&str>) -> (Option, Option) { } } -/// Start the watchdog thread, once per process. +/// Start the watchdog thread, once per process. A lock and a flag set only once the thread is +/// running, not `Once`: a spawn that fails is tried again at the next [`watch`]. fn start(limit: Duration) { - static STARTED: Once = Once::new(); - STARTED.call_once(|| { - // read here, not when the watchdog fires: a stuck process may hold the environment's lock - let grace = parse_grace(std::env::var(GRACE_ENV).ok().as_deref()); - // check often enough that a short limit (tests of the watchdog) isn't overshot by much - let poll = (limit / 20).clamp(Duration::from_millis(10), Duration::from_secs(5)); - let spawned = std::thread::Builder::new().name("lightcraft-testkit-watchdog".into()).spawn(move || { - loop { - std::thread::sleep(poll); - let report = overdue(&WATCHED.lock().unwrap_or_else(PoisonError::into_inner), Instant::now(), limit); - if let Some(report) = report { - end_process(report, grace); - } + static STARTED: Mutex = Mutex::new(false); + static NOTED: Once = Once::new(); + let mut started = STARTED.lock().unwrap_or_else(PoisonError::into_inner); + if *started { + return; + } + // read here, not when the watchdog fires: a stuck process may hold the environment's lock + let grace = parse_grace(std::env::var(GRACE_ENV).ok().as_deref()); + // check often enough that a short limit (tests of the watchdog) isn't overshot by much + let poll = (limit / 20).clamp(Duration::from_millis(10), Duration::from_secs(5)); + let spawned = std::thread::Builder::new().name("lightcraft-testkit-watchdog".into()).spawn(move || { + loop { + std::thread::sleep(poll); + let report = overdue(&WATCHED.lock().unwrap_or_else(PoisonError::into_inner), Instant::now(), limit); + if let Some(report) = report { + end_process(report, grace); } - }); - if let Err(e) = spawned { - crate::note(&format!("{WATCHDOG_ENV}: can't start the test watchdog ({e}); hung tests won't be stopped")); } }); + match spawned { + Ok(_) => *started = true, + // said once; the next watch() tries again + Err(e) => NOTED.call_once(|| crate::note(&format!("{WATCHDOG_ENV}: can't start the test watchdog ({e}); hung tests won't be stopped yet"))), + } } /// End the process for a stuck test, on the watchdog thread. A helper thread prints `report` and From fcbd28a80bcff1639462cf79685ee8811c6f5b54 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Tue, 3 Nov 2026 03:27:24 -0600 Subject: [PATCH 15/33] test(ui-egui): #3062 the shutdown integration test is watched too Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/tests/shutdown.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/ui-egui/tests/shutdown.rs b/crates/ui-egui/tests/shutdown.rs index 1a601bc78f3..879eef8df13 100644 --- a/crates/ui-egui/tests/shutdown.rs +++ b/crates/ui-egui/tests/shutdown.rs @@ -10,6 +10,8 @@ use lightcraft_ui_egui::{LightcraftApp, Services}; #[test] fn shutdown_leaves_no_render_worker_running() { + // an integration test builds the library without cfg(test), so Headless::new doesn't register it + let _watch = lightcraft_testkit::watch(); let services = Services { png: None, ..Default::default() }; let mut h = Headless::new(LightcraftApp::new(lightcraft_engine::Session::with_demo(), services), [1200.0, 760.0], 1.0); // frames until the grid's thumbnails are being rendered on the workers From 17bfc68b9b27f5ab7b82ba7edae6f2490ad1944f Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:00:23 -0500 Subject: [PATCH 16/33] test(engine): #3064 the availability cache's caller check holds the probe shut instead of timing it The "caller never waits for the probe" check asserted the two calls took under 150 ms, which a loaded machine can exceed with nothing wrong. The probe now waits on a gate the test opens only after the calls returned, and the test asserts no check had finished by then. The module's waits use lightcraft_testkit::wait_until (stretched by LIGHTCRAFT_TIME_SCALE), and the paused-sweep test waits for the check in flight instead of assuming it ends within 500 ms. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/availability.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/availability.rs b/crates/engine/src/availability.rs index f78c4fb4890..57a887d78fc 100644 --- a/crates/engine/src/availability.rs +++ b/crates/engine/src/availability.rs @@ -300,12 +300,19 @@ mod tests { let mut a = Availability::default(); let caller = std::thread::current().id(); let on_caller = Arc::new(AtomicUsize::new(0)); - let c = on_caller.clone(); + // a sleeping NAS: every check waits until the test opens the gate (or gives up, so a + // broken build fails instead of hanging) + let gate = Arc::new((Mutex::new(false), std::sync::Condvar::new())); + let finished = Arc::new(AtomicUsize::new(0)); + let (c, g, f) = (on_caller.clone(), gate.clone(), finished.clone()); a.set_probe(Arc::new(move |p| { if std::thread::current().id() == caller { c.fetch_add(1, Ordering::SeqCst); } - std::thread::sleep(Duration::from_millis(200)); // a sleeping NAS + let (open, cv) = &*g; + let limit = lightcraft_testkit::scaled(Duration::from_secs(5)); + drop(cv.wait_timeout_while(held(open), limit, |open| !*open).unwrap_or_else(PoisonError::into_inner)); + f.fetch_add(1, Ordering::SeqCst); p == "here" })); let woke = Arc::new(AtomicUsize::new(0)); @@ -313,14 +320,13 @@ mod tests { a.run_in_background(Arc::new(move || { w.fetch_add(1, Ordering::SeqCst); })); - let t0 = std::time::Instant::now(); assert_eq!(a.exists("here"), None, "unknown at first"); assert_eq!(a.exists("gone"), None); - assert!(t0.elapsed() < Duration::from_millis(150), "the caller never waits for the probe"); - let deadline = std::time::Instant::now() + Duration::from_secs(20); - while (a.exists("here").is_none() || a.exists("gone").is_none()) && std::time::Instant::now() < deadline { - std::thread::sleep(Duration::from_millis(10)); - } + // the gate is still shut, so a caller that waited for a check could not have got here yet + assert_eq!(finished.load(Ordering::SeqCst), 0, "the caller never waits for the probe"); + *held(&gate.0) = true; + gate.1.notify_all(); + wait_until("both answers", || a.exists("here").is_some() && a.exists("gone").is_some()); assert_eq!(a.exists("here"), Some(true)); assert_eq!(a.exists("gone"), Some(false)); // Cache publication precedes its wake callback. Check these two notifications before @@ -332,12 +338,10 @@ mod tests { assert!(a.gone_generation() >= 2); } + /// Wait for `done` (at most 20 s, stretched by `LIGHTCRAFT_TIME_SCALE`). + #[track_caller] fn wait_until(what: &str, done: impl Fn() -> bool) { - let deadline = std::time::Instant::now() + Duration::from_secs(20); - while !done() { - assert!(std::time::Instant::now() < deadline, "timed out waiting: {what}"); - std::thread::sleep(Duration::from_millis(2)); - } + lightcraft_testkit::wait_until(Duration::from_secs(20), what, done); } fn held(m: &Mutex) -> MutexGuard<'_, T> { @@ -503,6 +507,7 @@ mod tests { wait_until("a few checks", || held(&log).len() >= 3); busy.store(true, Ordering::SeqCst); hold.store(false, Ordering::SeqCst); + wait_until("the check in flight", || held(&log).len() >= 4); std::thread::sleep(YIELD_POLL * 2); let stopped = held(&log).len(); assert_eq!(stopped, 4, "stopped part way, after the check it was in"); From 3b692cb8830257fffd328561bd622be46ac131b5 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:01:53 -0500 Subject: [PATCH 17/33] test(fetch): #3064 timeout and cancel bounds stretch with LIGHTCRAFT_TIME_SCALE The connect, stall and cancel tests compared elapsed time with fixed upper bounds (1-4 s) that a loaded machine can exceed. Those bounds, the client timeouts they test and the test server's stall now scale together with lightcraft_testkit::scaled, so each bound still separates the working client from the broken one; the >= 100 ms lower bound stays as it was. The cancel test cancels once the part file has bytes instead of 400 ms in, so the kept part is never empty. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/fetch/Cargo.toml | 4 ++++ crates/fetch/src/http.rs | 18 +++++++++++------- crates/fetch/src/tests.rs | 19 ++++++++++++++----- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 22a0f8e19a1..0cb48b57362 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4112,6 +4112,7 @@ dependencies = [ name = "lightcraft-fetch" version = "0.21.1" dependencies = [ + "lightcraft-testkit", "log", "rustls", "rustls-rustcrypto", diff --git a/crates/fetch/Cargo.toml b/crates/fetch/Cargo.toml index 2e65583fbc1..b92d515c3a0 100644 --- a/crates/fetch/Cargo.toml +++ b/crates/fetch/Cargo.toml @@ -15,3 +15,7 @@ rustls = { version = "0.23", default-features = false, features = ["std", "tls12 rustls-rustcrypto = "0.0.2-alpha" webpki-roots = "1" sha2 = { version = "0.10", default-features = false, features = ["std"] } + +[dev-dependencies] +# deadlines stretched by LIGHTCRAFT_TIME_SCALE +lightcraft-testkit = { workspace = true } diff --git a/crates/fetch/src/http.rs b/crates/fetch/src/http.rs index f5ae5e410a2..026767ba45a 100644 --- a/crates/fetch/src/http.rs +++ b/crates/fetch/src/http.rs @@ -611,22 +611,25 @@ pub fn get(url: &Url, headers: &[(&str, String)], limits: &Limits) -> Result Limits<'_> { - Limits { connect: Duration::from_millis(300), stall: Duration::from_secs(1), cancel } + Limits { connect: scaled(Duration::from_millis(300)), stall: scaled(Duration::from_secs(1)), cancel } } /// A name lookup that hangs is given up on at the connect timeout, and Cancel stops it at once (issue #1532). #[test] fn a_hung_name_lookup_times_out_and_can_be_cancelled() { let cancel = AtomicBool::new(false); + // (every time here is stretched by LIGHTCRAFT_TIME_SCALE, so the resolver stays far slower than the bounds) let slow = || { - std::thread::sleep(Duration::from_secs(10)); + std::thread::sleep(scaled(Duration::from_secs(10))); Ok(Vec::new()) }; let t = Instant::now(); - let r = lookup("slow.example", slow, Instant::now() + Duration::from_millis(300), &limits(&cancel)); + let r = lookup("slow.example", slow, Instant::now() + scaled(Duration::from_millis(300)), &limits(&cancel)); assert!(matches!(&r, Err(HttpError::Connect(m)) if m.contains("timed out")), "{r:?}"); - assert!(t.elapsed() < Duration::from_secs(2), "{:?}", t.elapsed()); + assert!(t.elapsed() < scaled(Duration::from_secs(2)), "{:?}", t.elapsed()); // Cancel while the lookup is waiting on the resolver, not before it starts let t = Instant::now(); let r = std::thread::scope(|scope| { @@ -637,7 +640,7 @@ mod tests_connect { lookup("slow.example", slow, Instant::now() + Duration::from_secs(30), &limits(&cancel)) }); assert!(matches!(r, Err(HttpError::Cancelled)), "{r:?}"); - assert!(t.elapsed() >= Duration::from_millis(100) && t.elapsed() < Duration::from_secs(1), "{:?}", t.elapsed()); + assert!(t.elapsed() >= Duration::from_millis(100) && t.elapsed() < scaled(Duration::from_secs(1)), "{:?}", t.elapsed()); // and a lookup asked for after Cancel doesn't start assert!(matches!(lookup("slow.example", slow, Instant::now() + Duration::from_secs(30), &limits(&cancel)), Err(HttpError::Cancelled))); // a lookup that answers is used @@ -652,10 +655,11 @@ mod tests_connect { let cancel = AtomicBool::new(false); // non-routable addresses: each either hangs until the deadline or fails at once, never longer let addrs: Vec = (1..=5).map(|i| format!("10.255.255.{i}:81").parse().unwrap()).collect(); + // five full timeouts would take 2 s; one shared one 400 ms let t = Instant::now(); - let r = connect_any("blackhole", &addrs, Instant::now() + Duration::from_millis(400), &limits(&cancel)); + let r = connect_any("blackhole", &addrs, Instant::now() + scaled(Duration::from_millis(400)), &limits(&cancel)); assert!(r.is_err()); - assert!(t.elapsed() < Duration::from_millis(1500), "{:?}", t.elapsed()); + assert!(t.elapsed() < scaled(Duration::from_millis(1500)), "{:?}", t.elapsed()); } /// Content-Length is `1*DIGIT`: a sign, hex prefix, trailing junk or empty value is refused, not read as a length. diff --git a/crates/fetch/src/tests.rs b/crates/fetch/src/tests.rs index d144e08d29b..0fb78daed70 100644 --- a/crates/fetch/src/tests.rs +++ b/crates/fetch/src/tests.rs @@ -102,7 +102,8 @@ fn handle(mut s: TcpStream, hits: &AtomicUsize) { // half the file, then nothing let _ = s.write_all(format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", body.len()).as_bytes()); let _ = s.write_all(&body[..body.len() / 2]); - std::thread::sleep(Duration::from_secs(5)); + // (stretched with the client's timeouts, so it stays well past the stall timeout) + std::thread::sleep(lightcraft_testkit::scaled(Duration::from_secs(5))); } "slow" => { let _ = s.write_all(format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", body.len()).as_bytes()); @@ -139,8 +140,11 @@ fn tmp(name: &str) -> PathBuf { d } +/// Short timeouts, stretched by `LIGHTCRAFT_TIME_SCALE` (a loaded machine can leave a working +/// connection quiet for longer). fn quick() -> Options { - Options { connect_timeout: Duration::from_secs(2), stall_timeout: Duration::from_millis(600), attempts: 2, token_env: None } + use lightcraft_testkit::scaled; + Options { connect_timeout: scaled(Duration::from_secs(2)), stall_timeout: scaled(Duration::from_millis(600)), attempts: 2, token_env: None } } fn run(files: &[FileSpec<'_>], mirrors: &[String], dir: &Path, cancel: &AtomicBool) -> (Result<(), DownloadError>, Vec) { @@ -296,7 +300,8 @@ fn a_stalled_server_times_out_and_the_next_mirror_finishes() { let t = Instant::now(); let (r, _) = run(&[pinned()], &[format!("{}/stall", srv.base), format!("{}/good", srv.base)], &dir, &AtomicBool::new(false)); r.unwrap(); - assert!(t.elapsed() < Duration::from_secs(4), "{:?}", t.elapsed()); + // two stalled tries (600 ms each) and the good mirror, not two waits for the server to hang up (5 s each) + assert!(t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(4)), "{:?}", t.elapsed()); assert_eq!(std::fs::read(dir.join("model.bin")).unwrap(), payload()); let _ = std::fs::remove_dir_all(&dir); } @@ -307,14 +312,18 @@ fn cancel_stops_quickly_and_keeps_the_part_for_later() { let dir = tmp("cancel"); let cancel = Arc::new(AtomicBool::new(false)); let c = cancel.clone(); + let part = part_path(&dir, "model.bin"); + // cancel once part of the file is written (the slow server takes 6 s for the whole of it) std::thread::spawn(move || { - std::thread::sleep(Duration::from_millis(400)); + lightcraft_testkit::wait_until(Duration::from_secs(30), "the first bytes of the part", || { + std::fs::metadata(&part).is_ok_and(|m| m.len() > 0) + }); c.store(true, Ordering::SeqCst); }); let t = Instant::now(); let (r, _) = run(&[pinned()], &[format!("{}/slow", srv.base)], &dir, &cancel); assert_eq!(r, Err(DownloadError::Cancelled)); - assert!(t.elapsed() < Duration::from_secs(3), "{:?}", t.elapsed()); + assert!(t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(3)), "{:?}", t.elapsed()); assert!(!dir.join("model.bin").exists()); let kept = std::fs::metadata(part_path(&dir, "model.bin")).map(|m| m.len()).unwrap_or(0); assert!(kept > 0 && kept < 300_000, "{kept}"); From ccb71179e218879e0d4760742010b2cee5721152 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:01:53 -0500 Subject: [PATCH 18/33] test(catalog): #3064 the smart-album chain's time guard stretches with LIGHTCRAFT_TIME_SCALE It guards against exponential evaluation (minutes) of a case that takes microseconds, so it stays as a regression guard with its 2 s bound scaled by lightcraft_testkit::scaled. Co-Authored-By: Claude Opus 5.5 --- crates/catalog/src/tests.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/catalog/src/tests.rs b/crates/catalog/src/tests.rs index 8688d87a304..f01a8a861ef 100644 --- a/crates/catalog/src/tests.rs +++ b/crates/catalog/src/tests.rs @@ -816,7 +816,9 @@ fn chains_of_smart_albums_stay_linear() { } let start = std::time::Instant::now(); assert_eq!(c.album_count(AlbumId(26)), 20); - assert!(start.elapsed() < std::time::Duration::from_secs(2), "took {:?}", start.elapsed()); + // a regression guard, not a speed measurement: linear is microseconds, 2^25 passes take minutes + // (stretched by LIGHTCRAFT_TIME_SCALE for a loaded machine) + assert!(start.elapsed() < lightcraft_testkit::scaled(std::time::Duration::from_secs(2)), "took {:?}", start.elapsed()); } /// A smart album's own album filter (not only its rules) counts in loops: A filtered to "in A" is From 2a61127de160ba81ade8a676c801151a331977fa Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Tue, 27 Oct 2026 02:03:29 -0500 Subject: [PATCH 19/33] test(raw): #3064 the hostile-dimension time guard stretches with LIGHTCRAFT_TIME_SCALE "Hostile dimensions are rejected quickly" is a regression guard (the broken code takes far longer, not slightly), so it stays, with its bound scaled by lightcraft_testkit::scaled; it goes from 2 s to 5 s and says what it measured when it fails. (The fake-JPEG scan's wall-clock guard is gone on main: #2975 bounds that scan by counting its work.) Co-Authored-By: Claude Opus 5.5 --- crates/raw/tests/it/robust.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/raw/tests/it/robust.rs b/crates/raw/tests/it/robust.rs index 41a4ef302e2..79d3479f271 100644 --- a/crates/raw/tests/it/robust.rs +++ b/crates/raw/tests/it/robust.rs @@ -111,7 +111,10 @@ fn hostile_dimensions_are_rejected_quickly() { let bytes = TiffWriter::default().write(&[ifd]).unwrap(); let t0 = std::time::Instant::now(); assert!(lightcraft_raw::decode(&bytes).is_err()); - assert!(t0.elapsed().as_secs_f64() < 2.0); + // a regression guard, not a speed measurement: rejecting it takes milliseconds, reading the 7 GB + // frame it claims would take far longer (stretched by LIGHTCRAFT_TIME_SCALE for a loaded machine) + let limit = lightcraft_testkit::scaled(std::time::Duration::from_secs(5)); + assert!(t0.elapsed() < limit, "took {:?} (limit {limit:?})", t0.elapsed()); } /// A Bayer DNG whose `BlackLevel` is a FLOAT holding NaN (or infinity), and a float CFA DNG with From d5681e18f4d0223d45f1e903d2ae178311aae6c2 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:01:53 -0500 Subject: [PATCH 20/33] test(preview): #3064 the pool's no-second-wait checks compare with DROP_WAIT Dropping a pool after shutdown, or from its own job, must not wait for the running job again. The tests asserted under 1 s; the wrong behaviour waits DROP_WAIT (2 s), so they now assert under DROP_WAIT itself, which only a real wait can exceed. The waits that only stop a hung test scale with LIGHTCRAFT_TIME_SCALE, and the spin on the panicked worker has a limit. Co-Authored-By: Claude Opus 5.5 --- crates/preview/src/pool.rs | 2 +- crates/preview/src/tests.rs | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/crates/preview/src/pool.rs b/crates/preview/src/pool.rs index 9e966468501..f5c848a7990 100644 --- a/crates/preview/src/pool.rs +++ b/crates/preview/src/pool.rs @@ -45,7 +45,7 @@ struct Shared { } /// How long dropping a pool waits for the jobs still running (see [`JobPool::shutdown`]). -const DROP_WAIT: Duration = Duration::from_secs(2); +pub(crate) const DROP_WAIT: Duration = Duration::from_secs(2); /// A finished job. pub struct Done { diff --git a/crates/preview/src/tests.rs b/crates/preview/src/tests.rs index d667229f671..0559f71503c 100644 --- a/crates/preview/src/tests.rs +++ b/crates/preview/src/tests.rs @@ -234,7 +234,7 @@ fn pool_with_a_running_job() -> (JobPool, std::sync::mpsc::Sender<()>) { 1, Box::new(move || { let _ = started.send(()); - let _ = released.recv_timeout(std::time::Duration::from_secs(60)); + let _ = released.recv_timeout(lightcraft_testkit::scaled(std::time::Duration::from_secs(60))); }), ); lightcraft_testkit::recv_within(&has_started, std::time::Duration::from_secs(30), "the job starts"); @@ -274,20 +274,21 @@ fn pool_shutdown_reports_workers_and_drop_does_not_wait_again() { let t0 = std::time::Instant::now(); assert!(!p.shutdown(std::time::Duration::from_millis(100)), "the job is still running"); let waited = t0.elapsed(); - assert!(waited >= std::time::Duration::from_millis(100) && waited < std::time::Duration::from_secs(30), "{waited:?}"); + assert!(waited >= std::time::Duration::from_millis(100) && waited < lightcraft_testkit::scaled(std::time::Duration::from_secs(30)), "{waited:?}"); assert_eq!(p.live_workers(), 1); // stopped: nothing new is queued, and the job that ends is noticed p.submit(2, 0, 1, Box::new(|| ())); assert_eq!(p.queued(), 0); drop(release); - assert!(p.shutdown(std::time::Duration::from_secs(30))); + assert!(p.shutdown(lightcraft_testkit::scaled(std::time::Duration::from_secs(30)))); assert_eq!(p.live_workers(), 0); let (mut p, _release) = pool_with_a_running_job(); assert!(!p.shutdown(std::time::Duration::ZERO)); let t0 = std::time::Instant::now(); drop(p); - assert!(t0.elapsed() < std::time::Duration::from_secs(1), "dropped after a shutdown: no second wait"); + // a second wait would take DROP_WAIT (the job runs until `_release` goes) + assert!(t0.elapsed() < crate::pool::DROP_WAIT, "dropped after a shutdown: no second wait ({:?})", t0.elapsed()); } #[test] @@ -297,7 +298,7 @@ fn pool_drop_is_bounded_when_a_job_never_ends() { let t0 = std::time::Instant::now(); drop(p); let waited = t0.elapsed(); - assert!(waited >= std::time::Duration::from_secs(1) && waited < std::time::Duration::from_secs(30), "{waited:?}"); + assert!(waited >= std::time::Duration::from_secs(1) && waited < lightcraft_testkit::scaled(std::time::Duration::from_secs(30)), "{waited:?}"); } #[test] @@ -305,10 +306,8 @@ fn pool_stops_after_a_job_panicked_and_when_dropped_by_its_own_job() { // a worker that unwound is not waited for let mut p: JobPool = JobPool::new(1); p.submit(1, 0, 1, Box::new(|| panic!("a job panics (expected in this test)"))); - while p.live_workers() > 0 { - std::thread::yield_now(); - } - assert!(p.shutdown(std::time::Duration::from_secs(30))); + lightcraft_testkit::wait_until(std::time::Duration::from_secs(30), "the panicked worker ends", || p.live_workers() == 0); + assert!(p.shutdown(lightcraft_testkit::scaled(std::time::Duration::from_secs(30)))); // the last owner of a pool is one of its jobs: the worker must not wait for (or join) itself let slot: Arc>>> = Arc::new(std::sync::Mutex::new(None)); @@ -332,8 +331,9 @@ fn pool_stops_after_a_job_panicked_and_when_dropped_by_its_own_job() { }), ); *slot.lock().unwrap() = Some(p); - let waited = has_dropped.recv_timeout(std::time::Duration::from_secs(30)).unwrap(); - assert!(waited < std::time::Duration::from_secs(1), "{waited:?}"); + let waited = lightcraft_testkit::recv_within(&has_dropped, std::time::Duration::from_secs(30), "the job drops its own pool"); + // waiting for itself would take DROP_WAIT (a running job can't end while it waits) + assert!(waited < crate::pool::DROP_WAIT, "{waited:?}"); } #[test] @@ -682,16 +682,16 @@ fn pool_wait_keeps_the_finished_job_for_try_recv() { let mut p: JobPool = JobPool::new(1); assert!(!p.wait(std::time::Duration::from_millis(10)), "nothing runs"); p.submit(1, 0, 1, Box::new(|| 7)); - assert!(p.wait(std::time::Duration::from_secs(10))); + assert!(p.wait(lightcraft_testkit::scaled(std::time::Duration::from_secs(10)))); assert!(p.wait(std::time::Duration::ZERO), "kept until taken"); assert_eq!(p.try_recv().map(|d| d.result), Some(7)); assert!(p.try_recv().is_none()); } -/// Wait up to `secs` for `f` to hold. +/// Wait up to `secs` (stretched by `LIGHTCRAFT_TIME_SCALE`) for `f` to hold. fn eventually(secs: u64, mut f: impl FnMut() -> bool) -> bool { let t0 = std::time::Instant::now(); - while t0.elapsed() < std::time::Duration::from_secs(secs) { + while t0.elapsed() < lightcraft_testkit::scaled(std::time::Duration::from_secs(secs)) { if f() { return true; } @@ -719,7 +719,7 @@ fn a_slot_runs_one_job_at_a_time() { most.fetch_max(now, Ordering::SeqCst); let _ = started.send(n); if let Some(w) = wait { - let _ = w.recv_timeout(std::time::Duration::from_secs(30)); + let _ = w.recv_timeout(lightcraft_testkit::scaled(std::time::Duration::from_secs(30))); } running.fetch_sub(1, Ordering::SeqCst); n @@ -777,7 +777,7 @@ fn deferred_writes_are_bounded_and_drain_to_disk() { assert!(cache.queued_bytes() <= MAX_QUEUED_BYTES, "{} bytes queued", cache.queued_bytes()); } let t = std::time::Instant::now(); - while cache.queued_bytes() > 0 && t.elapsed() < std::time::Duration::from_secs(120) { + while cache.queued_bytes() > 0 && t.elapsed() < lightcraft_testkit::scaled(std::time::Duration::from_secs(120)) { std::thread::sleep(std::time::Duration::from_millis(20)); } assert_eq!(cache.queued_bytes(), 0, "the queue drains"); From 60e4af18606f94fb3bd40dfce2633c990af32d0b Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:08:52 -0500 Subject: [PATCH 21/33] test(ui-egui): #3064 the activity stack's half-second threshold is tested without the clock The panel's "has a row yet" rule moves into panels::activity::due, and a new test checks it at 0, 499 and 500 ms with made-up tasks, so a row shown too early fails every time instead of only when the first frame happens to finish within half a second. The wall-clock check stays, guarded as before, but finds the test's own task by id rather than taking the first in the list. demo() also waits until the demo's own tasks have ended, so a task that outlives half a second on a loaded machine is not counted as one of the test's rows. Every wait in the file scales with LIGHTCRAFT_TIME_SCALE. The album picker's 20 000-album check is a regression guard (about 20 ms in a debug build, seconds for a per-row scan), so it stays with its 1 s bound scaled. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/panels/activity.rs | 7 +- crates/ui-egui/src/tests/activity.rs | 100 ++++++++++++++------- crates/ui-egui/src/widgets/album_picker.rs | 4 +- 3 files changed, 75 insertions(+), 36 deletions(-) diff --git a/crates/ui-egui/src/panels/activity.rs b/crates/ui-egui/src/panels/activity.rs index dffdd1c4d33..d04d21a9280 100644 --- a/crates/ui-egui/src/panels/activity.rs +++ b/crates/ui-egui/src/panels/activity.rs @@ -23,6 +23,11 @@ pub const WIDTH: f32 = 280.0; const MARGIN: i8 = 8; const CROSS: f32 = 16.0; +/// Whether `task` has a row yet: from [`SHOW_AFTER_MS`] on. +pub(crate) fn due(task: &TaskInfo) -> bool { + task.age_ms >= SHOW_AFTER_MS +} + /// "3 of 25", "12 of 340 MB", "40 %"; empty while the amount of work isn't known. pub fn count_text(t: &TaskInfo) -> String { if t.total == 0 { @@ -62,7 +67,7 @@ pub fn show(app: &mut LightcraftApp, ctx: &egui::Context) { } // look again soon: a young task becomes due, bars move ctx.request_repaint_after(std::time::Duration::from_millis(100)); - let tasks: Vec<&TaskInfo> = all.iter().filter(|t| t.age_ms >= SHOW_AFTER_MS).collect(); + let tasks: Vec<&TaskInfo> = all.iter().filter(|t| due(t)).collect(); if tasks.is_empty() { return; } diff --git a/crates/ui-egui/src/tests/activity.rs b/crates/ui-egui/src/tests/activity.rs index bdeaccf4e90..3b7a6fdc24a 100644 --- a/crates/ui-egui/src/tests/activity.rs +++ b/crates/ui-egui/src/tests/activity.rs @@ -6,18 +6,25 @@ use lightcraft_engine::activity::{Cancel, TaskInfo, Unit}; use serde_json::json; use crate::headless::Headless; -use crate::panels::activity::{WIDTH, count_text}; +use crate::panels::activity::{SHOW_AFTER_MS, WIDTH, count_text, due}; use crate::{LightcraftApp, Services}; -// generous: merges and renders run on worker threads that a busy machine (a full workspace test run) slows a lot -const T: Duration = Duration::from_secs(60); +/// Generous: merges and renders run on worker threads that a busy machine (a full workspace test run) +/// slows a lot. Stretched by `LIGHTCRAFT_TIME_SCALE`. +fn t() -> Duration { + lightcraft_testkit::scaled(Duration::from_secs(60)) +} fn demo() -> Headless { let app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() }); let mut h = Headless::new(app, [1200.0, 800.0], 1.0); // let the first renders finish: a render still running when the test binary exits can crash in the // GPU driver's teardown (the preview pool doesn't join its workers) - h.settle(T); + h.settle(t()); + // and whatever the demo itself runs in the stack: on a loaded machine it outlives half a second + // and its row would be counted with the test's own + let ended = h.step_until(t(), |h| h.app.session.activity.list().is_empty()); + assert!(ended, "the demo's own tasks never ended: {:?}", h.app.session.activity.list()); h } @@ -44,7 +51,7 @@ fn wait_visible(h: &mut Headless) { fn click(h: &mut Headless, id: &str) { h.step(); h.step(); - assert_eq!(h.request("ui.clickWidget", json!({"id": id}), T)["ok"], true, "{id}"); + assert_eq!(h.request("ui.clickWidget", json!({"id": id}), t())["ok"], true, "{id}"); for _ in 0..4 { h.step(); } @@ -57,14 +64,16 @@ fn rows_appear_after_half_a_second_and_cancel_by_click() { g.progress(2, 5); h.step(); // a frame on a loaded machine can outlast the half second: only judge a task still young after it - if h.app.session.activity.list()[0].age_ms < crate::panels::activity::SHOW_AFTER_MS { + // (`a_task_has_a_row_from_half_a_second_on` checks the threshold itself without the clock) + let young = h.app.session.activity.list().iter().any(|t| t.id == g.id() && !due(t)); + if young { assert!(!has(&h, &format!("activity:row:{}", g.id())), "not before 0.5 s"); } wait_visible(&mut h); assert!(has(&h, &format!("activity:row:{}", g.id()))); click(&mut h, &format!("activity:cancel:{}", g.id())); assert!(g.is_cancelled()); - let inspect = h.request("ui.inspect", json!({}), T); + let inspect = h.request("ui.inspect", json!({}), t()); assert_eq!(inspect["result"]["activity"][0]["cancelling"], true, "{}", inspect["result"]["activity"]); } @@ -97,6 +106,29 @@ fn not_cancellable_rows_have_no_cross() { assert!(!has(&h, &format!("activity:cancel:{}", g.id()))); } +/// The threshold the stack draws by, without the wall clock (a test that waits for real time can't tell +/// a row shown too early from a frame that took half a second on a loaded machine). +#[test] +fn a_task_has_a_row_from_half_a_second_on() { + let task = |age_ms| TaskInfo { + id: 1, + kind: "export", + label: "Exporting".into(), + done: 0, + total: 0, + unit: Unit::Count, + detail: String::new(), + cancellable: true, + cancelling: false, + age_ms, + }; + assert_eq!(SHOW_AFTER_MS, 500); + assert!(!due(&task(0)), "a task just started has no row"); + assert!(!due(&task(SHOW_AFTER_MS - 1)), "nor one a moment short of half a second"); + assert!(due(&task(SHOW_AFTER_MS)), "half a second old: a row"); + assert!(due(&task(u64::MAX))); +} + #[test] fn count_text_by_unit() { let mut t = TaskInfo { @@ -127,7 +159,7 @@ fn stale_cancel_is_harmless() { wait_visible(&mut h); let id = g.id(); drop(g); - let r = h.request("engine.execute", json!({"command": "activity.cancel", "params": {"id": id}}), T); + let r = h.request("engine.execute", json!({"command": "activity.cancel", "params": {"id": id}}), t()); assert_eq!(r["ok"], false, "an error result, not a panic: {r}"); h.step(); assert!(h.app.ui.toast.is_none(), "{:?}", h.app.ui.toast); @@ -231,15 +263,15 @@ fn slow_writer(ms: u64) -> crate::SharedWrite { fn start_export(h: &mut Headless, n: usize) { let ids: Vec = h.app.session.visible_cloned().iter().take(n).map(|p| p.0).collect(); assert_eq!(ids.len(), n, "the demo library has {n} photos"); - h.request("engine.execute", json!({"command": "library.select", "params": {"ids": ids}}), T); - h.request("engine.execute", json!({"command": "dialog.export", "params": {}}), T); + h.request("engine.execute", json!({"command": "library.select", "params": {"ids": ids}}), t()); + h.request("engine.execute", json!({"command": "dialog.export", "params": {}}), t()); h.step(); if let Some(crate::state::Dialog::Export { full_size, resize, dir, .. }) = &mut h.app.ui.dialog { *full_size = false; *resize = lightcraft_engine::export::Resize::long_edge(64); *dir = "/lc-test-out".into(); } - let r = h.request("ui.dialog.confirm", json!({}), T); + let r = h.request("ui.dialog.confirm", json!({}), t()); assert_eq!(r["ok"], true, "{r}"); } @@ -249,12 +281,12 @@ fn export_shows_a_row_and_stops_on_activity_cancel() { h.app.services.write_shared = Some(slow_writer(200)); start_export(&mut h, 5); assert!(h.app.export.is_some(), "running in the background"); - let tasks = h.request("ui.inspect", json!({}), T)["result"]["activity"].clone(); + let tasks = h.request("ui.inspect", json!({}), t())["result"]["activity"].clone(); assert_eq!(tasks[0]["kind"], "export", "{tasks}"); assert_eq!(tasks[0]["total"], 5, "{tasks}"); let id = tasks[0]["id"].as_u64().unwrap(); - assert_eq!(h.request("engine.execute", json!({"command": "activity.cancel", "params": {"id": id}}), T)["ok"], true); - assert!(h.step_until(Duration::from_secs(60), |h| h.app.export.is_none())); + assert_eq!(h.request("engine.execute", json!({"command": "activity.cancel", "params": {"id": id}}), t())["ok"], true); + assert!(h.step_until(t(), |h| h.app.export.is_none())); assert!(h.app.session.activity.list().is_empty()); assert!(h.app.last_export_result.as_ref().is_some_and(|r| r["cancelled"] == true), "{:?}", h.app.last_export_result); assert!(!has(&h, "button:exportCancel"), "the old window is gone"); @@ -265,7 +297,7 @@ fn dead_export_worker_leaves_no_row() { let mut h = demo(); h.app.services.write_shared = Some(std::sync::Arc::new(|_: &str, _: &[u8]| -> Result<(), String> { panic!("synthetic writer panic") })); start_export(&mut h, 2); - assert!(h.step_until(Duration::from_secs(60), |h| h.app.export.is_none())); + assert!(h.step_until(t(), |h| h.app.export.is_none())); assert!(h.app.session.activity.list().is_empty()); } @@ -293,7 +325,7 @@ fn gated_import(tag: &str, n: usize, gate: Option= lightcraft_engine::import::batch_size() { let t0 = std::time::Instant::now(); - while !gate.load(std::sync::atomic::Ordering::Acquire) && t0.elapsed() < Duration::from_secs(60) { + while !gate.load(std::sync::atomic::Ordering::Acquire) && t0.elapsed() < t() { std::thread::sleep(Duration::from_millis(1)); } } @@ -306,7 +338,7 @@ fn gated_import(tag: &str, n: usize, gate: Option= 2))); + assert!(h.step_until(t(), |h| h.app.import.as_ref().is_some_and(|t| t.imported >= 2))); let tasks = h.app.session.activity.list(); assert_eq!(tasks.first().map(|t| t.kind), Some("import"), "{tasks:?}"); h.app.session.activity.cancel(tasks[0].id).unwrap(); - assert!(h.step_until(Duration::from_secs(60), |h| h.app.import.is_none())); + assert!(h.step_until(t(), |h| h.app.import.is_none())); assert!(h.app.session.activity.list().is_empty()); assert!(h.app.ui.toast.as_ref().is_some_and(|t| t.0.starts_with("Import cancelled")), "{:?}", h.app.ui.toast); assert!(h.app.session.catalog.len() < n, "stopped part-way"); @@ -335,15 +367,15 @@ fn a_photo_chosen_during_an_import_stays_selected() { let gate = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); let (mut h, dir) = gated_import("chosen", 20, Some(gate.clone())); crate::import::start_paths(&mut h.app, vec![dir.to_string_lossy().to_string()]).unwrap(); - assert!(h.step_until(Duration::from_secs(60), |h| h.app.import.as_ref().is_some_and(|t| t.imported >= 2))); + assert!(h.step_until(t(), |h| h.app.import.as_ref().is_some_and(|t| t.imported >= 2))); // the newest photo so far, never the import's first let chosen = h.app.session.catalog.photos().map(|p| p.id).max().expect("photos arrived"); - let r = h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [chosen.0]}}), T); + let r = h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [chosen.0]}}), t()); assert_eq!(r["ok"], true, "{r}"); // held at the gate until now, so the import cannot have ended however slowly the frames ran assert!(h.app.import.is_some(), "still importing"); gate.store(true, std::sync::atomic::Ordering::Release); - assert!(h.step_until(Duration::from_secs(60), |h| h.app.import.is_none())); + assert!(h.step_until(t(), |h| h.app.import.is_none())); assert_eq!(h.app.session.catalog.len(), 20, "all imported"); assert_eq!(h.app.session.selection.active, Some(chosen)); let _ = std::fs::remove_dir_all(&dir); @@ -359,7 +391,7 @@ fn scan_shows_a_row_and_cancel_closes_it() { assert!(tasks[0].cancellable); h.app.session.activity.cancel(tasks[0].id).unwrap(); // like the old Cancel: the scan is dropped at once, without waiting for the file being read - assert!(h.step_until(Duration::from_secs(10), |h| h.app.scan.is_none())); + assert!(h.step_until(lightcraft_testkit::scaled(Duration::from_secs(10)), |h| h.app.scan.is_none())); assert!(h.app.session.activity.list().is_empty()); for _ in 0..3 { h.step(); @@ -385,7 +417,7 @@ fn bracket(tag: &str) -> (Headless, std::path::PathBuf) { session.execute("library.select", &json!({"ids": ids})).unwrap(); let app = LightcraftApp::new(session, Services { png: None, ..Default::default() }); let mut h = Headless::new(app, [1200.0, 800.0], 1.0); - h.settle(T); + h.settle(t()); (h, dir) } @@ -401,7 +433,7 @@ fn final_merge_shows_a_row_and_cancels() { assert!(rows[0].cancellable); assert_eq!(rows[0].total, 1000); h.app.session.activity.cancel(rows[0].id).unwrap(); - assert!(h.step_until(T, |h| h.app.merge.final_task.is_none())); + assert!(h.step_until(t(), |h| h.app.merge.final_task.is_none())); assert!(h.app.merge.last_result.is_none(), "nothing merged"); assert_eq!(h.app.session.catalog.len(), before, "no photo added"); assert!(h.app.session.activity.list().is_empty()); @@ -426,7 +458,7 @@ fn merge_dialog_remembers_the_last_settings_per_kind() { crate::merge::start_final(&mut h.app, &opts).unwrap(); let row = h.app.session.activity.list()[0].id; h.app.session.activity.cancel(row).unwrap(); - assert!(h.step_until(T, |h| h.app.merge.final_task.is_none())); + assert!(h.step_until(t(), |h| h.app.merge.final_task.is_none())); crate::merge::open(&mut h.app, "merge.hdr").unwrap(); let Some(crate::state::Dialog::Merge { opts: shown }) = &h.app.ui.dialog else { panic!("no merge dialog") }; assert_eq!(shown, &opts); @@ -452,7 +484,7 @@ fn merge_dialog_retries_a_failed_preview_when_it_opens_again() { std::fs::rename(&file, &away).unwrap(); crate::merge::open(&mut h.app, "merge.panorama").unwrap(); h.step(); - assert!(h.step_until(super::ui_settle_timeout(), |h| h.app.merge.preview_task.is_none())); + assert!(h.step_until(lightcraft_testkit::scaled(super::ui_settle_timeout()), |h| h.app.merge.preview_task.is_none())); assert!(h.app.merge.error.is_some(), "the preview failed"); h.app.ui.dialog = None; h.step(); @@ -464,7 +496,7 @@ fn merge_dialog_retries_a_failed_preview_when_it_opens_again() { h.app.merge.preview.is_some() || h.app.merge.preview_task.is_some() || h.app.merge.error.is_some(), "the dialog shows a preview, one being made, or why not" ); - assert!(h.step_until(super::ui_settle_timeout(), |h| h.app.merge.preview_task.is_none())); + assert!(h.step_until(lightcraft_testkit::scaled(super::ui_settle_timeout()), |h| h.app.merge.preview_task.is_none())); assert!(h.app.merge.preview.is_some(), "{:?}", h.app.merge.error); h.app.ui.dialog = None; h.step(); @@ -500,21 +532,21 @@ fn batch_merge_runs_each_set_in_turn() { let app = LightcraftApp::new(session, Services { png: None, ..Default::default() }); let mut h = Headless::new(app, [1200.0, 800.0], 1.0); // the merge ordering does not need the grid's nine thumbnails; one Detail render is enough - let r = h.request("ui.set", json!({"view": "detail", "filmstrip": false}), T); + let r = h.request("ui.set", json!({"view": "detail", "filmstrip": false}), t()); assert_eq!(r["ok"], true, "{r}"); - assert!(h.settle(T), "the imported photos and the active preview settle"); + assert!(h.settle(t()), "the imported photos and the active preview settle"); let r = crate::menus::run_ui_command(&mut h.app, "merge.hdrBatch", &json!({})).unwrap().unwrap(); assert_eq!(r["sets"], 3); assert_eq!(h.app.merge.batch.len(), 2, "one running, two queued"); let rows = h.app.session.activity.list(); assert_eq!((rows.len(), rows.first().map(|r| r.kind)), (1, Some("merge"))); // the first merge finishes and the second starts by itself - assert!(h.step_until(super::ui_settle_timeout(), |h| h.app.merge.batch.len() == 1 && h.app.merge.final_task.is_some())); + assert!(h.step_until(lightcraft_testkit::scaled(super::ui_settle_timeout()), |h| h.app.merge.batch.len() == 1 && h.app.merge.final_task.is_some())); assert_eq!(h.app.session.catalog.len(), 10); // cancelling the second drops the third let rows = h.app.session.activity.list(); h.app.session.activity.cancel(rows[0].id).unwrap(); - assert!(h.step_until(T, |h| h.app.merge.final_task.is_none())); + assert!(h.step_until(t(), |h| h.app.merge.final_task.is_none())); assert!(h.app.merge.batch.is_empty()); assert_eq!(h.app.session.catalog.len(), 10, "nothing more merged"); assert!(h.app.session.activity.list().is_empty()); @@ -534,7 +566,7 @@ fn find_missing_shows_an_indeterminate_row() { assert_eq!(rows.len(), 1, "{rows:?}"); assert_eq!((rows[0].kind, rows[0].label.as_str()), ("findMissing", "Find Missing Photos")); assert_eq!((rows[0].total, rows[0].cancellable), (0, false), "indeterminate, no ✕"); - assert!(h.step_until(T, |h| h.app.tasks.is_empty())); + assert!(h.step_until(t(), |h| h.app.tasks.is_empty())); assert!(h.app.session.activity.list().is_empty(), "the row goes with the task"); let _ = std::fs::remove_dir_all(dir); } @@ -546,7 +578,7 @@ fn auto_import_listing_has_no_row() { std::fs::create_dir_all(&dir).unwrap(); h.app.session.import_defaults.auto_folder = Some(dir.to_string_lossy().to_string()); // the watched folder is listed every few seconds, quietly - assert!(h.step_until(Duration::from_secs(10), |h| h.app.tasks.is_running("Auto Import"))); + assert!(h.step_until(lightcraft_testkit::scaled(Duration::from_secs(10)), |h| h.app.tasks.is_running("Auto Import"))); assert!(h.app.session.activity.list().is_empty(), "{:?}", h.app.session.activity.list()); h.app.session.import_defaults.auto_folder = None; let _ = std::fs::remove_dir_all(dir); diff --git a/crates/ui-egui/src/widgets/album_picker.rs b/crates/ui-egui/src/widgets/album_picker.rs index a88e5f1aaf7..02152b7b81e 100644 --- a/crates/ui-egui/src/widgets/album_picker.rs +++ b/crates/ui-egui/src/widgets/album_picker.rs @@ -474,7 +474,9 @@ mod tests { let open: Vec = (0..200u64).map(|f| f * 101 + 1).collect(); assert_eq!(visible_tree(&v, &open).len(), v.len()); assert_eq!(search(&v, "folder 7 wedding").len(), want); - assert!(start.elapsed() < std::time::Duration::from_secs(1), "took {:?}", start.elapsed()); + // a regression guard, not a speed measurement: this takes some 20 ms in a debug build; a path + // worked out per row by scanning every album takes seconds (stretched by LIGHTCRAFT_TIME_SCALE) + assert!(start.elapsed() < lightcraft_testkit::scaled(std::time::Duration::from_secs(1)), "took {:?}", start.elapsed()); } /// Every word typed is in the album's path (folders included), in any case; names that start From d3ff89ec81b8a12fd6241f6d3143d737d4d7cfba Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:17:17 -0500 Subject: [PATCH 22/33] test(engine): #3064 device scans, card set-up and the SAM 3 download hold a gate instead of timing the caller Three more "the caller never waits" checks compared elapsed time with a fixed bound (750 ms, 500 ms, 1 s) that a loaded machine can exceed. The device scanner and the card set-up now wait on a gate the test opens after asserting the call returned with the work still held; the SAM 3 download test checks the download is still running while the local mirror holds its answer, and asks for an AI mask during that hold. The waits that only stop a hung test scale with LIGHTCRAFT_TIME_SCALE. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/denoise/gpu_tests.rs | 20 +++++++++++++----- crates/engine/src/devices.rs | 28 +++++++++++++++++--------- crates/engine/src/tests/segment.rs | 15 +++++++------- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/crates/engine/src/denoise/gpu_tests.rs b/crates/engine/src/denoise/gpu_tests.rs index 71081d9c119..b0d050ad57f 100644 --- a/crates/engine/src/denoise/gpu_tests.rs +++ b/crates/engine/src/denoise/gpu_tests.rs @@ -303,20 +303,30 @@ fn a_set_up_makes_the_folder_of_its_marker() { fn a_set_up_that_takes_too_long_is_given_up_on() { let dir = folder("slowsetup"); let marker = dir.join(GPU_SETUP_MARKER); - let started = web_time::Instant::now(); - let slow = || -> Result<(), String> { - std::thread::sleep(Duration::from_millis(600)); + // a card whose set-up is stuck until the test lets it go (it gives up after a while, so a caller + // that waits for it fails the test instead of hanging it) + let gate = Arc::new(AtomicBool::new(false)); + let finished = Arc::new(AtomicBool::new(false)); + let (g, f) = (gate.clone(), finished.clone()); + let slow = move || -> Result<(), String> { + let t = web_time::Instant::now(); + while !g.load(Ordering::SeqCst) && t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(5)) { + std::thread::sleep(Duration::from_millis(1)); + } + f.store(true, Ordering::SeqCst); Ok(()) }; let r = set_up_card(&marker, Duration::from_millis(50), slow, || ()); assert!(r.as_ref().is_err_and(|e| e.contains("took longer") && worth_retrying(e)), "{r:?}"); - assert!(started.elapsed() < Duration::from_millis(500), "the caller does not wait for the card"); + assert!(!finished.load(Ordering::SeqCst), "the caller does not wait for the card"); // while the first is still stuck, another set-up of the same model does not start a second one let again = set_up_card(&marker, Duration::from_secs(5), || Ok(()), || ()); assert!(again.is_err_and(|e| e.contains("still setting up") && worth_retrying(&e))); // once it ends its marker goes, and the card can be set up again + gate.store(true, Ordering::SeqCst); + let started = web_time::Instant::now(); while marker.exists() { - assert!(started.elapsed() < Duration::from_secs(10), "the stuck set-up ended"); + assert!(started.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(10)), "the stuck set-up ended"); std::thread::sleep(Duration::from_millis(20)); } while SETTING_UP.lock().unwrap().iter().any(|m| m == &marker) { diff --git a/crates/engine/src/devices.rs b/crates/engine/src/devices.rs index 366cb5273fd..c6561b6bd6a 100644 --- a/crates/engine/src/devices.rs +++ b/crates/engine/src/devices.rs @@ -247,10 +247,10 @@ mod tests { Device { name: name.into(), path: format!("/v/{name}/DCIM"), root: format!("/v/{name}") } } - /// Wait (bounded) for `f`. + /// Wait (bounded: 20 s, stretched by `LIGHTCRAFT_TIME_SCALE`) for `f`. fn until(f: impl Fn() -> bool) -> bool { let t = std::time::Instant::now(); - while t.elapsed() < Duration::from_secs(20) { + while t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(20)) { if f() { return true; } @@ -261,15 +261,21 @@ mod tests { #[test] fn slow_scans_never_block_and_hot_plug_still_arrives() { - // a scanner that takes 1.5 s (a sleeping network drive) and counts its runs - const SLOW: Duration = Duration::from_millis(1500); + // a scanner held until the test opens its gate (a sleeping network drive) that counts its runs; + // it gives up after a while so a get() that waits for it fails the test instead of hanging it let mounted = Arc::new(Mutex::new(Vec::::new())); let runs = Arc::new(AtomicUsize::new(0)); - let (m, r) = (mounted.clone(), runs.clone()); + let finished = Arc::new(AtomicUsize::new(0)); + let gate = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let (m, r, f, g) = (mounted.clone(), runs.clone(), finished.clone(), gate.clone()); let watch = DeviceWatch::new( move || { r.fetch_add(1, Ordering::SeqCst); - std::thread::sleep(SLOW); + let t = std::time::Instant::now(); + while !g.load(Ordering::SeqCst) && t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(5)) { + std::thread::sleep(Duration::from_millis(1)); + } + f.fetch_add(1, Ordering::SeqCst); m.lock().unwrap().clone() }, Duration::from_millis(20), @@ -280,21 +286,23 @@ mod tests { c.fetch_add(1, Ordering::SeqCst); }); // cold: served at once (empty) while one scan runs in the background - let t = std::time::Instant::now(); for _ in 0..50 { assert!(watch.get().is_empty()); } - assert!(t.elapsed() < SLOW / 2, "get() waited for the scan: {:?}", t.elapsed()); + // the scan is still held: a get() that waited for it could not have returned yet + assert_eq!(finished.load(Ordering::SeqCst), 0, "get() waited for the scan"); assert!(watch.refreshing()); + gate.store(true, Ordering::SeqCst); assert!(until(|| !watch.refreshing())); assert_eq!(runs.load(Ordering::SeqCst), 1, "requests while scanning are coalesced"); assert_eq!(changes.load(Ordering::SeqCst), 0, "nothing changed"); // a card is plugged in: the next stale request starts a rescan, still without waiting + gate.store(false, Ordering::SeqCst); mounted.lock().unwrap().push(card("EOS_DIGITAL")); std::thread::sleep(Duration::from_millis(40)); - let t = std::time::Instant::now(); assert!(watch.get().is_empty(), "the old list is served while rescanning"); - assert!(t.elapsed() < SLOW / 2, "warm get() waited: {:?}", t.elapsed()); + assert_eq!(finished.load(Ordering::SeqCst), 1, "warm get() waited for the rescan"); + gate.store(true, Ordering::SeqCst); assert!(until(|| changes.load(Ordering::SeqCst) == 1), "a changed scan notifies"); assert_eq!(watch.get(), [card("EOS_DIGITAL")]); // explicit requests scan synchronously diff --git a/crates/engine/src/tests/segment.rs b/crates/engine/src/tests/segment.rs index 51d6e26e8fe..c2ea1e63a54 100644 --- a/crates/engine/src/tests/segment.rs +++ b/crates/engine/src/tests/segment.rs @@ -92,7 +92,7 @@ fn a_failing_download_ends_with_an_error_and_never_blocks() { line.clear(); } if let Some(released) = first.take() { - let _ = released.recv_timeout(Duration::from_secs(10)); + let _ = released.recv_timeout(lightcraft_testkit::scaled(Duration::from_secs(30))); } let _ = c.write_all(b"HTTP/1.1 404 Not Found\r\nContent-Length: 13\r\nConnection: close\r\n\r\n

404

\n"); } @@ -103,22 +103,23 @@ fn a_failing_download_ends_with_an_error_and_never_blocks() { let mut s = Session::with_demo(); s.segmenter.dir = Some(dir.clone()); s.segmenter.mirrors_file = Some(mirrors.clone()); - let t = Instant::now(); let r = s.execute("segment.model.download", &json!({"acknowledged": true})).unwrap(); assert_eq!(r["started"], true); - assert!(t.elapsed() < Duration::from_secs(1), "starting a download returns at once"); + // the mirror holds its answer until `release`: still running means the call did not wait for it + assert!(s.segmenter.download_status().running, "starting a download returns at once"); // a second request while it runs starts nothing let again = s.execute("segment.model.download", &json!({"acknowledged": true})).unwrap(); assert!(again["started"] == false || again["downloading"] == false); + // AI mask requests meanwhile: an error while the mirror still holds the download, never a wait for it + let e = s.execute("mask.add", &json!({"kind": "prompt", "text": "sky"})).unwrap_err().to_string(); + assert!(e.contains(NOT_INSTALLED), "{e}"); + assert!(s.segmenter.download_status().running, "the AI mask request did not wait for the download"); let _ = release.send(()); let t = Instant::now(); while s.segmenter.download_status().running { - assert!(t.elapsed() < Duration::from_secs(30), "the download never ended"); - // AI mask requests meanwhile: an error at once, never a wait - let q = Instant::now(); + assert!(t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(30)), "the download never ended"); let e = s.execute("mask.add", &json!({"kind": "prompt", "text": "sky"})).unwrap_err().to_string(); assert!(e.contains(NOT_INSTALLED), "{e}"); - assert!(q.elapsed() < Duration::from_millis(500)); std::thread::sleep(Duration::from_millis(20)); } let st = s.segmenter.download_status(); From 1d287472463cfafebe1821e5a2ec157787b97325 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:14:24 -0500 Subject: [PATCH 23/33] test(fetch): #3064 timing bounds never shrink below their unscaled values At LIGHTCRAFT_TIME_SCALE=0.1 the cancel test asked for >= 100 ms and < 100 ms at once, and the stalled-server test's 400 ms bound fell under two 60 ms stalls plus the server's pace. The bounds, client timeouts and test server times now stretch with the scale but never shrink (they sit on the client's fixed 250 ms poll and the server's fixed pace). The cancel lands 100 ms after the resolver has started rather than 100 ms after the test began, and its upper bound is that 100 ms plus the margin. Passes at scales 0.1, 1 and 3; the two tests failed at 0.1 before. Co-Authored-By: Claude Opus 5.5 --- crates/fetch/src/http.rs | 37 ++++++++++++++++++++++++++----------- crates/fetch/src/tests.rs | 15 ++++++++++----- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/crates/fetch/src/http.rs b/crates/fetch/src/http.rs index 026767ba45a..30d6fde29b0 100644 --- a/crates/fetch/src/http.rs +++ b/crates/fetch/src/http.rs @@ -611,10 +611,14 @@ pub fn get(url: &Url, headers: &[(&str, String)], limits: &Limits) -> Result Duration { + lightcraft_testkit::scaled(d).max(d) + } fn limits(cancel: &AtomicBool) -> Limits<'_> { - Limits { connect: scaled(Duration::from_millis(300)), stall: scaled(Duration::from_secs(1)), cancel } + Limits { connect: stretched(Duration::from_millis(300)), stall: stretched(Duration::from_secs(1)), cancel } } /// A name lookup that hangs is given up on at the connect timeout, and Cancel stops it at once (issue #1532). @@ -623,24 +627,35 @@ mod tests_connect { let cancel = AtomicBool::new(false); // (every time here is stretched by LIGHTCRAFT_TIME_SCALE, so the resolver stays far slower than the bounds) let slow = || { - std::thread::sleep(scaled(Duration::from_secs(10))); + std::thread::sleep(stretched(Duration::from_secs(10))); Ok(Vec::new()) }; let t = Instant::now(); - let r = lookup("slow.example", slow, Instant::now() + scaled(Duration::from_millis(300)), &limits(&cancel)); + let r = lookup("slow.example", slow, Instant::now() + stretched(Duration::from_millis(300)), &limits(&cancel)); assert!(matches!(&r, Err(HttpError::Connect(m)) if m.contains("timed out")), "{r:?}"); - assert!(t.elapsed() < scaled(Duration::from_secs(2)), "{:?}", t.elapsed()); - // Cancel while the lookup is waiting on the resolver, not before it starts + assert!(t.elapsed() < stretched(Duration::from_secs(2)), "{:?}", t.elapsed()); + // Cancel while the lookup is waiting on the resolver (100 ms after it started), not before it starts + let (began, has_begun) = std::sync::mpsc::channel(); + let signalling = move || { + let _ = began.send(()); + std::thread::sleep(stretched(Duration::from_secs(10))); + Ok(Vec::new()) + }; let t = Instant::now(); let r = std::thread::scope(|scope| { - scope.spawn(|| { + let cancel = &cancel; + scope.spawn(move || { + lightcraft_testkit::recv_within(&has_begun, Duration::from_secs(30), "the resolver starts"); std::thread::sleep(Duration::from_millis(100)); cancel.store(true, Ordering::Relaxed); }); - lookup("slow.example", slow, Instant::now() + Duration::from_secs(30), &limits(&cancel)) + lookup("slow.example", signalling, Instant::now() + stretched(Duration::from_secs(30)), &limits(cancel)) }); assert!(matches!(r, Err(HttpError::Cancelled)), "{r:?}"); - assert!(t.elapsed() >= Duration::from_millis(100) && t.elapsed() < scaled(Duration::from_secs(1)), "{:?}", t.elapsed()); + // at least the 100 ms before Cancel; then noticed within a TICK (the margin is for a slow machine) + let waited = t.elapsed(); + let limit = Duration::from_millis(100) + stretched(Duration::from_millis(900)); + assert!(waited >= Duration::from_millis(100) && waited < limit, "{waited:?} (limit {limit:?})"); // and a lookup asked for after Cancel doesn't start assert!(matches!(lookup("slow.example", slow, Instant::now() + Duration::from_secs(30), &limits(&cancel)), Err(HttpError::Cancelled))); // a lookup that answers is used @@ -657,9 +672,9 @@ mod tests_connect { let addrs: Vec = (1..=5).map(|i| format!("10.255.255.{i}:81").parse().unwrap()).collect(); // five full timeouts would take 2 s; one shared one 400 ms let t = Instant::now(); - let r = connect_any("blackhole", &addrs, Instant::now() + scaled(Duration::from_millis(400)), &limits(&cancel)); + let r = connect_any("blackhole", &addrs, Instant::now() + stretched(Duration::from_millis(400)), &limits(&cancel)); assert!(r.is_err()); - assert!(t.elapsed() < scaled(Duration::from_millis(1500)), "{:?}", t.elapsed()); + assert!(t.elapsed() < stretched(Duration::from_millis(1500)), "{:?}", t.elapsed()); } /// Content-Length is `1*DIGIT`: a sign, hex prefix, trailing junk or empty value is refused, not read as a length. diff --git a/crates/fetch/src/tests.rs b/crates/fetch/src/tests.rs index 0fb78daed70..187b185ae08 100644 --- a/crates/fetch/src/tests.rs +++ b/crates/fetch/src/tests.rs @@ -103,7 +103,7 @@ fn handle(mut s: TcpStream, hits: &AtomicUsize) { let _ = s.write_all(format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", body.len()).as_bytes()); let _ = s.write_all(&body[..body.len() / 2]); // (stretched with the client's timeouts, so it stays well past the stall timeout) - std::thread::sleep(lightcraft_testkit::scaled(Duration::from_secs(5))); + std::thread::sleep(stretched(Duration::from_secs(5))); } "slow" => { let _ = s.write_all(format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", body.len()).as_bytes()); @@ -140,11 +140,16 @@ fn tmp(name: &str) -> PathBuf { d } +/// `d` stretched by `LIGHTCRAFT_TIME_SCALE`, never shrunk: the timeouts and bounds here sit on the +/// test server's fixed pace (20 ms per kilobyte) and the client's fixed 250 ms poll. +fn stretched(d: Duration) -> Duration { + lightcraft_testkit::scaled(d).max(d) +} + /// Short timeouts, stretched by `LIGHTCRAFT_TIME_SCALE` (a loaded machine can leave a working /// connection quiet for longer). fn quick() -> Options { - use lightcraft_testkit::scaled; - Options { connect_timeout: scaled(Duration::from_secs(2)), stall_timeout: scaled(Duration::from_millis(600)), attempts: 2, token_env: None } + Options { connect_timeout: stretched(Duration::from_secs(2)), stall_timeout: stretched(Duration::from_millis(600)), attempts: 2, token_env: None } } fn run(files: &[FileSpec<'_>], mirrors: &[String], dir: &Path, cancel: &AtomicBool) -> (Result<(), DownloadError>, Vec) { @@ -301,7 +306,7 @@ fn a_stalled_server_times_out_and_the_next_mirror_finishes() { let (r, _) = run(&[pinned()], &[format!("{}/stall", srv.base), format!("{}/good", srv.base)], &dir, &AtomicBool::new(false)); r.unwrap(); // two stalled tries (600 ms each) and the good mirror, not two waits for the server to hang up (5 s each) - assert!(t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(4)), "{:?}", t.elapsed()); + assert!(t.elapsed() < stretched(Duration::from_secs(4)), "{:?}", t.elapsed()); assert_eq!(std::fs::read(dir.join("model.bin")).unwrap(), payload()); let _ = std::fs::remove_dir_all(&dir); } @@ -323,7 +328,7 @@ fn cancel_stops_quickly_and_keeps_the_part_for_later() { let t = Instant::now(); let (r, _) = run(&[pinned()], &[format!("{}/slow", srv.base)], &dir, &cancel); assert_eq!(r, Err(DownloadError::Cancelled)); - assert!(t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(3)), "{:?}", t.elapsed()); + assert!(t.elapsed() < stretched(Duration::from_secs(3)), "{:?}", t.elapsed()); assert!(!dir.join("model.bin").exists()); let kept = std::fs::metadata(part_path(&dir, "model.bin")).map(|m| m.len()).unwrap_or(0); assert!(kept > 0 && kept < 300_000, "{kept}"); From a8d4563272da24a3b54ae0ad9b51325652b3a824 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:15:53 -0500 Subject: [PATCH 24/33] test(preview): #3064 the no-second-wait checks count the pool's waits instead of timing them Dropping a pool after a shutdown, or from its own job, was checked with `< DROP_WAIT`: still a 2 s wall-clock bound that a descheduled test thread can exceed. The pool now counts, in test builds only (a #[cfg(test)] counter that release code never touches), how often its stop waited for a running job, and the tests assert those drops didn't wait. Reintroducing either wait fails them. DROP_WAIT is private again. The job hold and the bounds above a fixed lower one stretch with LIGHTCRAFT_TIME_SCALE but never shrink below their unscaled values. Co-Authored-By: Claude Opus 5.5 --- crates/preview/src/pool.rs | 16 +++++++++++++++- crates/preview/src/tests.rs | 31 +++++++++++++++++++------------ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/crates/preview/src/pool.rs b/crates/preview/src/pool.rs index f5c848a7990..7d586c9e9f9 100644 --- a/crates/preview/src/pool.rs +++ b/crates/preview/src/pool.rs @@ -42,10 +42,14 @@ struct Shared { live: Mutex>, #[cfg(not(target_arch = "wasm32"))] ended: Condvar, + /// Tests: how many times [`stop`] waited for a running job (a drop after a shutdown, or by + /// the pool's own job, must not wait at all). + #[cfg(all(test, not(target_arch = "wasm32")))] + stop_waits: Arc, } /// How long dropping a pool waits for the jobs still running (see [`JobPool::shutdown`]). -pub(crate) const DROP_WAIT: Duration = Duration::from_secs(2); +const DROP_WAIT: Duration = Duration::from_secs(2); /// A finished job. pub struct Done { @@ -87,6 +91,8 @@ impl JobPool { live: Mutex::new(Vec::new()), #[cfg(not(target_arch = "wasm32"))] ended: Condvar::new(), + #[cfg(all(test, not(target_arch = "wasm32")))] + stop_waits: Arc::default(), }), tx, rx, @@ -166,6 +172,12 @@ impl JobPool { } } + /// Tests: the count of [`stop`]'s waits for a running job (it outlives the pool). + #[cfg(all(test, not(target_arch = "wasm32")))] + pub(crate) fn stop_waits(&self) -> Arc { + self.shared.stop_waits.clone() + } + /// Worker threads that have not ended (0 before the first job, and on wasm). pub fn live_workers(&self) -> usize { #[cfg(not(target_arch = "wasm32"))] @@ -329,6 +341,8 @@ fn stop(shared: &Shared, workers: &mut Vec = live.iter().copied().filter(|id| *id != me).collect(); diff --git a/crates/preview/src/tests.rs b/crates/preview/src/tests.rs index 0559f71503c..94a6e6ed788 100644 --- a/crates/preview/src/tests.rs +++ b/crates/preview/src/tests.rs @@ -4,6 +4,12 @@ use lightcraft_raster::Rgba8; use super::*; +/// `d` stretched by `LIGHTCRAFT_TIME_SCALE`, never shrunk: for a job held past the pool's fixed +/// 2 s drop wait, and for bounds above a fixed lower one. +fn stretched(d: std::time::Duration) -> std::time::Duration { + lightcraft_testkit::scaled(d).max(d) +} + #[test] fn cleared_generation_cannot_read_or_repopulate_memory_or_disk() { let dir = std::env::temp_dir().join(format!("lc-thumbnail-generation-{}-{}", std::process::id(), next_tick())); @@ -234,7 +240,7 @@ fn pool_with_a_running_job() -> (JobPool, std::sync::mpsc::Sender<()>) { 1, Box::new(move || { let _ = started.send(()); - let _ = released.recv_timeout(lightcraft_testkit::scaled(std::time::Duration::from_secs(60))); + let _ = released.recv_timeout(stretched(std::time::Duration::from_secs(60))); }), ); lightcraft_testkit::recv_within(&has_started, std::time::Duration::from_secs(30), "the job starts"); @@ -274,7 +280,7 @@ fn pool_shutdown_reports_workers_and_drop_does_not_wait_again() { let t0 = std::time::Instant::now(); assert!(!p.shutdown(std::time::Duration::from_millis(100)), "the job is still running"); let waited = t0.elapsed(); - assert!(waited >= std::time::Duration::from_millis(100) && waited < lightcraft_testkit::scaled(std::time::Duration::from_secs(30)), "{waited:?}"); + assert!(waited >= std::time::Duration::from_millis(100) && waited < stretched(std::time::Duration::from_secs(30)), "{waited:?}"); assert_eq!(p.live_workers(), 1); // stopped: nothing new is queued, and the job that ends is noticed p.submit(2, 0, 1, Box::new(|| ())); @@ -285,10 +291,11 @@ fn pool_shutdown_reports_workers_and_drop_does_not_wait_again() { let (mut p, _release) = pool_with_a_running_job(); assert!(!p.shutdown(std::time::Duration::ZERO)); - let t0 = std::time::Instant::now(); + let waits = p.stop_waits(); + let before = waits.load(std::sync::atomic::Ordering::SeqCst); drop(p); - // a second wait would take DROP_WAIT (the job runs until `_release` goes) - assert!(t0.elapsed() < crate::pool::DROP_WAIT, "dropped after a shutdown: no second wait ({:?})", t0.elapsed()); + // (the job runs until `_release` goes: a second wait would have had something to wait for) + assert_eq!(waits.load(std::sync::atomic::Ordering::SeqCst), before, "dropped after a shutdown: no second wait"); } #[test] @@ -298,7 +305,7 @@ fn pool_drop_is_bounded_when_a_job_never_ends() { let t0 = std::time::Instant::now(); drop(p); let waited = t0.elapsed(); - assert!(waited >= std::time::Duration::from_secs(1) && waited < lightcraft_testkit::scaled(std::time::Duration::from_secs(30)), "{waited:?}"); + assert!(waited >= std::time::Duration::from_secs(1) && waited < stretched(std::time::Duration::from_secs(30)), "{waited:?}"); } #[test] @@ -313,6 +320,7 @@ fn pool_stops_after_a_job_panicked_and_when_dropped_by_its_own_job() { let slot: Arc>>> = Arc::new(std::sync::Mutex::new(None)); let (dropped, has_dropped) = std::sync::mpsc::channel(); let mut p: JobPool = JobPool::new(1); + let waits = p.stop_waits(); let s = slot.clone(); p.submit( 1, @@ -325,15 +333,14 @@ fn pool_stops_after_a_job_panicked_and_when_dropped_by_its_own_job() { } std::thread::yield_now(); }; - let t0 = std::time::Instant::now(); drop(own); - let _ = dropped.send(t0.elapsed()); + let _ = dropped.send(()); }), ); *slot.lock().unwrap() = Some(p); - let waited = lightcraft_testkit::recv_within(&has_dropped, std::time::Duration::from_secs(30), "the job drops its own pool"); - // waiting for itself would take DROP_WAIT (a running job can't end while it waits) - assert!(waited < crate::pool::DROP_WAIT, "{waited:?}"); + lightcraft_testkit::recv_within(&has_dropped, std::time::Duration::from_secs(30), "the job drops its own pool"); + // the dropping job is the only one running: any wait would be for itself + assert_eq!(waits.load(std::sync::atomic::Ordering::SeqCst), 0, "the job's drop waited for itself"); } #[test] @@ -719,7 +726,7 @@ fn a_slot_runs_one_job_at_a_time() { most.fetch_max(now, Ordering::SeqCst); let _ = started.send(n); if let Some(w) = wait { - let _ = w.recv_timeout(lightcraft_testkit::scaled(std::time::Duration::from_secs(30))); + let _ = w.recv_timeout(stretched(std::time::Duration::from_secs(30))); } running.fetch_sub(1, Ordering::SeqCst); n From 839218e4c093dc22a9c18276b4c58d59b38e2615 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:21:51 -0500 Subject: [PATCH 25/33] test: #3064 gates, regression guards and fixed-pace waits never shrink below their unscaled bounds LIGHTCRAFT_TIME_SCALE accepts factors down to 0.1. That must not turn a bound into one below a fixed time the code or the test needs: the gates the "caller never waits" tests hold (availability, devices, card set-up, the SAM 3 mirror), the regression guards on speed (catalog, raw, album picker: the code's own speed doesn't scale), and the activity tests' waits on the stand-in drive's 100 ms per file and the Auto Import listing every few seconds. These now stretch with the scale but never shrink. Waits on work alone keep the plain scale. Co-Authored-By: Claude Opus 5.5 --- crates/catalog/src/tests.rs | 5 +++-- crates/engine/src/availability.rs | 2 +- crates/engine/src/denoise/gpu_tests.rs | 2 +- crates/engine/src/devices.rs | 2 +- crates/engine/src/tests/segment.rs | 2 +- crates/raw/tests/it/robust.rs | 2 +- crates/ui-egui/src/tests/activity.rs | 12 +++++++++--- crates/ui-egui/src/widgets/album_picker.rs | 6 ++++-- 8 files changed, 21 insertions(+), 12 deletions(-) diff --git a/crates/catalog/src/tests.rs b/crates/catalog/src/tests.rs index f01a8a861ef..7ccdb237105 100644 --- a/crates/catalog/src/tests.rs +++ b/crates/catalog/src/tests.rs @@ -817,8 +817,9 @@ fn chains_of_smart_albums_stay_linear() { let start = std::time::Instant::now(); assert_eq!(c.album_count(AlbumId(26)), 20); // a regression guard, not a speed measurement: linear is microseconds, 2^25 passes take minutes - // (stretched by LIGHTCRAFT_TIME_SCALE for a loaded machine) - assert!(start.elapsed() < lightcraft_testkit::scaled(std::time::Duration::from_secs(2)), "took {:?}", start.elapsed()); + // (stretched by LIGHTCRAFT_TIME_SCALE for a loaded machine, never shrunk below 2 s) + let limit = lightcraft_testkit::scaled(std::time::Duration::from_secs(2)).max(std::time::Duration::from_secs(2)); + assert!(start.elapsed() < limit, "took {:?}", start.elapsed()); } /// A smart album's own album filter (not only its rules) counts in loops: A filtered to "in A" is diff --git a/crates/engine/src/availability.rs b/crates/engine/src/availability.rs index 57a887d78fc..2c26077797e 100644 --- a/crates/engine/src/availability.rs +++ b/crates/engine/src/availability.rs @@ -310,7 +310,7 @@ mod tests { c.fetch_add(1, Ordering::SeqCst); } let (open, cv) = &*g; - let limit = lightcraft_testkit::scaled(Duration::from_secs(5)); + let limit = lightcraft_testkit::scaled(Duration::from_secs(5)).max(Duration::from_secs(5)); drop(cv.wait_timeout_while(held(open), limit, |open| !*open).unwrap_or_else(PoisonError::into_inner)); f.fetch_add(1, Ordering::SeqCst); p == "here" diff --git a/crates/engine/src/denoise/gpu_tests.rs b/crates/engine/src/denoise/gpu_tests.rs index b0d050ad57f..c6dc1375d21 100644 --- a/crates/engine/src/denoise/gpu_tests.rs +++ b/crates/engine/src/denoise/gpu_tests.rs @@ -310,7 +310,7 @@ fn a_set_up_that_takes_too_long_is_given_up_on() { let (g, f) = (gate.clone(), finished.clone()); let slow = move || -> Result<(), String> { let t = web_time::Instant::now(); - while !g.load(Ordering::SeqCst) && t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(5)) { + while !g.load(Ordering::SeqCst) && t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(5)).max(Duration::from_secs(5)) { std::thread::sleep(Duration::from_millis(1)); } f.store(true, Ordering::SeqCst); diff --git a/crates/engine/src/devices.rs b/crates/engine/src/devices.rs index c6561b6bd6a..d42f30e44e0 100644 --- a/crates/engine/src/devices.rs +++ b/crates/engine/src/devices.rs @@ -272,7 +272,7 @@ mod tests { move || { r.fetch_add(1, Ordering::SeqCst); let t = std::time::Instant::now(); - while !g.load(Ordering::SeqCst) && t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(5)) { + while !g.load(Ordering::SeqCst) && t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(5)).max(Duration::from_secs(5)) { std::thread::sleep(Duration::from_millis(1)); } f.fetch_add(1, Ordering::SeqCst); diff --git a/crates/engine/src/tests/segment.rs b/crates/engine/src/tests/segment.rs index c2ea1e63a54..a609ec84313 100644 --- a/crates/engine/src/tests/segment.rs +++ b/crates/engine/src/tests/segment.rs @@ -92,7 +92,7 @@ fn a_failing_download_ends_with_an_error_and_never_blocks() { line.clear(); } if let Some(released) = first.take() { - let _ = released.recv_timeout(lightcraft_testkit::scaled(Duration::from_secs(30))); + let _ = released.recv_timeout(lightcraft_testkit::scaled(Duration::from_secs(30)).max(Duration::from_secs(30))); } let _ = c.write_all(b"HTTP/1.1 404 Not Found\r\nContent-Length: 13\r\nConnection: close\r\n\r\n

404

\n"); } diff --git a/crates/raw/tests/it/robust.rs b/crates/raw/tests/it/robust.rs index 79d3479f271..e84cb2da4f4 100644 --- a/crates/raw/tests/it/robust.rs +++ b/crates/raw/tests/it/robust.rs @@ -113,7 +113,7 @@ fn hostile_dimensions_are_rejected_quickly() { assert!(lightcraft_raw::decode(&bytes).is_err()); // a regression guard, not a speed measurement: rejecting it takes milliseconds, reading the 7 GB // frame it claims would take far longer (stretched by LIGHTCRAFT_TIME_SCALE for a loaded machine) - let limit = lightcraft_testkit::scaled(std::time::Duration::from_secs(5)); + let limit = lightcraft_testkit::scaled(std::time::Duration::from_secs(5)).max(std::time::Duration::from_secs(5)); assert!(t0.elapsed() < limit, "took {:?} (limit {limit:?})", t0.elapsed()); } diff --git a/crates/ui-egui/src/tests/activity.rs b/crates/ui-egui/src/tests/activity.rs index 3b7a6fdc24a..a7da1526e6f 100644 --- a/crates/ui-egui/src/tests/activity.rs +++ b/crates/ui-egui/src/tests/activity.rs @@ -12,7 +12,13 @@ use crate::{LightcraftApp, Services}; /// Generous: merges and renders run on worker threads that a busy machine (a full workspace test run) /// slows a lot. Stretched by `LIGHTCRAFT_TIME_SCALE`. fn t() -> Duration { - lightcraft_testkit::scaled(Duration::from_secs(60)) + stretched(Duration::from_secs(60)) +} + +/// `d` stretched by `LIGHTCRAFT_TIME_SCALE`, never shrunk: the waits here sit on fixed times (the +/// stand-in drive's 100 ms per file, the Auto Import listing every few seconds). +fn stretched(d: Duration) -> Duration { + lightcraft_testkit::scaled(d).max(d) } fn demo() -> Headless { @@ -391,7 +397,7 @@ fn scan_shows_a_row_and_cancel_closes_it() { assert!(tasks[0].cancellable); h.app.session.activity.cancel(tasks[0].id).unwrap(); // like the old Cancel: the scan is dropped at once, without waiting for the file being read - assert!(h.step_until(lightcraft_testkit::scaled(Duration::from_secs(10)), |h| h.app.scan.is_none())); + assert!(h.step_until(stretched(Duration::from_secs(10)), |h| h.app.scan.is_none())); assert!(h.app.session.activity.list().is_empty()); for _ in 0..3 { h.step(); @@ -578,7 +584,7 @@ fn auto_import_listing_has_no_row() { std::fs::create_dir_all(&dir).unwrap(); h.app.session.import_defaults.auto_folder = Some(dir.to_string_lossy().to_string()); // the watched folder is listed every few seconds, quietly - assert!(h.step_until(lightcraft_testkit::scaled(Duration::from_secs(10)), |h| h.app.tasks.is_running("Auto Import"))); + assert!(h.step_until(stretched(Duration::from_secs(10)), |h| h.app.tasks.is_running("Auto Import"))); assert!(h.app.session.activity.list().is_empty(), "{:?}", h.app.session.activity.list()); h.app.session.import_defaults.auto_folder = None; let _ = std::fs::remove_dir_all(dir); diff --git a/crates/ui-egui/src/widgets/album_picker.rs b/crates/ui-egui/src/widgets/album_picker.rs index 02152b7b81e..4844b7c12cf 100644 --- a/crates/ui-egui/src/widgets/album_picker.rs +++ b/crates/ui-egui/src/widgets/album_picker.rs @@ -475,8 +475,10 @@ mod tests { assert_eq!(visible_tree(&v, &open).len(), v.len()); assert_eq!(search(&v, "folder 7 wedding").len(), want); // a regression guard, not a speed measurement: this takes some 20 ms in a debug build; a path - // worked out per row by scanning every album takes seconds (stretched by LIGHTCRAFT_TIME_SCALE) - assert!(start.elapsed() < lightcraft_testkit::scaled(std::time::Duration::from_secs(1)), "took {:?}", start.elapsed()); + // worked out per row by scanning every album takes seconds (stretched by LIGHTCRAFT_TIME_SCALE, + // never shrunk below 1 s) + let limit = lightcraft_testkit::scaled(std::time::Duration::from_secs(1)).max(std::time::Duration::from_secs(1)); + assert!(start.elapsed() < limit, "took {:?}", start.elapsed()); } /// Every word typed is in the album's path (folders included), in any case; names that start From 17056fea165136c623e394dac6491cfd63feb900 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:05:43 -0500 Subject: [PATCH 26/33] test(engine): #3064 the paused-sweep test gives way only once the fourth check has started It waited for three logged checks and then set busy, but three checks done don't mean the fourth has begun: a worker descheduled between checks saw busy first, never started the fourth, and the wait for it timed out on correct code. The probe stand-in now says when it is inside the fourth check, and the test waits for that before giving way and releasing it. The hold inside the probe has a limit. Passed 30 of 30 runs at LIGHTCRAFT_TIME_SCALE 1 and at 0.1; a sweep that never gives way still fails it. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/availability.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/availability.rs b/crates/engine/src/availability.rs index 2c26077797e..0c069af82c1 100644 --- a/crates/engine/src/availability.rs +++ b/crates/engine/src/availability.rs @@ -490,12 +490,18 @@ mod tests { let mut a = Availability::default(); let log = Arc::new(Mutex::new(Vec::::new())); let l = log.clone(); - // the worker is held inside the fourth check until the sweep is told to give way + // the worker is held inside the fourth check (and says it got there) until the sweep is told to + // give way; the hold gives up after a while, so a failed test leaves no worker spinning let hold = Arc::new(std::sync::atomic::AtomicBool::new(true)); - let h = hold.clone(); + let entered = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let (h, e) = (hold.clone(), entered.clone()); a.set_probe(Arc::new(move |p| { - while p == "sweep 3" && h.load(Ordering::SeqCst) { - std::thread::sleep(Duration::from_millis(1)); + if p == "sweep 3" { + e.store(true, Ordering::SeqCst); + let t = std::time::Instant::now(); + while h.load(Ordering::SeqCst) && t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(30)).max(Duration::from_secs(30)) { + std::thread::sleep(Duration::from_millis(1)); + } } held(&l).push(p.to_string()); true @@ -504,7 +510,9 @@ mod tests { let busy = busy_flag(&a); busy.store(false, Ordering::SeqCst); assert!(a.sweep((0..100).map(|i| format!("sweep {i}")).collect())); - wait_until("a few checks", || held(&log).len() >= 3); + // the fourth check has started (so giving way now can't keep it from starting) + wait_until("the worker inside the fourth check", || entered.load(Ordering::SeqCst)); + assert_eq!(held(&log).len(), 3); busy.store(true, Ordering::SeqCst); hold.store(false, Ordering::SeqCst); wait_until("the check in flight", || held(&log).len() >= 4); From 874062fdad39911edd54fd71b0e84a4ebb825da8 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:57:27 -0500 Subject: [PATCH 27/33] test(fetch): #3064 the wait for the resolver to start is as long as the lookup's deadline recv_within scales its limit, so at LIGHTCRAFT_TIME_SCALE=0.1 it gave up after 3 s while the lookup's own deadline stays at least 30 s: a resolver thread delayed on a loaded machine failed a lookup that was still valid. The wait now uses the same stretched (never shrunk) 30 s. Co-Authored-By: Claude Opus 5.5 --- crates/fetch/src/http.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/fetch/src/http.rs b/crates/fetch/src/http.rs index 30d6fde29b0..a6bb8aac1e6 100644 --- a/crates/fetch/src/http.rs +++ b/crates/fetch/src/http.rs @@ -645,7 +645,9 @@ mod tests_connect { let r = std::thread::scope(|scope| { let cancel = &cancel; scope.spawn(move || { - lightcraft_testkit::recv_within(&has_begun, Duration::from_secs(30), "the resolver starts"); + // as long as the lookup's own deadline, which a time scale below 1 doesn't shorten + let started = has_begun.recv_timeout(stretched(Duration::from_secs(30))); + assert!(started.is_ok(), "the resolver didn't start within the lookup's deadline: {started:?}"); std::thread::sleep(Duration::from_millis(100)); cancel.store(true, Ordering::Relaxed); }); From 719598b6e345d6a7dd2fe40e0fcd730e2c77c23c Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:57:27 -0500 Subject: [PATCH 28/33] test(ui-egui): #3064 the sleeping drive wakes when a call's fallback wait runs out Without the watchdog thread, a call that waited out its limit released only itself: the drive stayed asleep and every later call waited the whole limit again. It now wakes the drive and the other waiters, as the watchdog does. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/tests/offline.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/ui-egui/src/tests/offline.rs b/crates/ui-egui/src/tests/offline.rs index baa262b106b..bcd48b75470 100644 --- a/crates/ui-egui/src/tests/offline.rs +++ b/crates/ui-egui/src/tests/offline.rs @@ -74,7 +74,11 @@ impl Nas { let limit = lightcraft_testkit::scaled(WATCHDOG * 2); let (mut state, waited) = self.changed.wait_timeout_while(state, limit, |s| !s.0 && s.1 == 0).unwrap(); if waited.timed_out() { + // wake the drive as the watchdog would have, so the calls after this one don't each + // wait the whole limit again self.forced.store(true, Ordering::SeqCst); + state.0 = true; + self.changed.notify_all(); } else if !state.0 { state.1 -= 1; } From 63247d9d7b674452655855dd94e58f8e80d795cf Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:57:27 -0500 Subject: [PATCH 29/33] test(engine): #3064 the stuck card set-up stays stuck until the test's checks are done The set-up gave up on its own after 5 s, so a test thread paused that long after set_up_card returned saw the set-up finish (or a second one start) and failed though the caller hadn't waited. The set-up now ends only when the gate opens: after the checks, or on drop when the test fails. The deadline moved to the caller's answer, which a helper thread sends and the test receives with recv_within. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/denoise/gpu_tests.rs | 33 +++++++++++++++++--------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/denoise/gpu_tests.rs b/crates/engine/src/denoise/gpu_tests.rs index c6dc1375d21..7c1b321c2db 100644 --- a/crates/engine/src/denoise/gpu_tests.rs +++ b/crates/engine/src/denoise/gpu_tests.rs @@ -279,6 +279,15 @@ fn a_set_up_runs_beside_the_cpu_and_removes_its_marker() { let _ = std::fs::remove_dir_all(&dir); } +/// Opens its gate when dropped, however the test ends. +struct OpenOnDrop(Arc); + +impl Drop for OpenOnDrop { + fn drop(&mut self) { + self.0.store(true, Ordering::SeqCst); + } +} + #[test] fn a_set_up_makes_the_folder_of_its_marker() { let dir = folder("setup-folder").join("not").join("yet"); @@ -303,32 +312,34 @@ fn a_set_up_makes_the_folder_of_its_marker() { fn a_set_up_that_takes_too_long_is_given_up_on() { let dir = folder("slowsetup"); let marker = dir.join(GPU_SETUP_MARKER); - // a card whose set-up is stuck until the test lets it go (it gives up after a while, so a caller - // that waits for it fails the test instead of hanging it) + // a card whose set-up is stuck until the test opens the gate, which it does only after its checks + // (or when it fails: `release` opens it on drop, so the set-up thread doesn't spin on) let gate = Arc::new(AtomicBool::new(false)); + let release = OpenOnDrop(gate.clone()); let finished = Arc::new(AtomicBool::new(false)); let (g, f) = (gate.clone(), finished.clone()); let slow = move || -> Result<(), String> { - let t = web_time::Instant::now(); - while !g.load(Ordering::SeqCst) && t.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(5)).max(Duration::from_secs(5)) { + while !g.load(Ordering::SeqCst) { std::thread::sleep(Duration::from_millis(1)); } f.store(true, Ordering::SeqCst); Ok(()) }; - let r = set_up_card(&marker, Duration::from_millis(50), slow, || ()); + // the deadline is on the caller's answer: a set_up_card that waited for the card fails here + let (tx, rx) = std::sync::mpsc::channel(); + let m = marker.clone(); + std::thread::spawn(move || { + let _ = tx.send(set_up_card(&m, Duration::from_millis(50), slow, || ())); + }); + let r = lightcraft_testkit::recv_within(&rx, Duration::from_secs(30), "set_up_card's answer while the card is stuck"); assert!(r.as_ref().is_err_and(|e| e.contains("took longer") && worth_retrying(e)), "{r:?}"); assert!(!finished.load(Ordering::SeqCst), "the caller does not wait for the card"); // while the first is still stuck, another set-up of the same model does not start a second one let again = set_up_card(&marker, Duration::from_secs(5), || Ok(()), || ()); assert!(again.is_err_and(|e| e.contains("still setting up") && worth_retrying(&e))); // once it ends its marker goes, and the card can be set up again - gate.store(true, Ordering::SeqCst); - let started = web_time::Instant::now(); - while marker.exists() { - assert!(started.elapsed() < lightcraft_testkit::scaled(Duration::from_secs(10)), "the stuck set-up ended"); - std::thread::sleep(Duration::from_millis(20)); - } + drop(release); + lightcraft_testkit::wait_until(Duration::from_secs(10), "the stuck set-up ended and removed its marker", || !marker.exists()); while SETTING_UP.lock().unwrap().iter().any(|m| m == &marker) { std::thread::sleep(Duration::from_millis(5)); } From dd49e63e6f553a5753b294c069b126c12e37ccc0 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Fri, 30 Oct 2026 03:51:53 -0500 Subject: [PATCH 30/33] test(engine): #3064 the second card set-up's answer is bounded too The second set_up_card ran on the test thread: had it waited for the stuck first set-up, the test would hang, since the gate opens only after it returns. Both calls now run on a helper thread with recv_within on their answer. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/denoise/gpu_tests.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/denoise/gpu_tests.rs b/crates/engine/src/denoise/gpu_tests.rs index 7c1b321c2db..2f732d042a4 100644 --- a/crates/engine/src/denoise/gpu_tests.rs +++ b/crates/engine/src/denoise/gpu_tests.rs @@ -325,18 +325,22 @@ fn a_set_up_that_takes_too_long_is_given_up_on() { f.store(true, Ordering::SeqCst); Ok(()) }; - // the deadline is on the caller's answer: a set_up_card that waited for the card fails here - let (tx, rx) = std::sync::mpsc::channel(); - let m = marker.clone(); - std::thread::spawn(move || { - let _ = tx.send(set_up_card(&m, Duration::from_millis(50), slow, || ())); - }); - let r = lightcraft_testkit::recv_within(&rx, Duration::from_secs(30), "set_up_card's answer while the card is stuck"); + // each call runs on a helper thread with the deadline on its answer: a set_up_card that waited + // for the stuck card fails the test there instead of hanging it + let answer = |limit: Duration, card: Box Result<(), String> + Send>, what: &str| { + let (tx, rx) = std::sync::mpsc::channel(); + let m = marker.clone(); + std::thread::spawn(move || { + let _ = tx.send(set_up_card(&m, limit, card, || ())); + }); + lightcraft_testkit::recv_within(&rx, Duration::from_secs(30), what) + }; + let r = answer(Duration::from_millis(50), Box::new(slow), "set_up_card's answer while the card is stuck"); assert!(r.as_ref().is_err_and(|e| e.contains("took longer") && worth_retrying(e)), "{r:?}"); assert!(!finished.load(Ordering::SeqCst), "the caller does not wait for the card"); // while the first is still stuck, another set-up of the same model does not start a second one - let again = set_up_card(&marker, Duration::from_secs(5), || Ok(()), || ()); - assert!(again.is_err_and(|e| e.contains("still setting up") && worth_retrying(&e))); + let again = answer(Duration::from_secs(5), Box::new(|| Ok(())), "a second set-up's answer while the first is stuck"); + assert!(again.as_ref().is_err_and(|e| e.contains("still setting up") && worth_retrying(e)), "{again:?}"); // once it ends its marker goes, and the card can be set up again drop(release); lightcraft_testkit::wait_until(Duration::from_secs(10), "the stuck set-up ended and removed its marker", || !marker.exists()); From ba76a4497b6ec5199d9ed4fbdeb7a14ac010d276 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:14:31 -0600 Subject: [PATCH 31/33] test(engine): #3064 the 50 cold device get() calls are bounded again The held-scan gate catches a get() that waits for the scan, but not a fixed delay in each call, and devices() runs every frame in menus. The loop is again held to scaled(1 s), never less than 1 s; a 50 ms sleep in get() fails it. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/devices.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/engine/src/devices.rs b/crates/engine/src/devices.rs index d42f30e44e0..a0bbb732dba 100644 --- a/crates/engine/src/devices.rs +++ b/crates/engine/src/devices.rs @@ -286,11 +286,15 @@ mod tests { c.fetch_add(1, Ordering::SeqCst); }); // cold: served at once (empty) while one scan runs in the background + let t = std::time::Instant::now(); for _ in 0..50 { assert!(watch.get().is_empty()); } // the scan is still held: a get() that waited for it could not have returned yet assert_eq!(finished.load(Ordering::SeqCst), 0, "get() waited for the scan"); + // nor may each call cost a fixed delay of its own: devices() runs every frame in menus + let bound = lightcraft_testkit::scaled(Duration::from_secs(1)).max(Duration::from_secs(1)); + assert!(t.elapsed() < bound, "50 cold get() calls took {:?} (limit {bound:?})", t.elapsed()); assert!(watch.refreshing()); gate.store(true, Ordering::SeqCst); assert!(until(|| !watch.refreshing())); From 23bb5bc2018b51171ba6255475f2f42b261822b7 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:26:32 -0600 Subject: [PATCH 32/33] test(fetch): #3064 the cancel test reports a part that never got bytes The canceller waits for the part's first bytes on a thread of its own. If they never came, its wait_until panicked there, the flag stayed unset, and the test failed later at the Cancelled check without the wait's message. The test now joins the canceller once the run returns and resumes its panic. Co-Authored-By: Claude Opus 5.5 --- crates/fetch/src/tests.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/fetch/src/tests.rs b/crates/fetch/src/tests.rs index 187b185ae08..1ec965ad644 100644 --- a/crates/fetch/src/tests.rs +++ b/crates/fetch/src/tests.rs @@ -319,7 +319,7 @@ fn cancel_stops_quickly_and_keeps_the_part_for_later() { let c = cancel.clone(); let part = part_path(&dir, "model.bin"); // cancel once part of the file is written (the slow server takes 6 s for the whole of it) - std::thread::spawn(move || { + let canceller = std::thread::spawn(move || { lightcraft_testkit::wait_until(Duration::from_secs(30), "the first bytes of the part", || { std::fs::metadata(&part).is_ok_and(|m| m.len() > 0) }); @@ -327,6 +327,10 @@ fn cancel_stops_quickly_and_keeps_the_part_for_later() { }); let t = Instant::now(); let (r, _) = run(&[pinned()], &[format!("{}/slow", srv.base)], &dir, &cancel); + // a part that never got bytes fails the test with the wait's message, not the result's + if let Err(panic) = canceller.join() { + std::panic::resume_unwind(panic); + } assert_eq!(r, Err(DownloadError::Cancelled)); assert!(t.elapsed() < stretched(Duration::from_secs(3)), "{:?}", t.elapsed()); assert!(!dir.join("model.bin").exists()); From 70eb6c54634a6e52925ad3dfef0ec7094ea088e9 Mon Sep 17 00:00:00 2001 From: Chad Elliott <880190+THEtheChad@users.noreply.github.com> Date: Tue, 3 Nov 2026 03:01:23 -0600 Subject: [PATCH 33/33] test(engine): #3064 the warm device get() is bounded like the cold ones Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/devices.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/engine/src/devices.rs b/crates/engine/src/devices.rs index a0bbb732dba..bbd2226f413 100644 --- a/crates/engine/src/devices.rs +++ b/crates/engine/src/devices.rs @@ -304,7 +304,9 @@ mod tests { gate.store(false, Ordering::SeqCst); mounted.lock().unwrap().push(card("EOS_DIGITAL")); std::thread::sleep(Duration::from_millis(40)); + let t = std::time::Instant::now(); assert!(watch.get().is_empty(), "the old list is served while rescanning"); + assert!(t.elapsed() < bound, "the warm get() took {:?} (limit {bound:?})", t.elapsed()); assert_eq!(finished.load(Ordering::SeqCst), 1, "warm get() waited for the rescan"); gate.store(true, Ordering::SeqCst); assert!(until(|| changes.load(Ordering::SeqCst) == 1), "a changed scan notifies");