use std::time::{Duration, Instant};
use processkit::{ProcessGroup, wait_any};
use crate::common::*;
#[tokio::test]
#[ignore = "spawns real subprocesses and races their exits"]
async fn wait_any_returns_first_finisher() {
let group = ProcessGroup::new().expect("create group");
let mut slow = group.start(&sleep_secs(15)).await.expect("start slow");
let mut fast = group.start(&sleep_secs(1)).await.expect("start fast");
let (idx, code) = tokio::time::timeout(
Duration::from_secs(10),
wait_any(&mut [&mut slow, &mut fast]),
)
.await
.expect("race finished in time")
.expect("race");
assert_eq!(idx, 1, "the 1-second sleeper should finish first");
assert_eq!(code, Some(0), "the fast sleeper exits cleanly");
}
#[tokio::test]
#[ignore = "spawns real subprocesses; proves the race loser stays usable"]
async fn wait_any_losers_still_waitable() {
let group = ProcessGroup::new().expect("create group");
let mut slow = group.start(&sleep_secs(30)).await.expect("start slow");
let mut fast = group.start(&sleep_secs(1)).await.expect("start fast");
let (idx, _code) = tokio::time::timeout(
Duration::from_secs(10),
wait_any(&mut [&mut slow, &mut fast]),
)
.await
.expect("race finished in time")
.expect("race");
assert_eq!(idx, 1);
slow.start_kill().expect("kill the loser");
let start = Instant::now();
let _ = tokio::time::timeout(Duration::from_secs(10), slow.wait())
.await
.expect("loser reaped in time")
.expect("wait");
assert!(
start.elapsed() < Duration::from_secs(5),
"loser wait was not prompt (took {:?})",
start.elapsed()
);
}
#[tokio::test]
#[ignore = "spawns a long-lived subprocess and kills it early"]
async fn start_kill_terminates_a_running_process() {
let mut process = sleeper().start().await.expect("start sleeper");
assert!(process.pid().is_some());
process.start_kill().expect("start_kill");
let start = Instant::now();
let _ = tokio::time::timeout(Duration::from_secs(10), process.wait())
.await
.expect("killed process should be reaped promptly")
.expect("wait");
assert!(
start.elapsed() < Duration::from_secs(5),
"kill was not prompt (took {:?})",
start.elapsed()
);
}