use std::time::Duration;
use super::{grace_wait, wait_exit};
use crate::identity::ProcessId;
fn std_blocker() -> std::process::Child {
let _guard = crate::child::spawn::spawn_lock();
let mut cmd = std::process::Command::new(if cfg!(windows) { "ping" } else { "sleep" });
#[cfg(windows)]
cmd.args(["-n", "30", "127.0.0.1"]).stdout(std::process::Stdio::null());
#[cfg(unix)]
cmd.arg("30");
cmd.spawn().expect("spawn std blocker")
}
#[tokio::test]
async fn grace_wait_true_for_exited_unreaped_child() {
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
child.kill().expect("kill");
let exited = grace_wait(id, Duration::from_secs(30)).await.expect("grace_wait");
assert!(exited, "an exited (unreaped) child must report exited");
child.wait().expect("reap");
}
#[tokio::test]
async fn grace_wait_false_for_live_child_at_zero_grace() {
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
let exited = grace_wait(id, Duration::ZERO).await.expect("grace_wait");
assert!(!exited, "a live child at ZERO grace must report still-alive");
child.kill().expect("cleanup");
child.wait().expect("reap");
}
#[tokio::test]
async fn grace_wait_true_for_stale_identity() {
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
child.kill().expect("kill");
child.wait().expect("reap"); let exited = grace_wait(id, Duration::from_secs(30)).await.expect("grace_wait");
assert!(exited, "a stale identity (reaped child) must report exited, never hang");
}
#[tokio::test]
async fn grace_wait_true_when_child_dies_mid_wait() {
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
let watch = ::tokio::spawn(grace_wait(id, Duration::from_secs(30)));
child.kill().expect("kill mid-wait");
let exited = watch.await.expect("join").expect("grace_wait");
assert!(exited, "the watch must resolve on the child's exit");
child.wait().expect("reap");
}
#[cfg(windows)]
#[test]
fn cancel_event_releases_the_blocking_wait() {
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
let cancel = crate::wait::backend::new_cancel_event().expect("event");
crate::wait::backend::signal_cancel(&cancel);
let exited = crate::wait::backend::block_until_exit_or_cancel(id, None, &cancel).expect("cancellable wait");
assert!(!exited, "a live child with a signaled cancel must report still-alive");
child.kill().expect("cleanup");
child.wait().expect("reap");
}
#[cfg(windows)]
#[test]
fn cancel_event_signaled_mid_wait_releases_the_blocking_wait() {
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
let cancel = std::sync::Arc::new(crate::wait::backend::new_cancel_event().expect("event"));
let watcher = std::thread::spawn({
let cancel = cancel.clone();
move || crate::wait::backend::block_until_exit_or_cancel(id, None, &cancel)
});
crate::wait::backend::signal_cancel(&cancel);
let exited = watcher.join().expect("watcher thread").expect("cancellable wait");
assert!(!exited, "a live child with a signaled cancel must report still-alive");
child.kill().expect("cleanup");
child.wait().expect("reap");
}
#[cfg(target_os = "macos")]
#[tokio::test]
async fn watch_loop_survives_a_non_exit_drain_cycle() {
let mut decoy = std_blocker();
let target = std_blocker();
let target_id = ProcessId::of(target.id()).found().expect("identity of live target");
let kq = crate::wait::backend::arm_proc_exit(target_id)
.expect("arm target")
.expect("a live target arms");
assert!(
crate::wait::backend::arm_note_exit_on(&kq, decoy.id())
.expect("arm decoy")
.is_some(),
"a live decoy arms"
);
decoy.kill().expect("kill decoy");
let afd = ::tokio::io::unix::AsyncFd::with_interest(super::KqueueFd(kq), ::tokio::io::Interest::READABLE)
.expect("register");
let target_cell = std::cell::RefCell::new(None);
let mut pending = Some(target);
let watch = super::watch_readable(&afd, |kq| {
let drained = crate::wait::backend::drain_proc_exit(kq)?;
if let Some(mut t) = pending.take() {
t.kill().expect("kill target mid-cycle");
*target_cell.borrow_mut() = Some(t);
return Ok(None);
}
Ok(drained)
});
::tokio::time::timeout(Duration::from_secs(30), watch)
.await
.expect("the re-awaited loop must resolve on the target's exit")
.expect("watch");
let mut target = target_cell
.borrow_mut()
.take()
.expect("target stored by the first cycle");
target.wait().expect("reap target");
decoy.wait().expect("reap decoy");
}
#[cfg(target_os = "linux")]
mod classify {
use ::tokio::io::Ready;
use super::super::classify_pidfd_ready;
#[test]
fn readable_and_read_closed_mean_exited() {
assert!(matches!(classify_pidfd_ready(Ready::READABLE), Some(Ok(()))));
assert!(matches!(classify_pidfd_ready(Ready::READ_CLOSED), Some(Ok(()))));
}
#[test]
fn error_readiness_is_surfaced_not_swallowed() {
assert!(matches!(
classify_pidfd_ready(Ready::ERROR | Ready::READABLE),
Some(Err(_))
));
assert!(matches!(classify_pidfd_ready(Ready::ERROR), Some(Err(_))));
}
#[test]
fn unclassified_readiness_retries_never_a_false_verdict() {
assert!(classify_pidfd_ready(Ready::EMPTY).is_none());
}
}
#[tokio::test]
async fn wait_exit_resolves_for_exited_unreaped_child() {
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
child.kill().expect("kill");
wait_exit(id).await.expect("wait_exit");
child.wait().expect("reap");
}
#[tokio::test]
async fn wait_exit_resolves_when_child_dies_mid_wait() {
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
let watch = ::tokio::spawn(wait_exit(id));
child.kill().expect("kill mid-wait");
watch.await.expect("join").expect("wait_exit");
child.wait().expect("reap");
}
#[tokio::test]
async fn grace_wait_zero_reports_an_observed_exit() {
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
child.kill().expect("kill");
wait_exit(id).await.expect("exit observed");
assert!(
grace_wait(id, Duration::ZERO).await.expect("zero probe"),
"an observed-exited child must report exited at ZERO grace"
);
child.wait().expect("reap");
}
#[tokio::test]
async fn wait_exit_cancel_leaves_child_untouched() {
use std::future::Future;
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
{
let mut fut = std::pin::pin!(wait_exit(id));
let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
if let std::task::Poll::Ready(r) = fut.as_mut().poll(&mut cx) {
panic!("unbounded watch resolved at first poll on a live child: {r:?}");
}
} assert_eq!(
id.is_alive(),
crate::identity::Liveness::Alive,
"a cancelled watch must not affect the child"
);
child.kill().expect("cleanup");
child.wait().expect("reap");
}
#[cfg(windows)]
#[tokio::test]
async fn wait_exit_drop_releases_the_windows_watcher() {
use std::future::Future;
let (tx, rx) = std::sync::mpsc::channel();
super::fault_observer::install_release_observer(tx);
let mut child = std_blocker();
let id = ProcessId::of(child.id()).found().expect("identity of live child");
{
let mut fut = std::pin::pin!(wait_exit(id));
let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
if let std::task::Poll::Ready(r) = fut.as_mut().poll(&mut cx) {
panic!("unbounded watch resolved at first poll on a live child: {r:?}");
}
} rx.recv()
.expect("the blocking watcher must return after the drop released it");
assert_eq!(
id.is_alive(),
crate::identity::Liveness::Alive,
"release must be signal-free"
);
child.kill().expect("cleanup");
child.wait().expect("reap");
}