#![cfg(unix)]
use std::io::Read;
use std::os::unix::process::ExitStatusExt;
use std::time::Duration;
use crate::wait::fault;
fn spawn_term_ignoring_sleeper() -> std::process::Child {
let _guard = crate::child::spawn::spawn_lock();
let mut child = std::process::Command::new("sh")
.args(["-c", "trap '' TERM; echo r; exec sleep 30"])
.stdout(std::process::Stdio::piped())
.spawn()
.expect("spawn");
let mut buf = [0u8; 1];
child
.stdout
.take()
.expect("piped stdout")
.read_exact(&mut buf)
.expect("readiness byte");
child
}
#[test]
fn foreign_graceful_lone_watch_error_still_escalates() {
let mut child = spawn_term_ignoring_sleeper();
let p = crate::Process::from_pid(child.id()).found().expect("resolves");
fault::set_force_watch_error(true);
let err = p
.graceful_shutdown(Duration::from_secs(30))
.expect_err("the watch error must surface");
assert!(
!fault::armed(),
"seam not consumed — the watch did not run on this thread"
);
assert!(matches!(err, crate::error::Error::Io(_)), "got {err:?}");
let status = child.wait().expect("reap");
assert_eq!(
status.signal(),
Some(libc::SIGKILL),
"child must be force-killed despite the watch error, got {status:?}"
);
}
#[test]
fn foreign_graceful_tree_watch_error_still_sweeps() {
let mut child = spawn_term_ignoring_sleeper();
let p = crate::Process::from_pid(child.id()).found().expect("resolves");
fault::set_force_watch_error(true);
let err = p
.graceful_shutdown_tree(Duration::from_secs(30))
.expect_err("the watch error must surface");
assert!(
!fault::armed(),
"seam not consumed — the watch did not run on this thread"
);
assert!(matches!(err, crate::error::Error::Io(_)), "got {err:?}");
let status = child.wait().expect("reap");
assert_eq!(
status.signal(),
Some(libc::SIGKILL),
"root must be swept despite the watch error, got {status:?}"
);
}