use std::cell::{Cell, RefCell};
use crate::test_child::Console;
use std::rc::Rc;
use crate::child::spawn::fault::{self, SpawnPoint};
use crate::error::ChildFate;
use crate::os_process::child::PeekOutcome;
use crate::wait::exit_only::seams::{assert_peeks_exhausted, force_peeks};
fn teardown_with_forced_looks(point: SpawnPoint, force: fn(bool)) -> (ChildFate, Vec<String>) {
crate::log_capture::install();
let mark = crate::log_capture::mark();
let (stdin, _writer) = crate::test_child::held_writer_stdin();
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
let forced = Rc::new(RefCell::new(None));
let pid = Rc::new(Cell::new(None));
let _hook = fault::set_at(point, {
let (forced, pid) = (Rc::clone(&forced), Rc::clone(&pid));
move || {
pid.set(Some(fault::spawn_pid()));
*forced.borrow_mut() = Some(force_peeks([Ok(PeekOutcome::Running), Ok(PeekOutcome::Running)]));
}
});
let _warn = crate::tokio::child::fault::warn_slow_teardowns_after(std::time::Duration::ZERO);
force(true);
let spawned = crate::child::spawn::spawn_uncommitted(cmd.as_sync_mut(), crate::tokio::child::async_policy());
force(false);
let failed = spawned.expect_err("the forced failure fails the spawn");
assert_peeks_exhausted();
let (_, fate) = failed.expect_may_have_started_with();
let pid = pid.get().expect("the hook ran");
let warns = crate::log_capture::records_since(mark, &format!("child {pid}"))
.into_iter()
.filter(|text| text.contains("still waiting"))
.collect();
(fate, warns)
}
#[skuld::test]
async fn macos_a_teardown_after_a_failed_attach_warns_once_past_its_threshold() {
let (fate, warns) = teardown_with_forced_looks(SpawnPoint::BeforeAttach, fault::set_force_attach_failure);
assert_eq!(warns.len(), 1, "one warn, and the wait goes on: {warns:?}");
assert_eq!(fate, ChildFate::Reaped, "the wait goes on past the threshold");
}