use std::io::BufRead as _;
use std::process::Stdio;
use crate::identity::stat_parse::{parse_num_threads, parse_starttime_jiffies, parse_state};
use crate::identity::{Key, Liveness, ProcessId, Resolved};
use crate::Recursive;
#[skuld::test]
fn a_process_whose_leader_thread_exited_is_alive_and_keeps_its_children() {
let mut root = crate::test_child::StdChild::spawn(
crate::test_reexec::command(std::env::current_exe().expect("current_exe"))
.args(crate::test_child::zombie_leader_root::argv())
.stdin(Stdio::piped())
.stdout(Stdio::piped()),
);
let _stdin = root.stdin.take().expect("piped stdin");
let mut stdout = std::io::BufReader::new(root.stdout.take().expect("piped stdout"));
let mut line = String::new();
stdout.read_line(&mut line).expect("the cat's pid");
let cat = ProcessId::of(line.trim().parse().expect("the cat's pid"))
.found()
.expect("the cat's identity");
let id = ProcessId::of(root.id()).found().expect("the root's identity");
line.clear();
stdout.read_line(&mut line).expect("the leader's exit report");
assert_eq!(line, "exited\n");
let stat = loop {
let stat = std::fs::read(format!("/proc/{}/stat", root.id())).expect("the root's stat");
if parse_state(&stat) == Some(b'Z') {
break stat;
}
std::thread::yield_now();
};
assert!(
parse_num_threads(&stat).is_some_and(|threads| threads > 1),
"premise: a zombie leader whose thread runs on counts it: {}",
String::from_utf8_lossy(&stat)
);
let ticks = parse_starttime_jiffies(&stat).expect("the root's start time");
let by_stat = ProcessId::from_parts_for_test(root.id(), Key::Ticks(ticks));
assert_eq!(
by_stat.is_alive(),
Liveness::Alive,
"the /proc read: the process has not exited"
);
assert_eq!(
id.is_alive(),
Liveness::Alive,
"the host's own id: the process has not exited"
);
for (who, id) in [("the /proc read", by_stat), ("the host's own id", id)] {
assert_eq!(
super::exit_begun_tests::begun(&id),
Resolved::Found(false),
"{who}: a thread runs, so the group is not exiting"
);
}
assert_eq!(
id.children(Recursive::No).expect("the root's children"),
vec![cat],
"the cat's parent is still the root's pid"
);
}