use super::*;
use std::collections::BTreeMap;
#[tokio::test(start_paused = true)]
async fn only_describe_carries_a_lamb_tree() {
let h = harness_identifying(
vec![ProcScript::never_exits()],
vec![
identity(FIRST_SCRIPTED_PID, None, "srv"),
identity(FIRST_SCRIPTED_PID + 1, Some(FIRST_SCRIPTED_PID), "node"),
],
);
start_web(&h).await;
let rows = list_flock(&h.ctx, 2).await;
assert!(
rows.iter().all(|row| row.lambs.is_none()),
"ListFlock must not walk the process table"
);
let described = describe(&h.ctx, 3, SelectorSpec::Name("web".to_string())).await;
assert_eq!(
described[0].lambs,
Some(vec![Lamb::new(FIRST_SCRIPTED_PID + 1, "node")])
);
}
async fn start_dog(ctx: &RpcContext, name: &str) -> ProcessInfo {
let spec = DogSpec {
name: name.to_string(),
source: DogSource::BuiltIn { grants: None },
env: BTreeMap::new(),
};
let app = crate::dogs::dog_app(&spec, &ctx.paths).expect("the dog fixture must assemble");
ctx.supervisor
.start_dog(app, DogSource::BuiltIn { grants: None })
.await
.expect("the dog fixture must start")
}
fn find_dog<'a>(infos: &'a [ProcessInfo], name: &str) -> &'a ProcessInfo {
infos
.iter()
.find(|info| info.name == name)
.unwrap_or_else(|| panic!("{name} is missing from the listing: {infos:?}"))
}
async fn staleness(ctx: &RpcContext) -> (Vec<String>, Vec<String>) {
let reply = reply_of(dispatch(envelope(1, Request::DogStaleness), ctx).await);
let Ok(Response::DogStaleness { stale, pending }) = reply.result else {
panic!("expected a dog staleness answer");
};
(stale, pending)
}
#[tokio::test]
async fn a_flock_of_ordinary_sheep_has_nothing_stale_and_nothing_pending() {
let h = harness(vec![ProcScript::never_exits()]);
start_web(&h).await;
assert_eq!(staleness(&h.ctx).await, (Vec::new(), Vec::new()));
}
#[tokio::test]
async fn a_listing_says_which_dogs_have_answered_this_shepherd() {
let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
start_dog(&h.ctx, "metrics").await;
let silent = list_flock(&h.ctx, 1).await;
let dog = find_dog(&silent, "metrics");
assert_eq!(dog.handshook, Some(false));
assert_eq!(
dog.status,
ProcStatus::Online,
"the process is up, and the listing still says so"
);
h.ctx.dog_refusals.handshook("metrics");
let talking = list_flock(&h.ctx, 2).await;
assert_eq!(find_dog(&talking, "metrics").handshook, Some(true));
}
#[tokio::test]
async fn a_sheep_carries_no_handshake_fact_at_all() {
let h = harness(vec![ProcScript::never_exits()]);
start_web(&h).await;
let infos = list_flock(&h.ctx, 1).await;
assert_eq!(infos[0].name, "web");
assert_eq!(infos[0].handshook, None);
assert_eq!(
infos[0].dog_stale, None,
"a sheep is never given up on, because it was never asked to answer"
);
}
#[tokio::test]
async fn a_listing_says_which_silent_dogs_this_shepherd_gave_up_on() {
let h = harness(vec![ProcScript::never_exits()]);
start_dog(&h.ctx, "metrics").await;
let waiting = list_flock(&h.ctx, 1).await;
let dog = find_dog(&waiting, "metrics");
assert_eq!(dog.handshook, Some(false));
assert_eq!(
dog.dog_stale,
Some(false),
"a dog that has not answered YET is not one this shepherd gave up on"
);
h.ctx.dog_refusals.refused("metrics");
h.ctx.dog_refusals.refused("metrics");
let given_up = list_flock(&h.ctx, 2).await;
let dog = find_dog(&given_up, "metrics");
assert_eq!(dog.dog_stale, Some(true));
assert_eq!(
dog.status,
ProcStatus::Online,
"the process is still up, and the listing still says so"
);
h.ctx.dog_refusals.handshook("metrics");
let talking = list_flock(&h.ctx, 3).await;
let dog = find_dog(&talking, "metrics");
assert_eq!(dog.handshook, Some(true));
assert_eq!(dog.dog_stale, Some(false));
}
#[tokio::test]
async fn describe_carries_the_handshake_fact_too() {
let h = harness(vec![ProcScript::never_exits()]);
start_dog(&h.ctx, "metrics").await;
let rows = describe(&h.ctx, 1, SelectorSpec::Name("metrics".to_string())).await;
assert_eq!(rows[0].handshook, Some(false));
}
#[tokio::test]
async fn a_dog_that_has_not_handshaken_is_pending() {
let h = harness(vec![ProcScript::never_exits()]);
start_dog(&h.ctx, "metrics").await;
assert_eq!(
staleness(&h.ctx).await,
(Vec::new(), vec!["metrics".to_string()])
);
h.ctx.dog_refusals.handshook("metrics");
assert_eq!(
staleness(&h.ctx).await,
(Vec::new(), Vec::new()),
"a dog talking to this shepherd is settled and is not worth reporting"
);
}
#[tokio::test]
async fn a_dog_being_restarted_is_pending_and_then_stale() {
let h = harness(vec![ProcScript::never_exits()]);
start_dog(&h.ctx, "metrics").await;
h.ctx.dog_refusals.handshook("metrics");
h.ctx.dog_refusals.refused("metrics");
assert_eq!(
staleness(&h.ctx).await,
(Vec::new(), vec!["metrics".to_string()]),
"one refusal buys a restart; it does not condemn the dog"
);
h.ctx.dog_refusals.refused("metrics");
assert_eq!(
staleness(&h.ctx).await,
(vec!["metrics".to_string()], Vec::new()),
"a stale dog is a finding, not something still to wait on"
);
}
#[tokio::test]
async fn a_dog_that_has_stopped_running_is_not_waited_on() {
let h = harness(vec![ProcScript::never_exits()]);
start_dog(&h.ctx, "metrics").await;
assert_eq!(staleness(&h.ctx).await.1, vec!["metrics".to_string()]);
h.ctx
.supervisor
.stop(ProcessSelector::Name("metrics".to_string()))
.await
.expect("the dog must stop");
assert_eq!(
staleness(&h.ctx).await,
(Vec::new(), Vec::new()),
"a dog that is not running has nothing to answer with"
);
}