use std::pin::pin;
use std::process::{ChildStdin, ExitStatus};
use super::fixtures::{identity_of, status};
use crate::child::owned::{ChildWaitOutcome, OwnedChild};
use crate::elevation::front::TrackedKind;
use crate::os_process::child::ExitRecord;
use crate::os_process::fake::{FakeProcess, ReapAnswer};
use crate::tokio::drive::drive_tests::poll_once;
fn exit() -> ExitRecord {
ExitRecord::Status(ExitStatus::default())
}
pub(super) fn exits_three_on_eof() -> (OwnedChild, ChildStdin) {
#[cfg(unix)]
let mut command = std::process::Command::new("sh");
#[cfg(unix)]
command.args(["-c", "read x; exit 3"]);
#[cfg(windows)]
let mut command = std::process::Command::new("cmd");
#[cfg(windows)]
command.args(["/D", "/C", "more >nul & exit 3"]);
command
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null());
let (child, stdin) = crate::test_child::StdChild::spawn(&mut command).into_parts();
let id = identity_of(&child);
let owned = OwnedChild::adopt(child, id, TrackedKind::Own).unwrap_or_else(|(e, _)| panic!("adopt: {e}"));
(owned, stdin)
}
#[cfg(unix)]
#[skuld::test(runtime = crate::tokio::test_runtime::paused)]
async fn wait_async_reaps_and_caches_the_record() {
let fake = FakeProcess::new([]).reaps([ReapAnswer::Recheck, ReapAnswer::Exited(exit())]);
let mut child = OwnedChild::new(Box::new(fake.clone()), TrackedKind::Own);
assert_eq!(child.wait_async().await.unwrap(), ChildWaitOutcome::Exited(exit()));
assert_eq!(child.wait(None).unwrap(), ChildWaitOutcome::Exited(exit()));
assert_eq!(fake.looks(), 2);
}
#[skuld::test]
async fn wait_async_after_a_sync_wait_answers_from_the_cache() {
let fake = FakeProcess::new([]).reaps([ReapAnswer::Exited(exit())]);
let mut child = OwnedChild::new(Box::new(fake.clone()), TrackedKind::Own);
child.wait(None).unwrap();
assert_eq!(child.wait_async().await.unwrap(), ChildWaitOutcome::Exited(exit()));
assert_eq!(fake.looks(), 1);
}
#[cfg(unix)]
#[skuld::test(runtime = crate::tokio::test_runtime::paused)]
async fn wait_async_pauses_on_tokio_s_timer_for_a_recheck() {
crate::wait::block_probe::take();
let fake = FakeProcess::new([]).reaps([ReapAnswer::Recheck, ReapAnswer::Recheck, ReapAnswer::Exited(exit())]);
let mut child = OwnedChild::new(Box::new(fake), TrackedKind::Own);
child.wait_async().await.unwrap();
let pauses: Vec<_> = crate::wait::block_probe::take()
.into_iter()
.map(|pause| pause.expect("a pause is bounded"))
.collect();
assert_eq!(crate::os_process::drive::drive_tests::whole_ms(&pauses), [1, 2]);
}
#[skuld::test]
async fn wait_async_waits_for_a_real_exit() {
let (mut child, stdin) = exits_three_on_eof();
let outcome = {
let mut wait = pin!(child.wait_async());
assert!(poll_once(wait.as_mut()).is_pending(), "a running child is waited for");
drop(stdin);
wait.await
};
assert_eq!(status(outcome).code(), Some(3));
assert!(child.exit_seen());
}
#[skuld::test]
async fn a_dropped_wait_async_leaves_nobody_waiting() {
let (mut child, stdin) = exits_three_on_eof();
{
let mut wait = pin!(child.wait_async());
assert!(poll_once(wait.as_mut()).is_pending(), "a running child is waited for");
}
assert!(!child.exit_seen());
drop(stdin);
assert_eq!(status(child.wait(None)).code(), Some(3));
}
#[skuld::test]
fn wait_async_is_send() {
fn assert_send<T: Send>(_: &T) {}
let mut child = OwnedChild::new(Box::new(FakeProcess::new([])), TrackedKind::Own);
assert_send(&child.wait_async());
}