#[tokio::test]
async fn wait_and_reap_waits_for_the_childs_own_exit_and_never_kills() {
use std::io::{Read, Write};
let (listener, addr) = crate::test_child::registration_rendezvous();
let mut child = {
let _guard = crate::child::spawn::spawn_lock();
::tokio::process::Command::new(std::env::current_exe().expect("current_exe"))
.args([
"--test-threads=1",
"--exact",
crate::test_child::FIXTURE_REGISTERS_THEN_BLOCKS_TEST,
])
.env(crate::test_child::FIXTURE_REGISTERS_THEN_BLOCKS_ADDR_ENV, &addr)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.expect("spawn the rendezvous fixture")
};
let pid = child.id().expect("tokio owns an un-reaped child");
let (mut sock, _) = listener.accept().expect("accept the fixture's rendezvous connection");
let mut tag = [0u8; 1];
sock.read_exact(&mut tag).expect("read the fixture's readiness tag");
sock.write_all(b"g").expect("release the fixture");
sock.flush().expect("flush the release byte");
super::wait_and_reap(&mut child, pid, true);
let status = child
.try_wait()
.expect("try_wait")
.expect("wait_and_reap must return only after the child has exited");
assert_eq!(
status.code(),
Some(0),
"the fixture must have exited on its own; a kill inside wait_and_reap would report the \
forced code instead ({status:?})"
);
}
fn spawn_a_tokio_child_that_exits() -> ::tokio::process::Child {
let _guard = crate::child::spawn::spawn_lock();
::tokio::process::Command::new(std::env::current_exe().expect("current_exe"))
.args(["--exact", "__cosca_no_such_test__"])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.expect("spawn")
}
#[cfg_attr(
debug_assertions,
should_panic(expected = "already-reaped child where one was impossible")
)]
#[tokio::test]
async fn wait_and_reap_refuses_an_already_reaped_child_the_caller_never_awaited() {
let mut child = spawn_a_tokio_child_that_exits();
let pid = child.id().expect("tokio owns an un-reaped child");
child.wait().await.expect("wait");
super::wait_and_reap(&mut child, pid, false);
assert!(child.id().is_none(), "the child was reaped by the wait() above");
}
#[tokio::test]
async fn wait_and_reap_accepts_an_already_reaped_child_the_caller_may_have_awaited() {
let mut child = spawn_a_tokio_child_that_exits();
let pid = child.id().expect("tokio owns an un-reaped child");
child.wait().await.expect("wait");
super::wait_and_reap(&mut child, pid, true); }
#[cfg(unix)]
#[cfg_attr(
debug_assertions,
should_panic(expected = "already-reaped child where one was impossible")
)]
#[tokio::test]
async fn the_elevated_cleanup_entry_refuses_an_already_reaped_child() {
let mut cmd = crate::tokio::Command::new();
cmd.executable(std::env::current_exe().expect("current_exe"))
.args(["cosca_unit_tests", "--exact", "__cosca_no_such_test__"]);
cmd.stdout(crate::stdio::Stdio::null()).expect("stdout null");
cmd.stderr(crate::stdio::Stdio::null()).expect("stderr null");
let mut child = cmd.spawn().expect("spawn");
child.wait().await.expect("wait");
child.wait_and_reap_blocking();
assert!(
matches!(child.try_wait(), Ok(Some(_))),
"the child was reaped by the wait() above"
);
}