use super::*;
use tokio::sync::watch;
const ACTION_TIMEOUT: Duration = Duration::from_secs(20);
pub(super) const ACTION_WINDOW: Duration = Duration::from_secs(120);
pub(super) fn actor_with_an_open_channel(
dir: &tempfile::TempDir,
) -> (
Actor<ScriptedRunner>,
mpsc::Receiver<Msg>,
mpsc::Receiver<ShepherdMessage>,
) {
let (mut actor, mailbox) = actor_with_one_online_sheep(dir, vec![]);
let (to_child, child_rx) = mpsc::channel(16);
actor
.sheep
.get_mut(&0)
.expect("the fixture registers id 0")
.to_child = Some(to_child);
(actor, mailbox, child_rx)
}
pub(super) fn trigger_action(
actor: &mut Actor<ScriptedRunner>,
action: &str,
) -> oneshot::Receiver<ActionOutcome> {
let to_child = actor.sheep[&0]
.to_child
.clone()
.expect("the fixture's sheep holds the daemon's end of a channel");
actor.arm_action(0, to_child, action.to_string(), None, ACTION_TIMEOUT)
}
pub(super) async fn settle_action(
actor: &mut Actor<ScriptedRunner>,
mailbox: &mut mpsc::Receiver<Msg>,
) -> ActionOutcome {
let msg = tokio::time::timeout(ACTION_WINDOW, mailbox.recv())
.await
.expect("an action wait reported nothing within the window")
.expect("the actor's mailbox closed");
match msg {
Msg::ActionResult { id, stamp, outcome } => {
actor.handle_action_result(id, stamp, outcome.clone());
outcome
}
other => panic!("expected an action result, got {other:?}"),
}
}
pub(super) async fn sent_action(child_rx: &mut mpsc::Receiver<ShepherdMessage>) -> ShepherdMessage {
tokio::time::timeout(ACTION_WINDOW, child_rx.recv())
.await
.expect("nothing reached the child's end of the channel")
.expect("the child's end of the channel closed")
}
pub(super) struct SilentPumpRunner {
inner: ScriptedRunner,
seen: watch::Sender<u32>,
}
impl SilentPumpRunner {
pub(super) fn new(scripts: Vec<ProcScript>) -> (Self, watch::Receiver<u32>) {
let (seen, requests) = watch::channel(0);
(
Self {
inner: ScriptedRunner::new(scripts),
seen,
},
requests,
)
}
}
impl fmt::Debug for SilentPumpRunner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SilentPumpRunner").finish_non_exhaustive()
}
}
impl ProcessRunner for SilentPumpRunner {
type Proc = crate::fake::FakeProc;
fn spawn(&self, spec: &SpawnSpec) -> Result<(Self::Proc, ProcIo), crate::runner::RunnerError> {
let (proc, mut io) = self.inner.spawn(spec)?;
let (tx, mut rx) = mpsc::channel(SHEEP_CTL_CAPACITY);
io.log_ctl = tx;
let seen = self.seen.clone();
tokio::spawn(async move {
let mut held = Vec::new();
while let Some(request) = rx.recv().await {
held.push(request);
seen.send_modify(|count| *count += 1);
}
});
Ok((proc, io))
}
}
pub(super) const PUMP_REFUSAL: &str = "/gone/web-out.log: No such file or directory";
pub(super) const REFUSING_SHEEP: &str = "web";
#[derive(Debug)]
pub(super) struct FailingPumpRunner {
inner: Arc<ScriptedRunner>,
}
impl FailingPumpRunner {
pub(super) fn new(inner: Arc<ScriptedRunner>) -> Self {
Self { inner }
}
}
impl ProcessRunner for FailingPumpRunner {
type Proc = crate::fake::FakeProc;
fn spawn(&self, spec: &SpawnSpec) -> Result<(Self::Proc, ProcIo), crate::runner::RunnerError> {
let (proc, mut io) = self.inner.spawn(spec)?;
if spec.name != REFUSING_SHEEP {
return Ok((proc, io));
}
let (tx, mut rx) = mpsc::channel(SHEEP_CTL_CAPACITY);
io.log_ctl = tx;
tokio::spawn(async move {
while let Some(ctl) = rx.recv().await {
match ctl {
LogCtl::Reopen { done } => {
let _ = done.send(Err(ReopenError {
message: PUMP_REFUSAL.to_string(),
}));
}
LogCtl::Flush { done } => {
let _ = done.send(Err(FlushError {
message: PUMP_REFUSAL.to_string(),
}));
}
#[cfg(unix)]
LogCtl::ReportFds { done } => {
let _ = done.send(CarriedFds::none());
}
#[cfg(unix)]
LogCtl::Resume => {}
}
}
});
Ok((proc, io))
}
}