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)]
mod own_reap {
use std::time::{Duration, Instant};
use super::super::ProcHandle;
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!(!h.is_reaped());
h.kill().expect("kill");
crate::test_child::wait_until_zombie(pid);
assert!(!h.is_reaped(), "a kill does not reap");
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!(!h.is_reaped());
h.wait().expect("wait");
assert!(h.is_reaped());
}
#[skuld::test]
fn is_reaped_is_true_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 = h.is_reaped();
release.send(()).expect("release");
assert!(a.join().expect("join").is_some());
assert!(seen, "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!(h.is_reaped());
}
#[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!(!h.is_reaped());
h.kill().expect("kill");
crate::test_child::wait_until_zombie(pid);
assert!(h.try_wait().expect("try_wait").is_some());
assert!(h.is_reaped());
}
#[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!(!h.is_reaped());
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!(h.is_reaped());
}
}
#[skuld::test]
fn is_reaped_is_true_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!(!h.is_reaped());
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 = h.is_reaped();
release.send(()).expect("release");
a.join().expect("join");
assert!(seen, "the reap was recorded, but is_reaped answered false");
});
}
#[skuld::test]
fn is_reaped_is_true_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!(!h.is_reaped());
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 = h.is_reaped();
release.send(()).expect("release");
a.join().expect("join");
assert!(seen, "the reap was recorded, but is_reaped answered false");
});
}
#[skuld::test]
fn is_reaped_is_false_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 = h.is_reaped();
drop(stdin);
release.send(()).expect("release");
a.join().expect("join");
assert!(!seen, "a holder is waiting, but is_reaped read it as reaped");
assert!(h.is_reaped());
});
}
#[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_levels_when_the_reap_fails(errno: i32) -> 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));
handle.teardown_on_drop(id);
drop(forced);
handle.wait().expect("reap the killed child");
crate::log_capture::records_since_on_current_thread(mark, &marker)
.into_iter()
.map(|(level, _)| level)
.collect()
}
#[cfg(unix)]
#[skuld::test]
fn a_failed_teardown_reap_is_warned() {
assert_eq!(teardown_levels_when_the_reap_fails(libc::EIO), [log::Level::Warn]);
}
#[cfg(unix)]
#[skuld::test]
fn a_teardown_reap_that_meets_echild_is_debug() {
assert_eq!(teardown_levels_when_the_reap_fails(libc::ECHILD), [log::Level::Debug]);
}