use std::cell::Cell;
use std::future::Future as _;
use std::rc::Rc;
use std::task::{Context, Poll};
use crate::child::spawn::fault::{self, SpawnPoint};
use crate::command::CommandInput;
use crate::graceful_hooks::{self, HookPoint};
use crate::identity::{Liveness, Resolved};
use crate::test_child::{held_writer_stdin, tokio_command, Console, BLOCKER_ARGV};
const SPAWN_TEST_ENV: &str = "COSCA_SPAWN_TEST";
#[skuld::test]
async fn the_spawn_runs_on_a_blocking_pool_thread() {
crate::tokio::test_runtime::assert_current_thread();
let (tx, rx) = std::sync::mpsc::channel();
let _probe = fault::install_ran(tx);
let (stdin, writer) = held_writer_stdin();
let mut cmd = tokio_command(Console::Hidden);
cmd.args(BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("stdin");
let mut child = cmd.spawn().await.expect("spawn");
drop(writer);
child.wait().await.expect("wait");
let ran: Vec<_> = rx.try_iter().collect();
assert!(
ran.iter().any(|at| at.point == SpawnPoint::BeforeIdentity),
"the sync spawn reported no step: the probe did not follow it"
);
let here = std::thread::current().id();
for at in &ran {
assert_ne!(
at.thread, here,
"the sync spawn ran on the runtime's thread at {:?}",
at.point
);
}
}
#[skuld::test]
async fn a_completed_spawn_leaves_the_command_as_the_sync_spawn_does() {
let (stdin, writer) = held_writer_stdin();
let mut cmd = tokio_command(Console::Hidden);
cmd.args(BLOCKER_ARGV.iter().copied()).env(SPAWN_TEST_ENV, "kept");
cmd.stdin(stdin).expect("stdin");
let mut child = cmd.spawn().await.expect("spawn");
drop(writer);
child.wait().await.expect("wait");
let sync = cmd.sync();
match sync.input() {
CommandInput::Argv(argv) => assert_eq!(argv.len(), BLOCKER_ARGV.len(), "the args are kept"),
other => panic!("the args are kept, got {other:?}"),
}
assert_eq!(sync.env_ops().len(), 1, "the env is kept");
assert!(sync.fds().is_empty(), "the spawn takes the fds");
}
#[skuld::test]
async fn a_failed_spawn_leaves_the_command_as_the_sync_spawn_does() {
let mut cmd = tokio_command(Console::Hidden);
cmd.executable("cosca-no-such-program-176").arg("kept");
cmd.spawn().await.expect_err("there is no such program");
assert!(matches!(cmd.sync().input(), CommandInput::Argv(argv) if argv.len() == 1));
}
#[skuld::test]
fn a_spawn_dropped_after_its_first_poll_runs_to_its_end_on_the_pool() {
let (tx, rx) = std::sync::mpsc::channel();
let _probe = fault::install_ran(tx);
let (stdin, _writer) = held_writer_stdin();
let rt = crate::tokio::test_runtime::single_thread_pool_runtime();
let (release, held) = std::sync::mpsc::channel::<()>();
let hold = rt.spawn_blocking(move || held.recv());
let mut cmd = tokio_command(Console::Hidden);
cmd.args(BLOCKER_ARGV.iter().copied()).env(SPAWN_TEST_ENV, "gone");
cmd.stdin(stdin).expect("stdin");
cmd.stdout(crate::Stdio::null()).expect("stdout");
rt.block_on(async {
let mut spawn = Box::pin(cmd.spawn());
std::future::poll_fn(|cx| {
assert!(
spawn.as_mut().poll(cx).is_pending(),
"the spawn is queued behind the held thread"
);
Poll::Ready(())
})
.await;
drop(spawn);
});
release.send(()).expect("release the held thread");
drop(hold);
drop(rt);
let sync = cmd.sync();
assert!(
matches!(sync.input(), CommandInput::Empty),
"the args went with the spawn"
);
assert!(sync.env_ops().is_empty(), "the env went with the spawn");
let child = rx
.try_iter()
.find(|at| at.point == SpawnPoint::BeforeIdentity)
.expect("the spawn ran to its end after its future was dropped");
let Resolved::Found(id) = child.id else {
panic!("the child was not identified: {:?}", child.id)
};
assert_eq!(
id.is_alive(),
Liveness::Dead,
"the dropped spawn's child is killed and reaped"
);
}
#[skuld::test]
fn a_spawn_dropped_after_its_task_finished_drops_the_child_off_the_runtime_thread() {
let (tx, rx) = std::sync::mpsc::channel();
let _probe = fault::install_ran(tx);
let (stdin, _writer) = held_writer_stdin();
let rt = crate::tokio::test_runtime::single_thread_pool_runtime();
let (release, held) = std::sync::mpsc::channel::<()>();
let hold = rt.spawn_blocking(move || held.recv());
let mut cmd = tokio_command(Console::Hidden);
cmd.args(BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("stdin");
let dropped_here = Rc::new(Cell::new(false));
rt.block_on(async {
let _teardown_here = graceful_hooks::at(HookPoint::AfterDropKill, {
let dropped_here = Rc::clone(&dropped_here);
move || dropped_here.set(true)
});
let (waker, finished) = crate::tokio::test_runtime::counting_waker();
let mut spawn = Box::pin(cmd.spawn());
assert!(
spawn.as_mut().poll(&mut Context::from_waker(&waker)).is_pending(),
"the spawn is queued behind the held thread"
);
release.send(()).expect("release the held thread");
finished.recv().expect("the spawn's task finished");
drop(spawn);
});
drop(hold);
let control_here = Rc::new(Cell::new(false));
rt.block_on(async {
let _teardown_here = graceful_hooks::at(HookPoint::AfterDropKill, {
let control_here = Rc::clone(&control_here);
move || control_here.set(true)
});
let (stdin, _writer) = held_writer_stdin();
let mut control = tokio_command(Console::Hidden);
control.args(BLOCKER_ARGV.iter().copied());
control.stdin(stdin).expect("stdin");
drop(control.spawn().await.expect("spawn the control child"));
});
drop(rt);
assert!(
control_here.get(),
"a child dropped on the armed thread is not seen by the hook"
);
assert!(
!dropped_here.get(),
"the finished spawn's child was dropped on the runtime thread"
);
let child = rx
.try_iter()
.find(|at| at.point == SpawnPoint::BeforeIdentity)
.expect("the spawn ran");
let Resolved::Found(id) = child.id else {
panic!("the child was not identified: {:?}", child.id)
};
assert_eq!(id.is_alive(), Liveness::Dead, "the dropped child is killed and reaped");
}