use std::future::Future as _;
use std::task::Poll;
use crate::identity::Liveness;
use crate::os_process::fake::ReapAnswer;
use crate::test_child::{held_blocker_async, Console};
#[skuld::test]
async fn the_async_spawn_hands_its_child_the_async_policy() {
use crate::child::owned::unreaped::DropKind;
use crate::child::spawn::policy::SpawnPolicy;
crate::tokio::test_runtime::assert_current_thread();
let _warn = super::fault::warn_slow_teardowns_after(std::time::Duration::ZERO);
let (child, _stdin) = held_blocker_async(crate::Stdio::null(), Console::Hidden, |_| {}).await;
assert_eq!(
child.inner.policy,
SpawnPolicy {
teardown_warn_after: Some(std::time::Duration::ZERO),
unreaped_drop: DropKind::LetGo,
}
);
}
#[skuld::test]
async fn a_teardown_future_dropped_at_its_wait_settles_as_a_drop_does() {
crate::tokio::test_runtime::assert_current_thread();
let (mut child, _stdin) = held_blocker_async(crate::Stdio::null(), Console::Hidden, |_| {}).await;
let id = child.id().expect(crate::test_child::IDENTITY_READ);
#[cfg(unix)]
let pid = child.pid();
child.inner.proc.script_over(|f| f.reaps([ReapAnswer::Running]));
let mut teardown = Box::pin(child.tear_down_awaiting());
let first = std::future::poll_fn(|cx| Poll::Ready(teardown.as_mut().poll(cx))).await;
assert!(first.is_pending(), "the teardown waits for the root");
drop(teardown);
assert_eq!(id.is_alive(), Liveness::Dead, "the dropped teardown ended the root");
#[cfg(unix)]
assert_eq!(
crate::child::front_kill_tests::reap(pid),
None,
"the dropped teardown reaped the root"
);
}
#[skuld::test]
async fn death_watch_wait_closes_the_untaken_stdin() {
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(crate::Stdio::pipe()).expect("stdin pipe");
let mut child = cmd.spawn().await.expect("spawn");
child.wait().await.expect("the child ends on the EOF wait made");
}
#[skuld::test]
async fn death_watch_wait_closes_an_untaken_stdin_another_slot_merges_into() {
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.fd(3, crate::Stdio::merge(crate::Fd::STDIN)).expect("merge fd 3");
let mut child = cmd.spawn().await.expect("spawn");
child.wait().await.expect("the child ends on the EOF wait made");
}
#[cfg(windows)]
#[skuld::test]
async fn death_watch_wait_closes_the_untaken_stdin_beside_an_fd_3_pipe() {
let mut cmd = crate::test_child::tokio_command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
cmd.fd(3, crate::Stdio::pipe_out()).expect("fd 3 pipe");
let mut child = cmd.spawn().await.expect("spawn");
child.wait().await.expect("the child ends on the EOF wait made");
}
#[skuld::test]
async fn a_teardown_future_dropped_while_panicking_gives_the_child_up_and_warns() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let (mut child, _stdin) = held_blocker_async(crate::Stdio::null(), Console::Hidden, |_| {}).await;
let pid = child.pid();
child.inner.proc.script_over(|f| f.reaps([ReapAnswer::Running]));
let mut teardown = Box::pin(child.tear_down_awaiting());
let first = std::future::poll_fn(|cx| Poll::Ready(teardown.as_mut().poll(cx))).await;
assert!(first.is_pending(), "the teardown waits for the root");
let mark = crate::log_capture::mark();
let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
let _teardown = teardown;
panic!("a sibling branch panicked");
}));
assert!(unwound.is_err());
let warns = crate::log_capture::records_since_on_current_thread(mark, "while the thread is panicking");
assert_eq!(warns.len(), 1, "one warn, got {warns:?}");
assert_eq!(warns[0].0, log::Level::Warn);
assert!(warns[0].1.contains(&format!("child {pid}")), "names the pid: {warns:?}");
}
#[cfg(target_os = "linux")]
mod leaf_drain {
use std::cell::Cell;
use std::rc::Rc;
use std::task::Poll;
use crate::containment::cgroup::fault as leaf_fault;
use crate::containment::cgroup::test_support::{entered_leaf_at, FakeLeaf};
use crate::test_child::{held_blocker_async, Console};
use crate::wait::block_probe::{on_each_block, Armed, WaitObject};
struct UndrainedLeaf {
fake: FakeLeaf,
blocked: Rc<Cell<bool>>,
_release: Armed,
}
impl UndrainedLeaf {
fn new(name: &str) -> UndrainedLeaf {
let fake = FakeLeaf::new(name, true);
let (path, events) = (fake.leaf.clone(), fake.events.clone());
leaf_fault::set_rmdir_hook(move |_| FakeLeaf::rmdir(&path, &events));
let blocked = Rc::new(Cell::new(false));
let _release = on_each_block({
let (blocked, events) = (Rc::clone(&blocked), fake.events.clone());
move |block| {
if matches!(block.on, WaitObject::Drain) {
blocked.set(true);
FakeLeaf::set_populated(&events, false);
}
}
});
UndrainedLeaf {
fake,
blocked,
_release,
}
}
#[track_caller]
fn assert_never_blocked(&self) {
assert!(
!self.blocked.get(),
"the teardown blocked its thread on the leaf's drain instead of awaiting it"
);
}
async fn spawn(&self) -> (crate::tokio::Child, crate::tokio::ChildStdin) {
self.spawn_reporting(None).await
}
async fn spawn_front(&self) -> (crate::tokio::Child, crate::tokio::ChildStdin) {
self.spawn_reporting(crate::child::front_kill_tests::sudo_report())
.await
}
async fn spawn_reporting(
&self,
elevation: Option<crate::elevation::ElevationReport>,
) -> (crate::tokio::Child, crate::tokio::ChildStdin) {
let (mut child, stdin) = held_blocker_async(crate::Stdio::null(), Console::Hidden, |cmd| {
cmd.set_elevation_report(elevation)
})
.await;
child.inner.tree = crate::containment::Tree::contained(crate::containment::Mechanism::Cgroup(
entered_leaf_at(self.fake.leaf.clone()),
));
(child, stdin)
}
fn drain(&self) {
FakeLeaf::set_populated(&self.fake.events, false);
}
}
impl Drop for UndrainedLeaf {
fn drop(&mut self) {
leaf_fault::take_rmdir_hook();
}
}
async fn poll_once<F: std::future::Future + Unpin>(future: &mut F) -> Poll<F::Output> {
std::future::poll_fn(|cx| Poll::Ready(std::pin::Pin::new(&mut *future).poll(cx))).await
}
#[skuld::test]
async fn death_watch_a_teardown_of_a_reaped_root_awaits_the_leafs_drain() {
crate::tokio::test_runtime::assert_current_thread();
let leaf = UndrainedLeaf::new("cosca-awaited-reaped-root");
let (mut child, stdin) = leaf.spawn().await;
drop(stdin);
child.wait().await.expect("the blocker ends at EOF");
let mut teardown = Box::pin(child.tear_down_awaiting());
let first = poll_once(&mut teardown).await;
leaf.assert_never_blocked();
assert!(first.is_pending(), "the leaf has not drained, so the teardown waits");
leaf.drain();
let torn = teardown.await;
assert_eq!(torn.fate, crate::error::ChildFate::Reaped);
assert!(torn.unreaped.is_none());
assert!(!leaf.fake.leaf.exists(), "the drained leaf is removed");
}
#[skuld::test]
async fn a_teardown_whose_leafs_drain_cannot_be_watched_leaves_it_to_the_drop() {
crate::tokio::test_runtime::assert_current_thread();
crate::log_capture::install();
let leaf = UndrainedLeaf::new("cosca-awaited-unwatchable");
leaf_fault::set_rmdir_hook(|_| Err(std::io::Error::from_raw_os_error(libc::EBUSY)));
let (mut child, stdin) = leaf.spawn().await;
drop(stdin);
child.wait().await.expect("the blocker ends at EOF");
std::fs::write(&leaf.fake.events, "garbled\n").expect("garble cgroup.events");
let mark = crate::log_capture::mark();
let torn = child.tear_down_awaiting().await;
assert_eq!(torn.fate, crate::error::ChildFate::Reaped);
assert!(torn.unreaped.is_none());
let said = crate::log_capture::records_since(mark, "its leaf's drain cannot be watched");
assert_eq!(said.len(), 1, "{said:?}");
let warned = crate::log_capture::records_since(mark, "its drain could not be watched");
assert_eq!(warned.len(), 1, "the leaf's drop reports it left behind: {warned:?}");
leaf.assert_never_blocked();
}
#[skuld::test]
async fn death_watch_a_teardown_of_a_front_behind_a_closed_gate_awaits_the_leafs_drain() {
crate::tokio::test_runtime::assert_current_thread();
let leaf = UndrainedLeaf::new("cosca-awaited-closed-gate");
let (child, _stdin) = leaf.spawn_front().await;
let mut teardown = Box::pin(child.tear_down_awaiting());
let first = poll_once(&mut teardown).await;
leaf.assert_never_blocked();
assert!(first.is_pending(), "the leaf has not drained, so the teardown waits");
leaf.drain();
let torn = teardown.await;
assert!(!leaf.fake.leaf.exists(), "the drained leaf is removed");
assert!(
matches!(torn.fate, crate::error::ChildFate::Running { .. }),
"got {:?}",
torn.fate
);
assert!(torn.unreaped.is_some(), "a front left running is the caller's");
}
#[skuld::test]
async fn a_teardown_of_a_front_behind_a_closed_gate_removes_an_empty_leaf_and_writes_no_kill() {
crate::tokio::test_runtime::assert_current_thread();
let leaf = UndrainedLeaf::new("cosca-awaited-empty-leaf");
leaf.drain();
let (child, _stdin) = leaf.spawn_front().await;
let _torn = child.tear_down_awaiting().await;
assert!(!leaf.fake.leaf.exists(), "the empty leaf is removed");
assert!(leaf.fake.kill_written().is_empty(), "an empty leaf is written no kill");
leaf.assert_never_blocked();
}
#[skuld::test]
async fn a_teardown_of_a_front_behind_a_closed_gate_removes_its_leaf_once() {
crate::tokio::test_runtime::assert_current_thread();
let leaf = UndrainedLeaf::new("cosca-awaited-removed-once");
leaf.drain();
let (child, _stdin) = leaf.spawn_front().await;
leaf_fault::record_leaf_steps();
let _torn = child.tear_down_awaiting().await;
let rmdirs = leaf_fault::take_leaf_steps()
.iter()
.filter(|step| step.starts_with("rmdir"))
.count();
assert_eq!(rmdirs, 1, "the leaf is removed once");
leaf.assert_never_blocked();
}
}