use std::time::Duration;
use rustix::io::Errno;
use rustix::process::{Pid, test_kill_process_group};
use super::{after, kept, run_sh};
use crate::testing::pid_in;
use crate::{Escalation, Outcome, Signal};
const RUNS: usize = 20;
fn assert_empty(group: i32) {
let group = Pid::from_raw(group).unwrap();
assert_eq!(
test_kill_process_group(group),
Err(Errno::SRCH),
"a process of the group is left after the run returned"
);
}
#[test]
fn a_run_leaves_no_process_of_the_group_after_its_child_ends() {
for _ in 0..RUNS {
let finished = run_sh("echo $$; sleep 30 & sleep 30 & exit 0", &kept());
assert_empty(pid_in(finished.stdout().bytes()));
}
}
#[test]
fn a_run_leaves_no_process_of_the_group_when_a_descendant_forks_in_a_loop() {
for _ in 0..RUNS {
let finished = run_sh("echo $$; (while :; do (:) & done) & sleep 0.05", &kept());
assert_empty(pid_in(finished.stdout().bytes()));
}
}
#[test]
fn a_run_leaves_no_process_of_the_group_after_it_stops_its_child() {
let escalation = Escalation::default().step(Signal::Terminate, Duration::from_millis(100));
for _ in 0..RUNS {
let options = kept().deadline(after(0.1)).escalation(escalation.clone());
let finished = run_sh("echo $$; sleep 30 & sleep 30 & sleep 30", &options);
assert_eq!(finished.outcome(), Outcome::TimedOut);
assert_empty(pid_in(finished.stdout().bytes()));
}
}