use std::collections::BTreeMap;
use crate::child::owned::OwnedChild;
use crate::child::spawn::policy::SpawnPolicy;
use crate::containment::fake::FakeContains;
use crate::containment::{Attachment, Containment, Mechanism, Tree};
use crate::graceful::GracefulMechanism;
use crate::identity::{Key, ProcessId};
use crate::os_process::child::ExitRecord;
use crate::os_process::fake::{Answer, FakeProcess, ReapAnswer};
use crate::signal::Sent;
use crate::{Child, Unreaped};
const ABSENT_PID: u32 = 0x7fff_fffe;
pub(crate) fn child_over(fake: &FakeProcess) -> Child {
Child::from_parts(
OwnedChild::new(Box::new(fake.clone().with_pid(ABSENT_PID))),
Some(ProcessId::from_parts_for_test(ABSENT_PID, Key::for_test(1))),
BTreeMap::new(),
false,
Attachment {
tree: Tree::none(),
graceful: if cfg!(windows) {
GracefulMechanism::None
} else {
GracefulMechanism::Process
},
},
None,
SpawnPolicy::SYNC,
)
}
pub(crate) fn root_looking(exits: impl IntoIterator<Item = Answer>) -> FakeProcess {
let root = FakeProcess::new(exits).signals([Ok(Sent::Delivered)]);
#[cfg(target_os = "macos")]
let root = root.peeks_always(|| Ok(crate::os_process::child::PeekOutcome::Running));
root
}
pub(crate) fn child_reaching(root: &FakeProcess, members: &[ProcessId]) -> Child {
let (fake, _) = FakeContains::new(Containment::Session, false);
child_with(root, fake.reaching(members.iter().copied()))
}
pub(crate) fn child_with(root: &FakeProcess, fake: FakeContains) -> Child {
let mut child = child_over(root);
child.tree = Tree::contained(Mechanism::Fake(fake));
child
}
#[cfg(unix)]
pub(crate) fn holders_only_skip(child: &Child) -> crate::containment::Skip {
use crate::containment::{Refusal, Why};
crate::containment::Skip {
action: "signal its process group".to_owned(),
why: Why::MayNameAnother(Refusal::Unpinned(child.pid())),
root_alone: false,
holders: Some(Ok(())),
}
}
#[cfg(all(feature = "tokio", unix))]
pub(crate) fn tokio_child_with(root: &FakeProcess, fake: FakeContains) -> crate::tokio::Child {
crate::tokio::Child {
inner: child_with(root, fake),
}
}
#[cfg(feature = "tokio")]
pub(crate) fn tokio_child_reaching(root: &FakeProcess, members: &[ProcessId]) -> crate::tokio::Child {
crate::tokio::Child {
inner: child_reaching(root, members),
}
}
pub(crate) fn live_member() -> (Child, crate::PipeWriter, ProcessId) {
let (member, writer) = super::held_uncontained_blocker(false);
let id = member.id().expect(super::IDENTITY_READ);
(member, writer, id)
}
pub(crate) fn unreaped_over(fake: &FakeProcess, id: Option<ProcessId>) -> Unreaped {
Unreaped::new(
OwnedChild::new(Box::new(fake.clone())),
id,
false,
SpawnPolicy::SYNC.unreaped_drop,
)
}
pub(crate) fn exited_unreaped(id: ProcessId) -> Box<Unreaped> {
let exit = ExitRecord::Status(std::process::ExitStatus::default());
let fake = FakeProcess::new([])
.with_pid(id.pid())
.reaps([ReapAnswer::Reaped(exit)]);
Box::new(unreaped_over(&fake, Some(id)))
}