Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/edda

crates/appliance-pid1/tests/reap_all.rs

//! Gunnar's reaper test (pid1 `reap_once_reports_the_pid_it_reaped_and_nothing_while_the_child_lives`),
//! against edda's `reap_all`, in its own test binary: `waitpid(-1)` in one binary would steal
//! the other reaper test's child.

/// The reaper is asserted against the **pid it reaped**: nothing while the child lives,
/// that exact pid once it has exited.
#[test]
fn reap_all_reports_the_pid_it_reaped_and_nothing_while_the_child_lives() {
    let child = std::process::Command::new("sleep").arg("30").spawn().expect("spawning a test child");
    let pid = child.id() as i32;
    assert!(!appliance_pid1::reap_all().contains(&pid), "reap_all() claimed to have reaped pid {pid}, which is still running");
    let mut child = child;
    child.kill().expect("killing the test child");
    std::mem::forget(child);
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
    let mut seen = false;
    while std::time::Instant::now() < deadline {
        if appliance_pid1::reap_all().contains(&pid) {
            seen = true;
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
    assert!(seen, "reap_all() never reported pid {pid} after it exited; an orphan on an appliance would stay a zombie");
}