Lines
95.83 %
Functions
100 %
Branches
use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
use super::*;
fn state_of(pid: u32) -> Option<char> {
let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
stat.rsplit_once(')')?.1.trim().chars().next()
}
fn wait_until(what: &str, mut done: impl FnMut() -> bool) {
let deadline = Instant::now() + Duration::from_secs(10);
while !done() {
assert!(Instant::now() < deadline, "timed out waiting for {what}");
std::thread::sleep(Duration::from_millis(20));
/// A group with a `sleep` grandchild, as a watched cargo has its test binaries.
fn spawn_group() -> (std::process::Child, Group, u32) {
let mut child = Command::new("bash")
.args(["-c", "sleep 120 & wait"])
.stdout(Stdio::null())
.process_group(0)
.spawn()
.expect("spawn bash");
let group = Group::of(&child);
wait_until("the grandchild", || group.members().len() == 2);
let grandchild = group
.members()
.into_iter()
.find(|&pid| pid != child.id())
.expect("a grandchild");
let _ = child.try_wait();
(child, group, grandchild)
#[test]
fn thread_states_cover_every_member_of_the_group() {
let _processes = crate::test_support::processes();
let (mut child, group, grandchild) = spawn_group();
let states = group.thread_states();
group.kill();
let _ = child.wait();
assert!(
states
.iter()
.any(|line| line.contains(&format!("pid {grandchild} "))),
"the grandchild is missing from {states:?}"
);
fn a_frozen_group_stays_visible_until_it_is_killed() {
group.freeze();
wait_until("the grandchild to stop", || {
state_of(grandchild) == Some('T')
});
assert!(group.members().contains(&grandchild));
wait_until(
"the grandchild to be gone",
|| !matches!(state_of(grandchild), Some(state) if state != 'Z'),
const HELPER_ENV: &str = "XTASK_SIGNAL_HELPER";
/// Body of the child process of `termination_keeps_the_default_action_between_runs`;
/// a no-op in the normal suite.
fn signal_helper() {
if std::env::var_os(HELPER_ENV).is_none() {
return;
for _ in 0..2 {
drop(Termination::watch().expect("watch"));
println!("ready");
std::thread::sleep(Duration::from_secs(60));
/// Handlers outlive a run, so they must go back to terminating the process.
fn termination_keeps_the_default_action_between_runs() {
use std::io::{BufRead, BufReader};
use std::os::unix::process::ExitStatusExt;
let mut helper = Command::new(std::env::current_exe().expect("test exe"))
.args([
"--exact",
"watchdog::group::tests::signal_helper",
"--nocapture",
])
.env(HELPER_ENV, "1")
.stdout(Stdio::piped())
.expect("spawn helper");
let stdout = helper.stdout.take().expect("helper stdout");
let ready = BufReader::new(stdout)
.lines()
.map_while(Result::ok)
.any(|line| line == "ready");
assert!(ready, "the helper never got past its watched runs");
let pid = Pid::from_raw(i32::try_from(helper.id()).expect("pid"));
nix::sys::signal::kill(pid, Signal::SIGTERM).expect("signal the helper");
let status = helper.wait().expect("helper exit");
assert_eq!(status.signal(), Some(SIGTERM), "{status:?}");
/// Body of the child process of `an_inner_watch_leaves_the_outer_one_in_force`;
fn nested_signal_helper() {
let outer = Termination::watch().expect("outer watch");
drop(Termination::watch().expect("inner watch"));
for _ in 0..600 {
if outer.requested() {
println!("seen");
std::thread::sleep(Duration::from_millis(100));
fn an_inner_watch_leaves_the_outer_one_in_force() {
"watchdog::group::tests::nested_signal_helper",
let mut lines = BufReader::new(stdout).lines().map_while(Result::ok);
lines.any(|line| line == "ready"),
"the helper never got past its watches"
let seen = lines.any(|line| line == "seen");
assert!(seen && status.success(), "{status:?}");