use std::time::Instant;
use crate::identity::{uniq_info, ReadPurpose, UniqRead};
use crate::test_groups::{tracer_group, Group};
use crate::test_support::tracer::{self, Mode, Report};
use crate::wait::backend::{await_reapable, test_hooks, Waited};
use crate::wait::exit_only::{self, Reap, Target};
#[skuld::test]
fn a_child_held_by_a_tracer_is_running_until_the_hand_back(#[fixture(tracer_group)] _group: &Group) {
let (mut tracee, _) = tracer::spawn_tracee(tracer::Tracee::Plain);
let stdin = tracee.stdin().expect("the tracee's stdin is piped");
let pid = tracee.id().pid();
let unique = match uniq_info(pid, ReadPurpose::Adopt) {
UniqRead::Found(info) => info.unique_id,
other => panic!("the tracee's unique id: {other:?}"),
};
let mut helper = tracer::start(Mode::Auto).attach(&mut tracee);
assert_eq!(helper.recv(), Report::Attached);
let target = Target::pid(pid, Some(unique));
assert_eq!(exit_only::try_reap(&target).expect("try_reap"), Reap::Running);
let held = await_reapable(pid, Some(unique), Some(Instant::now())).expect("wait");
assert_eq!(held, Waited::DeadlinePassed);
drop(stdin);
assert_eq!(helper.recv(), Report::Reaped);
assert_eq!(await_reapable(pid, Some(unique), None).expect("wait"), Waited::Reapable);
drop(helper);
let status = tracee.wait().expect("the handed-back zombie is ours to reap");
assert!(status.success(), "{status:?}");
}
fn held() -> (crate::Child, std::io::PipeWriter) {
let (mut tracee, _) = tracer::spawn_tracee(tracer::Tracee::Plain);
let stdin = tracee.stdin().expect("the tracee's stdin is piped");
(tracee, stdin)
}
fn wait_blocked<'s, G>(
scope: &'s std::thread::Scope<'s, '_>,
tracee: &'s crate::Child,
arm: impl FnOnce() -> G + Send + 's,
) -> std::thread::ScopedJoinHandle<'s, Result<std::process::ExitStatus, crate::error::Error>> {
let (blocked_tx, blocked_rx) = std::sync::mpsc::channel();
let waiter = scope.spawn(move || {
let _armed = arm();
let _blocked = test_hooks::on_kevent_round(0, move || _ = blocked_tx.send(()));
tracee.wait()
});
blocked_rx.recv().expect("the waiter must reach its blocking kevent");
waiter
}
#[skuld::test]
fn a_wait_blocked_across_the_exit_and_the_hand_back_returns_the_status(#[fixture(tracer_group)] _group: &Group) {
let (tracee, stdin) = held();
let mut helper = tracer::start(Mode::Auto).attach_shared(&tracee);
assert_eq!(helper.recv(), Report::Attached);
std::thread::scope(|s| {
let waiter = wait_blocked(s, &tracee, || ());
assert_eq!(tracee.try_wait().expect("try_wait"), None, "a held child is running");
drop(stdin);
assert_eq!(helper.recv(), Report::Reaped);
let status = waiter.join().expect("waiter").expect("the wait ends with the status");
assert!(status.success(), "{status:?}");
});
}
#[skuld::test]
fn kill_ends_a_held_child_and_the_wait_returns_the_kill(#[fixture(tracer_group)] _group: &Group) {
let (tracee, _stdin) = held();
let mut helper = tracer::start(Mode::Auto).attach_shared(&tracee);
assert_eq!(helper.recv(), Report::Attached);
assert_eq!(tracee.try_wait().expect("try_wait"), None, "a held child is running");
tracee.kill().expect("kill");
assert_eq!(helper.recv(), Report::Reaped);
drop(helper);
let status = tracee.wait().expect("the handed-back zombie is ours to reap");
assert_eq!(
std::os::unix::process::ExitStatusExt::signal(&status),
Some(libc::SIGKILL)
);
}
#[skuld::test]
fn a_zombie_a_tracer_holds_is_running_until_the_hand_back(#[fixture(tracer_group)] _group: &Group) {
let (tracee, stdin) = held();
let mut helper = tracer::start(Mode::Hold).attach_shared(&tracee);
assert_eq!(helper.recv(), Report::Attached);
std::thread::scope(|s| {
let waiter = wait_blocked(s, &tracee, || ());
drop(stdin);
assert_eq!(helper.recv(), Report::Exited);
assert_eq!(
tracee.try_wait().expect("try_wait"),
None,
"a held zombie is not ours yet"
);
helper.signal();
assert_eq!(helper.recv(), Report::Reaped);
let status = waiter.join().expect("waiter").expect("the wait ends with the status");
assert!(status.success(), "{status:?}");
});
}