//! Process trees started through `lutra_proc` die with their owner. //! //! Each test starts `lutra-proc-helper` in a tree role: the helper starts a //! sleeping grandchild that holds its standard output, prints `ready` //! and sleeps. Reading that output to end of file proves that every //! process holding it, the grandchild included, has exited. Without the //! groups the grandchild sleeps for two minutes and the read times out. #![allow(clippy::unwrap_used)] use std::io::{BufRead, BufReader, Read, Write}; use std::process::{ChildStdout, Command, Stdio}; use std::sync::mpsc; use std::thread; use std::time::Duration; /// How long a test waits for a tree to go away. const GONE_WITHIN: Duration = Duration::from_secs(15); /// The helper in `role`, with its output piped. fn helper(role: &str) -> Command { let mut command = Command::new(env!("CARGO_BIN_EXE_lutra-proc-helper")); command .arg(role) .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::null()); command } /// Waits for the helper's `ready` line and returns the rest of its output. fn ready(stdout: ChildStdout) -> BufReader { let mut reader = BufReader::new(stdout); let mut line = String::new(); reader.read_line(&mut line).unwrap(); assert_eq!(line.trim(), "ready"); reader } /// Asserts that the output reaches end of file within [`GONE_WITHIN`], /// i.e. that no process holding it is left. fn assert_gone(mut reader: BufReader) { let (tx, rx) = mpsc::channel(); thread::spawn(move || { let mut rest = Vec::new(); let _ = reader.read_to_end(&mut rest); let _ = tx.send(()); }); assert!( rx.recv_timeout(GONE_WITHIN).is_ok(), "a grandchild still holds the pipe after {GONE_WITHIN:?}" ); } #[test] fn hard_kill_of_the_parent_takes_its_guarded_child() { let mut parent = helper("tree-proc").spawn().unwrap(); let reader = ready(parent.stdout.take().unwrap()); parent.kill().unwrap(); parent.wait().unwrap(); assert_gone(reader); } #[test] fn hard_kill_works_when_the_parent_is_in_a_group_itself() { let mut parent = lutra_proc::spawn(helper("tree-proc")).unwrap(); let reader = ready(parent.stdout.take().unwrap()); std::process::Child::kill(&mut parent).unwrap(); parent.wait().unwrap(); assert_gone(reader); drop(parent); } #[test] fn kill_takes_the_whole_tree() { let mut parent = lutra_proc::spawn(helper("tree-plain")).unwrap(); let reader = ready(parent.stdout.take().unwrap()); parent.kill().unwrap(); parent.wait().unwrap(); assert_gone(reader); } #[test] fn drop_takes_the_whole_tree() { let mut parent = lutra_proc::spawn(helper("tree-plain")).unwrap(); let reader = ready(parent.stdout.take().unwrap()); drop(parent); assert_gone(reader); } #[test] fn a_child_that_exits_reports_its_status() { let mut child = lutra_proc::spawn(helper("unknown-role")).unwrap(); let status = child.wait().unwrap(); assert_eq!(status.code(), Some(2)); child.kill().unwrap(); } #[test] fn a_missing_program_is_an_error() { let error = lutra_proc::spawn(Command::new("lutra-proc-no-such-program")).unwrap_err(); assert_eq!(error.kind(), std::io::ErrorKind::NotFound); } #[test] fn a_suspended_start_runs_and_takes_the_whole_tree() { let mut child = lutra_proc::spawn_suspended(helper("unknown-role")).unwrap(); assert_eq!(child.wait().unwrap().code(), Some(2)); let mut parent = lutra_proc::spawn_suspended(helper("tree-plain")).unwrap(); let reader = ready(parent.stdout.take().unwrap()); drop(parent); assert_gone(reader); } #[test] fn hard_kill_works_when_a_suspended_start_parent_guards_its_child() { let mut parent = lutra_proc::spawn_suspended(helper("tree-proc")).unwrap(); let reader = ready(parent.stdout.take().unwrap()); std::process::Child::kill(&mut parent).unwrap(); parent.wait().unwrap(); assert_gone(reader); } #[cfg(windows)] #[test] fn dropping_an_adopt_guard_takes_the_tree_started_after_it() { use std::os::windows::io::AsRawHandle; let mut command = helper("tree-on-input"); command.stdin(Stdio::piped()); let mut parent = command.spawn().unwrap(); let guard = lutra_proc::adopt(parent.as_raw_handle()).unwrap(); parent.stdin.take().unwrap().write_all(b"go\n").unwrap(); let reader = ready(parent.stdout.take().unwrap()); drop(guard); assert_gone(reader); parent.wait().unwrap(); } /// Starts the helper in a `detached-*` role, reads the grandchild's id and /// returns it with whether the output then reached end of file within /// `within`. Kills the grandchild before returning. fn detached_output_closes(role: &str, within: Duration) -> bool { let mut parent = helper(role).spawn().unwrap(); let mut reader = BufReader::new(parent.stdout.take().unwrap()); let mut line = String::new(); reader.read_line(&mut line).unwrap(); let id: u32 = line.trim().parse().unwrap(); assert!(parent.wait().unwrap().success()); let (tx, rx) = mpsc::channel(); thread::spawn(move || { let mut rest = Vec::new(); let _ = reader.read_to_end(&mut rest); let _ = tx.send(()); }); let closed = rx.recv_timeout(within).is_ok(); let kill = if cfg!(windows) { Command::new("taskkill") .args(["/F", "/PID", &id.to_string()]) .output() } else { Command::new("kill").args(["-9", &id.to_string()]).output() }; kill.unwrap(); closed } #[test] fn a_detached_start_holds_none_of_the_callers_handles() { assert!( detached_output_closes("detached-proc", GONE_WITHIN), "the detached grandchild holds its parent's output pipe" ); } #[cfg(windows)] #[test] fn a_plain_detached_start_inherits_the_callers_pipe_on_windows() { assert!( !detached_output_closes("detached-plain", Duration::from_secs(3)), "the standard library no longer passes inheritable handles on; spawn_detached may be simpler now" ); }