diff --git a/crates/cli/src/watch/tests.rs b/crates/cli/src/watch/tests.rs index 5e6218c3d..efa81b564 100644 --- a/crates/cli/src/watch/tests.rs +++ b/crates/cli/src/watch/tests.rs @@ -23,6 +23,12 @@ const RULE: &str = r#"{"on":"issue.opened","do":"label","with":{"labels":["triag const ISSUE: &str = r#"[{"number":7,"created_at":"2026-11-05T10:00:00Z","updated_at":"2026-11-05T10:00:00Z","user":{"login":"stranger"},"author_association":"NONE","labels":[],"body":"hello"}]"#; const NOW: &str = "2026-11-05T12:00:00Z"; const EARLIER: &str = "2026-11-05T09:00:00Z"; +const BUSY: &str = "watch already running\n"; +const LOCK_POLL: std::time::Duration = std::time::Duration::from_millis(10); +/// Polls a test makes for a lock that another test's child still shares, which +/// is 30 seconds in all. Counted rather than timed: no watch source but the +/// clock reads the wall clock. +const LOCK_POLLS: u32 = 3000; #[derive(Default)] struct Spy { @@ -138,10 +144,25 @@ impl Env { } } + /// One `watch once`, after which the lock is free again. fn once(&self, since: Option<&str>) -> Result { + let out = self.once_raw(since); + self.settle(); + out + } + + fn once_raw(&self, since: Option<&str>) -> Result { once(&self.ctx(), since, || Canned(self.requests.clone())) } + /// Waits until the lock is free. A lock file stays locked for as long as + /// any process shares its open handle, and another test that forks while + /// this one holds the lock gives its child a copy of that handle until the + /// child replaces itself with its program. + fn settle(&self) { + drop(hold(&self.data)); + } + fn rows(&self, state: &str) -> Vec { Store::open(&self.data.join("watch.db")) .unwrap() @@ -152,10 +173,13 @@ impl Env { fn hold(data: &Path) -> lock::InstanceLock { fs::create_dir_all(data).unwrap(); - match acquire(&data.join("watch.lock")).unwrap() { - Acquire::Held(lock) => lock, - Acquire::Busy => panic!("the lock was taken"), + for _ in 0..LOCK_POLLS { + if let Acquire::Held(lock) = acquire(&data.join("watch.lock")).unwrap() { + return lock; + } + std::thread::sleep(LOCK_POLL); } + panic!("the lock was taken"); } #[test] @@ -229,10 +253,10 @@ fn a_second_watch_process_prints_already_running_and_polls_nothing() { let _held = hold(&e.data); // Act - let out = e.once(Some(EARLIER)).unwrap(); + let out = e.once_raw(Some(EARLIER)).unwrap(); // Assert - assert_eq!(out, "watch already running\n"); + assert_eq!(out, BUSY); assert_eq!(e.requests.get(), 0); } @@ -278,6 +302,7 @@ fn the_running_watch_carries_out_an_approved_item_on_its_next_cycle() { let held = hold(&e.data); approve(&e.ctx(), 1).unwrap(); drop(held); + e.settle(); // Act let out = e.once(None).unwrap(); diff --git a/crates/cli/tests/doc_read.rs b/crates/cli/tests/doc_read.rs index 425b3fcbf..abeb235df 100644 --- a/crates/cli/tests/doc_read.rs +++ b/crates/cli/tests/doc_read.rs @@ -25,6 +25,48 @@ static COUNTER: AtomicU64 = AtomicU64::new(0); /// Names the debug build reads from `LIMITS_VAR` to lower a limit. const LIMITS_VAR: &str = "PLAYBOOK_TEST_DOC_LIMITS"; +const SCRIPT_MODE: u32 = 0o755; +/// Pause between tries to start a script that is still open for writing. +const BUSY_POLL: Duration = Duration::from_millis(10); +/// The longest wait for such a script, and for its first run, which the system +/// scans before it lets it start. +const FIRST_RUN_DEADLINE: Duration = Duration::from_secs(180); +/// How long the converters that wait to be stopped would run if left alone. +const IDLE_CONVERTER_SECONDS: u64 = 30; + +/// Writes `script` to `path` and returns only once it can be started. +/// +/// Another test's `fork` can copy this file's write handle, and a file open +/// for writing cannot be executed until that copy goes away at the child's +/// `exec`. Under load that takes long enough for the converter under test to +/// fail to start, so this runs the script with `--version`, which every fake +/// answers before doing anything else, and retries while it is busy. +fn install_runnable(path: &Path, script: &str) { + use std::io::Write; + use std::os::unix::fs::OpenOptionsExt; + let mut file = fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .mode(SCRIPT_MODE) + .open(path) + .unwrap(); + file.write_all(script.as_bytes()).unwrap(); + file.sync_all().unwrap(); + drop(file); + let deadline = Instant::now() + FIRST_RUN_DEADLINE; + loop { + match Command::new(path).arg("--version").output() { + Ok(_) => return, + Err(e) if e.kind() == std::io::ErrorKind::ExecutableFileBusy => { + assert!(Instant::now() < deadline, "{} stayed busy", path.display()); + std::thread::sleep(BUSY_POLL); + } + Err(e) => panic!("cannot start {}: {e}", path.display()), + } + } +} + struct Env { root: PathBuf, home: PathBuf, @@ -86,8 +128,7 @@ impl Env { env = self.env_log().display(), started = self.started().display(), ); - fs::write(path, script).unwrap(); - fs::set_permissions(path, fs::Permissions::from_mode(0o755)).unwrap(); + install_runnable(path, &script); } /// The directory holding a copy of the playbook binary, so a `pbdoc` @@ -385,7 +426,7 @@ fn a_scanned_pdf_says_it_needs_ocr_and_never_retries() { fn a_converter_that_runs_too_long_is_stopped_and_cleaned_up() { // Arrange let env = Env::new("timeout"); - env.fake("sleep 30"); + env.fake(&format!("sleep {IDLE_CONVERTER_SECONDS}")); env.limit("timeout_seconds", 1); let doc = env.doc("slow.pdf", b"%PDF-1.4"); let start = Instant::now(); @@ -396,7 +437,8 @@ fn a_converter_that_runs_too_long_is_stopped_and_cleaned_up() { // Assert assert_eq!(out.status.code(), Some(1)); assert!(text(&out).contains("stopped after 1 seconds")); - assert!(start.elapsed() < Duration::from_secs(15)); + // Stopped by the limit, not by the converter finishing on its own. + assert!(start.elapsed() < Duration::from_secs(IDLE_CONVERTER_SECONDS)); assert!(env.leftovers().is_empty()); } @@ -425,7 +467,7 @@ fn a_converter_that_uses_too_much_memory_is_stopped() { fn sigterm_stops_the_converter_and_removes_the_temp_dir() { // Arrange let env = Env::new("signal"); - env.fake("sleep 30"); + env.fake(&format!("sleep {IDLE_CONVERTER_SECONDS}")); let doc = env.doc("slow.pdf", b"%PDF-1.4"); let mut child = env .command(&["doc", "read", doc.to_str().unwrap()]) @@ -433,7 +475,7 @@ fn sigterm_stops_the_converter_and_removes_the_temp_dir() { .stderr(Stdio::null()) .spawn() .unwrap(); - let deadline = Instant::now() + Duration::from_secs(10); + let deadline = Instant::now() + FIRST_RUN_DEADLINE; while !env.started().exists() && Instant::now() < deadline { std::thread::sleep(Duration::from_millis(20)); }