From df2aacd6dde4eab69ca6d391a6d93f9cb83922d8 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Sat, 14 Mar 2026 23:24:12 +0000 Subject: [PATCH] Add test helper binary and subprocess handling tests --- crates/bough-core/Cargo.toml | 4 + .../bough-core/src/bin/bough-test-helper.rs | 44 ++++++++++ crates/bough-core/src/phase.rs | 83 +++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 crates/bough-core/src/bin/bough-test-helper.rs diff --git a/crates/bough-core/Cargo.toml b/crates/bough-core/Cargo.toml index a7a8c306..a6f63dce 100644 --- a/crates/bough-core/Cargo.toml +++ b/crates/bough-core/Cargo.toml @@ -26,3 +26,7 @@ tracing = "0.1" [dev-dependencies] tempfile = "3.26" + +[[bin]] +name = "bough-test-helper" +path = "src/bin/bough-test-helper.rs" diff --git a/crates/bough-core/src/bin/bough-test-helper.rs b/crates/bough-core/src/bin/bough-test-helper.rs new file mode 100644 index 00000000..05bc2970 --- /dev/null +++ b/crates/bough-core/src/bin/bough-test-helper.rs @@ -0,0 +1,44 @@ +use std::io::Write; + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + match args.first().map(|s| s.as_str()) { + Some("flood-stdout") => flood(std::io::stdout(), &args[1..]), + Some("flood-stderr") => flood(std::io::stderr(), &args[1..]), + Some("spawn-and-wait") => spawn_and_wait(&args[1..]), + Some("sleep") => std::thread::sleep(std::time::Duration::from_secs(1000)), + _ => { + eprintln!( + "usage: bough-test-helper [args...]" + ); + std::process::exit(1); + } + } +} + +fn flood(mut w: impl Write, args: &[String]) { + let bytes: usize = args + .first() + .and_then(|s| s.parse().ok()) + .unwrap_or(256 * 1024); + let chunk = vec![b'x'; 1024]; + let mut written = 0; + while written < bytes { + let n = std::cmp::min(chunk.len(), bytes - written); + w.write_all(&chunk[..n]).unwrap(); + written += n; + } + w.flush().unwrap(); +} + +fn spawn_and_wait(args: &[String]) { + let pid_file = args + .first() + .expect("spawn-and-wait requires a pid-file path"); + let child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["sleep"]) + .spawn() + .expect("spawn child"); + std::fs::write(pid_file, child.id().to_string()).unwrap(); + std::thread::sleep(std::time::Duration::from_secs(1000)); +} diff --git a/crates/bough-core/src/phase.rs b/crates/bough-core/src/phase.rs index 7ef80c3f..e88a546d 100644 --- a/crates/bough-core/src/phase.rs +++ b/crates/bough-core/src/phase.rs @@ -569,4 +569,87 @@ mod tests { assert!(!outcome.timed_out()); assert_eq!(outcome.exit_code(), 0); } + + fn helper_bin() -> PathBuf { + let test_exe = std::env::current_exe().expect("current_exe"); + let dir = test_exe.parent().unwrap().parent().unwrap(); + dir.join("bough-test-helper") + } + + #[test] + fn phase_run_does_not_deadlock_on_large_stdout() { + let dir = tempfile::tempdir().unwrap(); + let root = TestRoot(dir.path().to_path_buf()); + let phase = Phase { + cmd: vec![ + helper_bin().to_str().unwrap().into(), + "flood-stdout".into(), + "262144".into(), + ], + pwd: crate::file::Twig::new(PathBuf::from(".")).unwrap(), + timeout_absolute: Some(Duration::from_secs(10)), + ..make_phase(&root) + }; + let outcome = phase.run(None).unwrap(); + assert!(!outcome.timed_out()); + assert_eq!(outcome.exit_code(), 0); + assert!( + outcome.stdout().len() > 64 * 1024, + "expected >64KB stdout, got {}", + outcome.stdout().len() + ); + } + + #[test] + fn phase_run_does_not_deadlock_on_large_stderr() { + let dir = tempfile::tempdir().unwrap(); + let root = TestRoot(dir.path().to_path_buf()); + let phase = Phase { + cmd: vec![ + helper_bin().to_str().unwrap().into(), + "flood-stderr".into(), + "262144".into(), + ], + pwd: crate::file::Twig::new(PathBuf::from(".")).unwrap(), + timeout_absolute: Some(Duration::from_secs(10)), + ..make_phase(&root) + }; + let outcome = phase.run(None).unwrap(); + assert!(!outcome.timed_out()); + assert!( + outcome.stderr().len() > 64 * 1024, + "expected >64KB stderr, got {}", + outcome.stderr().len() + ); + } + + #[cfg(unix)] + #[test] + fn phase_run_timeout_kills_grandchild_processes() { + let dir = tempfile::tempdir().unwrap(); + let pid_file = dir.path().join("grandchild.pid"); + let root = TestRoot(dir.path().to_path_buf()); + let phase = Phase { + cmd: vec![ + helper_bin().to_str().unwrap().into(), + "spawn-and-wait".into(), + pid_file.to_str().unwrap().into(), + ], + pwd: crate::file::Twig::new(PathBuf::from(".")).unwrap(), + timeout_absolute: Some(Duration::from_millis(500)), + ..make_phase(&root) + }; + let outcome = phase.run(None).unwrap(); + assert!(outcome.timed_out()); + + std::thread::sleep(Duration::from_millis(100)); + + let pid_str = std::fs::read_to_string(&pid_file).expect("grandchild pid file"); + let pid: i32 = pid_str.trim().parse().expect("parse pid"); + let alive = unsafe { libc::kill(pid, 0) } == 0; + assert!( + !alive, + "grandchild process {pid} should have been killed but is still running" + ); + } } -- knotmirror 0.0.1