#[cfg(unix)]
use super::ProcHandle;
use super::{std_teardown_action, StdTeardown};
#[skuld::test]
fn kill_success_reaps_with_a_blocking_wait() {
assert_eq!(std_teardown_action(&Ok(())), StdTeardown::ReapBlocking);
}
#[skuld::test]
fn eperm_never_blocks_even_without_an_elevated_flag() {
let eperm = std::io::Error::from(std::io::ErrorKind::PermissionDenied);
assert_eq!(std_teardown_action(&Err(eperm)), StdTeardown::ReapNonBlocking);
}
#[skuld::test]
fn any_other_kill_error_also_never_blocks() {
let other = std::io::Error::from(std::io::ErrorKind::NotFound);
assert_eq!(std_teardown_action(&Err(other)), StdTeardown::ReapNonBlocking);
}
#[cfg(unix)]
fn reaped(h: &ProcHandle) -> bool {
let _running = crate::wait::exit_only::seams::force_peek_once(Ok(crate::wait::exit_only::Peek::Running));
matches!(h.state(), crate::signal::RootState::Reaped)
}
#[cfg(unix)]
mod own_reap {
use std::time::{Duration, Instant};
use super::super::ProcHandle;
use super::reaped;
fn adopt_child(child: std::process::Child) -> crate::child::shared::SharedChild {
let id = crate::identity::ProcessId::of(child.id()).found().expect("identity");
crate::child::shared::SharedChild::adopt(child, id).unwrap_or_else(|(e, _)| panic!("adopt: {e}"))
}
fn adopt(argv: &[&str]) -> (ProcHandle, u32) {
let mut cmd = std::process::Command::new(argv[0]);
cmd.args(&argv[1..]);
let child = crate::test_spawn::spawn(&mut cmd).expect("spawn");
let pid = child.id();
(ProcHandle::std(adopt_child(child)), pid)
}
fn killed_zombie() -> ProcHandle {
let (h, pid) = adopt(&["sleep", "300"]);
assert!(!reaped(&h));
assert!(!h.is_reaped(), "is_reaped disagrees with the state");
h.kill().expect("kill");
crate::test_child::wait_until_zombie(pid);
assert!(!reaped(&h), "a kill does not reap");
assert!(!h.is_reaped(), "is_reaped disagrees with the state");
h
}
#[skuld::test]
fn adopting_an_already_exited_child_is_not_a_reap() {
let mut cmd = std::process::Command::new("true");
let child = crate::test_spawn::spawn(&mut cmd).expect("spawn");
crate::test_child::wait_until_zombie(child.id());
let h = ProcHandle::std(adopt_child(child));
assert!(!reaped(&h));
assert!(!h.is_reaped(), "is_reaped disagrees with the state");
h.wait().expect("wait");
assert!(reaped(&h));
assert!(h.is_reaped(), "is_reaped disagrees with the state");
}
#[skuld::test]
fn the_state_is_reaped_as_soon_as_try_wait_records_the_reap() {
use crate::child::shared::seams;
let h = killed_zombie();
let (gate, reached, release) = seams::park_gate();
std::thread::scope(|scope| {
let a = scope.spawn(|| {
let _armed = seams::park_after_reap_recorded_on(gate);
h.try_wait().expect("try_wait")
});
reached.recv().expect("the reap was recorded");
let seen = reaped(&h);
let seen_own = h.is_reaped();
release.send(()).expect("release");
assert!(a.join().expect("join").is_some());
assert!(seen, "the reap was recorded, but the state was not Reaped");
assert!(seen_own, "the reap was recorded, but is_reaped answered false");
});
}
#[skuld::test]
fn wait_is_an_own_reap() {
let h = killed_zombie();
h.wait().expect("wait");
assert!(reaped(&h));
assert!(h.is_reaped(), "is_reaped disagrees with the state");
}
#[skuld::test]
fn try_wait_is_an_own_reap_only_once_it_returns_a_status() {
let (h, pid) = adopt(&["sleep", "300"]);
assert_eq!(h.try_wait().expect("try_wait"), None);
assert!(!reaped(&h));
assert!(!h.is_reaped(), "is_reaped disagrees with the state");
h.kill().expect("kill");
crate::test_child::wait_until_zombie(pid);
assert!(h.try_wait().expect("try_wait").is_some());
assert!(reaped(&h));
assert!(h.is_reaped(), "is_reaped disagrees with the state");
}
#[skuld::test]
fn wait_deadline_is_an_own_reap_only_once_it_returns_a_status() {
let (h, pid) = adopt(&["sleep", "300"]);
assert_eq!(h.wait_deadline(Instant::now()).expect("expired"), None);
assert!(!reaped(&h));
assert!(!h.is_reaped(), "is_reaped disagrees with the state");
h.kill().expect("kill");
crate::test_child::wait_until_zombie(pid);
assert!(h
.wait_deadline(Instant::now() + Duration::from_secs(600))
.expect("wait_deadline")
.is_some());
assert!(reaped(&h));
assert!(h.is_reaped(), "is_reaped disagrees with the state");
}
}
#[cfg(unix)]
#[skuld::test]
fn the_state_is_reaped_as_soon_as_the_reap_is_recorded() {
use crate::child::shared::seams;
let mut child = crate::test_spawn::spawn(&mut crate::test_child::held_std_blocker(std::process::Stdio::null()))
.expect("spawn the blocker");
let stdin = child.stdin.take().expect("piped stdin");
let id = crate::identity::ProcessId::of(child.id()).found().expect("identity");
let shared = crate::child::shared::SharedChild::adopt(child, id).unwrap_or_else(|(e, _)| panic!("adopt: {e}"));
let h = ProcHandle::std(shared);
assert!(!reaped(&h));
assert!(!h.is_reaped(), "is_reaped disagrees with the state");
let (gate, reached, release) = seams::park_gate();
std::thread::scope(|scope| {
let a = scope.spawn(|| {
let _armed = seams::park_after_reap_recorded_on(gate);
h.wait().expect("wait")
});
drop(stdin);
reached.recv().expect("the reap was recorded");
let seen = reaped(&h);
let seen_own = h.is_reaped();
release.send(()).expect("release");
a.join().expect("join");
assert!(seen, "the reap was recorded, but the state was not Reaped");
assert!(seen_own, "the reap was recorded, but is_reaped answered false");
});
}
#[cfg(unix)]
#[skuld::test]
fn the_state_is_reaped_as_soon_as_wait_deadline_records_the_reap() {
use crate::child::shared::seams;
let mut child = crate::test_spawn::spawn(&mut crate::test_child::held_std_blocker(std::process::Stdio::null()))
.expect("spawn the blocker");
let stdin = child.stdin.take().expect("piped stdin");
let id = crate::identity::ProcessId::of(child.id()).found().expect("identity");
let shared = crate::child::shared::SharedChild::adopt(child, id).unwrap_or_else(|(e, _)| panic!("adopt: {e}"));
let h = ProcHandle::std(shared);
assert!(!reaped(&h));
assert!(!h.is_reaped(), "is_reaped disagrees with the state");
let (gate, reached, release) = seams::park_gate();
std::thread::scope(|scope| {
let a = scope.spawn(|| {
let _armed = seams::park_after_reap_recorded_on(gate);
h.wait_deadline(std::time::Instant::now() + std::time::Duration::from_secs(600))
.expect("wait_deadline")
});
drop(stdin);
reached.recv().expect("the reap was recorded");
let seen = reaped(&h);
let seen_own = h.is_reaped();
release.send(()).expect("release");
a.join().expect("join");
assert!(seen, "the reap was recorded, but the state was not Reaped");
assert!(seen_own, "the reap was recorded, but is_reaped answered false");
});
}
#[cfg(unix)]
#[skuld::test]
fn the_state_is_unreaped_while_a_holder_waits() {
use crate::child::shared::seams;
let mut child = crate::test_spawn::spawn(&mut crate::test_child::held_std_blocker(std::process::Stdio::null()))
.expect("spawn the blocker");
let stdin = child.stdin.take().expect("piped stdin");
let id = crate::identity::ProcessId::of(child.id()).found().expect("identity");
let shared = crate::child::shared::SharedChild::adopt(child, id).unwrap_or_else(|(e, _)| panic!("adopt: {e}"));
let h = ProcHandle::std(shared);
let (gate, reached, release) = seams::park_gate();
std::thread::scope(|scope| {
let a = scope.spawn(|| {
let _armed = seams::park_in_unlocked_wait(gate);
h.wait().expect("wait")
});
reached.recv().expect("the holder reached its unlocked wait");
let seen = reaped(&h);
let seen_own = h.is_reaped();
drop(stdin);
release.send(()).expect("release");
a.join().expect("join");
assert!(!seen, "a holder is waiting, but the state read Reaped");
assert!(!seen_own, "a holder is waiting, but is_reaped answered true");
assert!(reaped(&h));
assert!(h.is_reaped(), "is_reaped disagrees with the state");
});
}
#[cfg(unix)]
fn std_handle() -> (ProcHandle, std::process::ChildStdin) {
let mut child = crate::test_spawn::spawn(&mut crate::test_child::held_std_blocker(std::process::Stdio::null()))
.expect("spawn the blocker");
let stdin = child.stdin.take().expect("piped stdin");
let id = crate::identity::ProcessId::of(child.id()).found().expect("identity");
let shared = crate::child::shared::SharedChild::adopt(child, id).unwrap_or_else(|(e, _)| panic!("adopt: {e}"));
(ProcHandle::std(shared), stdin)
}
#[cfg(unix)]
fn teardown_when_the_reap_fails(errno: i32) -> (Option<String>, Vec<log::Level>) {
use crate::child::shared::seams::{self, ForcedWait};
crate::log_capture::install();
let (handle, _stdin) = std_handle();
let id = crate::identity::ProcessId::of(handle.id()).found().expect("identity");
let marker = format!("teardown of child {}", handle.id());
let mark = crate::log_capture::mark();
let forced = seams::force_unlocked_wait(ForcedWait::Errno(errno));
let left = handle.teardown_on_drop(id).left;
drop(forced);
handle.wait().expect("reap the killed child");
let levels = crate::log_capture::records_since_on_current_thread(mark, &marker)
.into_iter()
.map(|(level, _)| level)
.collect();
(left, levels)
}
#[cfg(unix)]
#[skuld::test]
fn a_failed_teardown_reap_is_returned_and_not_logged() {
let (left, levels) = teardown_when_the_reap_fails(libc::EIO);
assert!(
left.as_deref()
.is_some_and(|text| text.contains("the reap after the kill failed")),
"{left:?}"
);
assert_eq!(levels, Vec::<log::Level>::new());
}
#[cfg(unix)]
#[skuld::test]
fn a_teardown_reap_that_meets_echild_is_debug() {
assert_eq!(
teardown_when_the_reap_fails(libc::ECHILD),
(None, vec![log::Level::Debug])
);
}
#[cfg(unix)]
#[skuld::test]
fn a_reap_after_a_delivered_tree_kill_that_meets_echild_is_killed() {
use crate::child::shared::seams::{self, ForcedWait};
let (handle, _stdin) = std_handle();
handle.kill().expect("the stand-in for the tree kill");
let forced = seams::force_unlocked_wait(ForcedWait::Errno(libc::ECHILD));
let torn = handle.reap_after_tree_kill();
drop(forced);
handle.wait().expect("reap the killed child");
assert_eq!(torn.fate, crate::error::ChildFate::Killed);
}
#[cfg(unix)]
#[skuld::test]
fn a_failed_wait_after_a_kill_that_delivered_nothing_is_unknown() {
use crate::child::shared::seams::{self, ForcedWait};
let (handle, _stdin) = std_handle();
let id = crate::identity::ProcessId::of(handle.id()).found().expect("identity");
let pid = handle.id() as libc::pid_t;
let mut status = 0;
unsafe {
assert_eq!(libc::kill(pid, libc::SIGKILL), 0);
assert_eq!(libc::waitpid(pid, &mut status, 0), pid);
}
let forced = seams::force_unlocked_wait(ForcedWait::Errno(libc::EIO));
let torn = handle.teardown_on_drop(id);
drop(forced);
assert_eq!(torn.fate, crate::error::ChildFate::Unknown);
}