#![cfg(unix)]
use std::io::Read;
use std::os::unix::process::ExitStatusExt;
use std::time::Duration;
use crate::graceful_hooks::{at, release_at, HookPoint};
use crate::send_log::Capture;
use crate::test_child::foreign;
use crate::wait::fault;
fn spawn_term_ignoring_blocker() -> crate::test_child::StdChild {
let mut child = crate::test_child::StdChild::spawn(
std::process::Command::new("sh")
.args(["-c", "trap '' TERM; echo r; exec cat"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped()),
);
let mut buf = [0u8; 1];
child
.stdout
.take()
.expect("piped stdout")
.read_exact(&mut buf)
.expect("readiness byte");
child
}
#[skuld::test]
async fn async_foreign_graceful_watch_error_still_escalates() {
let mut child = spawn_term_ignoring_blocker();
let p = crate::tokio::Process::open_pid(child.id())
.expect("open")
.expect("running");
fault::set_force_watch_error(true);
let err = p
.graceful_shutdown(Duration::from_secs(30))
.await
.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:?}"
);
}
#[skuld::test]
async fn async_foreign_graceful_tree_watch_error_still_sweeps() {
let mut child = spawn_term_ignoring_blocker();
let p = crate::tokio::Process::open_pid(child.id())
.expect("open")
.expect("running");
fault::set_force_watch_error(true);
let err = p
.graceful_shutdown_tree(Duration::from_secs(30))
.await
.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:?}"
);
}
#[skuld::test]
async fn async_foreign_terminate_tree_sigterms_the_root_and_its_descendant() {
crate::tokio::test_runtime::assert_current_thread();
let tree = foreign::blocked_tree();
let p = crate::tokio::Process::open_pid(tree.root.id())
.expect("open")
.expect("running");
let sends = Capture::start();
p.terminate_tree().expect("terminate_tree");
foreign::assert_tree_sigtermed(&sends, tree.root.id(), tree.grandchild);
tree.assert_died_of_sigterm();
}
#[skuld::test]
async fn async_foreign_graceful_shutdown_tree_graceful_path() {
crate::tokio::test_runtime::assert_current_thread();
let tree = foreign::blocked_tree();
let (root, grandchild) = (tree.root.id(), tree.grandchild);
let p = crate::tokio::Process::open_pid(root).expect("open").expect("running");
let sends = Capture::start();
let _armed = at(HookPoint::AfterTerminate, move || {
foreign::assert_tree_sigtermed(&sends, root, grandchild)
});
p.graceful_shutdown_tree(Duration::from_secs(30))
.await
.expect("graceful_shutdown_tree");
tree.assert_died_of_sigterm();
}
#[skuld::test]
async fn async_foreign_graceful_shutdown_tree_kills_a_member_that_ignores_the_sigterm_after_the_root_exits() {
crate::tokio::test_runtime::assert_current_thread();
let mut tree = foreign::survivor_tree();
let p = crate::tokio::Process::open_pid(tree.child.id())
.expect("open")
.expect("running");
p.graceful_shutdown_tree(Duration::from_secs(30))
.await
.expect("every member the walk found is killed");
for member in &tree.members {
assert_eq!(
member.is_alive(),
crate::identity::Liveness::Dead,
"{member:?} ignored the SIGTERM and is killed by identity"
);
}
let status = tree.child.wait().expect("reap the root");
assert_eq!(status.signal(), Some(libc::SIGTERM), "got {status:?}");
}
#[skuld::test]
async fn async_foreign_graceful_shutdown_tree_kills_a_member_reparented_while_the_root_outlives_the_grace() {
crate::tokio::test_runtime::assert_current_thread();
let mut tree = foreign::tree_of(crate::test_child::ROOT_OUTLIVING_THE_GRACE_ARGV);
let p = crate::tokio::Process::open_pid(tree.child.id())
.expect("open")
.expect("running");
let reparented = crate::test_child::wait_for_the_middle_to_exit_after_the_term(tree.process.id());
p.graceful_shutdown_tree(Duration::ZERO)
.await
.expect("the member its walk reached is killed by identity");
reparented.assert_fired();
for member in &tree.members {
assert_eq!(
member.is_alive(),
crate::identity::Liveness::Dead,
"{member:?} is killed or ended by the SIGTERM"
);
}
let status = tree.child.wait().expect("reap the root");
assert_eq!(
status.signal(),
Some(libc::SIGKILL),
"the sweep killed the live root: {status:?}"
);
}
#[skuld::test]
async fn async_foreign_graceful_shutdown_graceful_path() {
crate::tokio::test_runtime::assert_current_thread();
let mut child = crate::test_child::blocker();
let p = crate::tokio::Process::open_pid(child.id())
.expect("open")
.expect("running");
let stdin = child.stdin.take().expect("piped stdin");
let _release = release_at(HookPoint::AfterTerminate, stdin);
p.graceful_shutdown(Duration::from_secs(30))
.await
.expect("graceful_shutdown");
let status = child.wait().expect("reap");
assert_eq!(status.signal(), Some(libc::SIGTERM), "got {status:?}");
}