use super::*;
#[tokio::test(start_paused = true)]
async fn a_signal_reaches_the_sheeps_own_process_and_not_its_group() {
let dir = tempfile::tempdir().unwrap();
let (handle, runner, _events) = started(
&dir,
AppConfig::minimal("web", "./srv"),
vec![ProcScript::never_exits()],
)
.await;
let rows = handle
.signal(ProcessSelector::Id(0), OperatorSignal::Hup)
.await
.unwrap();
assert_eq!(
rows,
vec![SignalReply {
id: 0,
name: "web".to_string(),
outcome: SignalOutcome::Delivered,
}]
);
assert_eq!(runner.process_signals(0), vec![OperatorSignal::Hup]);
assert!(
runner.signals(0).is_empty(),
"shep signal must not reach the process group"
);
}
#[tokio::test(start_paused = true)]
async fn a_stopped_sheep_answers_not_running_rather_than_delivered() {
let dir = tempfile::tempdir().unwrap();
let (handle, _runner, _events) = started(
&dir,
AppConfig::minimal("web", "./srv"),
vec![ProcScript::never_exits()],
)
.await;
handle.stop(ProcessSelector::Id(0)).await.unwrap();
let rows = handle
.signal(ProcessSelector::Id(0), OperatorSignal::Hup)
.await
.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].outcome, SignalOutcome::NotRunning);
}
#[tokio::test(start_paused = true)]
async fn a_selector_that_matches_nothing_is_not_found() {
let h = harness(vec![]);
let err = h
.ctx
.supervisor
.signal(
ProcessSelector::Name("ghost".to_string()),
OperatorSignal::Hup,
)
.await
.unwrap_err();
assert_eq!(err, SupervisorError::NotFound);
}
#[tokio::test(start_paused = true)]
async fn a_reload_drainee_is_signalled_like_any_other_live_sheep() {
let dir = tempfile::tempdir().unwrap();
let (mut actor, mut signal_rx) = actor_with_a_drainee_holding_a_signal_mailbox(&dir);
let (reply, answer) = oneshot::channel();
actor.handle_command(Command::Signal {
selector: ProcessSelector::Id(0),
sig: OperatorSignal::Hup,
reply,
});
let request = tokio::time::timeout(ACTION_WINDOW, signal_rx.recv())
.await
.expect("no signal reached the drainee's mailbox within the window")
.expect("the drainee's signal mailbox closed");
assert_eq!(request.sig, OperatorSignal::Hup);
let _ = request.done.send(Ok(()));
let rows = tokio::time::timeout(ACTION_WINDOW, answer)
.await
.expect("the signal reported nothing within the window")
.expect("the signal's reply channel was dropped")
.unwrap();
assert_eq!(rows[0].outcome, SignalOutcome::Delivered);
}
#[tokio::test(start_paused = true)]
async fn a_line_reaches_a_sheep_that_asked_for_stdin() {
let dir = tempfile::tempdir().unwrap();
let mut app = AppConfig::minimal("repl", "./repl");
app.stdin = true;
let (handle, runner, _events) = started(&dir, app, vec![ProcScript::never_exits()]).await;
let rows = handle
.send_line(ProcessSelector::Id(0), "reload-config".to_string())
.await
.unwrap();
assert_eq!(
rows,
vec![LineReply {
id: 0,
name: "repl".to_string(),
outcome: LineOutcome::Sent,
}]
);
assert_eq!(runner.stdin_lines(0), vec!["reload-config".to_string()]);
}
#[tokio::test(start_paused = true)]
async fn a_sheep_without_stdin_answers_no_stdin() {
let dir = tempfile::tempdir().unwrap();
let (handle, _runner, _events) = started(
&dir,
AppConfig::minimal("web", "./srv"),
vec![ProcScript::never_exits()],
)
.await;
let rows = handle
.send_line(ProcessSelector::Id(0), "hello".to_string())
.await
.unwrap();
assert_eq!(rows[0].outcome, LineOutcome::NoStdin);
}
#[tokio::test(start_paused = true)]
async fn a_mixed_flock_reports_per_sheep_rather_than_failing() {
let dir = tempfile::tempdir().unwrap();
let mut piped = AppConfig::minimal("repl", "./repl");
piped.stdin = true;
let (handle, runner, _events) = started(
&dir,
piped,
vec![ProcScript::never_exits(), ProcScript::never_exits()],
)
.await;
handle
.start(vec![normalize(AppConfig::minimal("web", "./srv")).unwrap()])
.await
.unwrap();
let rows = handle
.send_line(ProcessSelector::All, "hello".to_string())
.await
.unwrap();
let outcome = |id| rows.iter().find(|r| r.id == id).unwrap().outcome.clone();
assert_eq!(outcome(0), LineOutcome::Sent);
assert_eq!(outcome(1), LineOutcome::NoStdin);
assert_eq!(runner.stdin_lines(1), Vec::<String>::new());
assert!(rows.windows(2).all(|w| w[0].id < w[1].id));
}
#[tokio::test(start_paused = true)]
async fn a_write_that_never_lands_times_out_and_says_so() {
let dir = tempfile::tempdir().unwrap();
let mut app = AppConfig::minimal("stuck", "./stuck");
app.stdin = true;
let (handle, _runner, _events) =
started(&dir, app, vec![ProcScript::never_reads_its_stdin()]).await;
let rows = tokio::time::timeout(
STDIN_WRITE_TIMEOUT * 4,
handle.send_line(ProcessSelector::Id(0), "hello".to_string()),
)
.await
.expect("send_line did not honour its own bound")
.unwrap();
let LineOutcome::NotWritten { reason } = rows[0].outcome.clone() else {
panic!("expected NotWritten, got {:?}", rows[0].outcome);
};
assert!(reason.contains("read"), "{reason}");
}
#[tokio::test(start_paused = true)]
async fn a_flock_of_wedged_sheep_is_bounded_once_and_not_once_each() {
let dir = tempfile::tempdir().unwrap();
let mut app = AppConfig::minimal("stuck", "./stuck");
app.stdin = true;
app.instances = 3;
let (handle, _runner, _events) =
started(&dir, app, vec![ProcScript::never_reads_its_stdin(); 3]).await;
let started_at = tokio::time::Instant::now();
let rows = handle
.send_line(ProcessSelector::All, "hello".to_string())
.await
.unwrap();
let elapsed = started_at.elapsed();
assert_eq!(rows.len(), 3);
assert!(
rows.iter()
.all(|row| matches!(row.outcome, LineOutcome::NotWritten { .. })),
"{rows:?}"
);
assert!(
elapsed < STDIN_WRITE_TIMEOUT * 2,
"three wedged sheep cost {elapsed:?}; the bound is per-CALL, not per-sheep"
);
}
#[tokio::test(start_paused = true)]
async fn a_selector_that_matches_nothing_is_not_found_for_send_line() {
let h = harness(vec![]);
assert_eq!(
h.ctx
.supervisor
.send_line(ProcessSelector::Name("ghost".to_string()), "x".to_string())
.await
.unwrap_err(),
SupervisorError::NotFound
);
}