mod dogs;
mod readiness;
use crate::boot::*;
use crate::fake::{ProcScript, ScriptedRunner};
use crate::testing::test_paths;
use shep_core::config::{AppConfig, ProbeConfig, ProbeKind, normalize};
use shep_core::protocol::{BusEvent, ProcessEventKind};
use shep_core::values::UpDuration;
use std::time::Duration;
#[test]
fn an_unset_max_cron_sleep_falls_back_to_the_daemons_own_default() {
assert_eq!(
max_cron_sleep(&BootOptions::default()),
DEFAULT_MAX_CRON_SLEEP,
"unset means the default"
);
assert_eq!(
max_cron_sleep(&BootOptions {
max_cron_sleep: Some(Duration::from_secs(300)),
..BootOptions::default()
}),
Duration::from_secs(300),
"a configured value must reach the workers unchanged"
);
}
#[tokio::test]
async fn a_booted_daemon_restarts_a_sheep_whose_liveness_probe_fails() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
let reserved = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = reserved.local_addr().unwrap();
drop(reserved);
let daemon = boot(
ScriptedRunner::new(vec![ProcScript::never_exits(); 4]),
paths.clone(),
BootOptions::default(),
)
.await
.unwrap();
let ctx = daemon.context();
let mut events = ctx.events.subscribe();
let run = tokio::spawn(daemon.run());
let mut app = AppConfig::minimal("web", "./srv");
app.liveness_probe = Some(ProbeConfig {
kind: ProbeKind::Tcp,
target: addr.to_string(),
interval: UpDuration::from_millis(1_000),
timeout: UpDuration::from_millis(500),
failure_threshold: 1,
});
ctx.supervisor
.start(vec![normalize(app).unwrap()])
.await
.unwrap();
let restarted = async {
loop {
match events.recv().await.map(|event| event.to_event()) {
Ok(BusEvent::Process {
event: ProcessEventKind::Restart,
info,
..
}) => return info,
Ok(_) => continue,
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
Err(err) => panic!("the event stream closed before a restart: {err}"),
}
}
};
let info = tokio::time::timeout(Duration::from_secs(20), restarted)
.await
.expect("a failing liveness probe must restart its sheep");
assert_eq!(info.id, 0);
assert_eq!(info.restarts, 1);
ctx.shutdown();
tokio::time::timeout(Duration::from_secs(10), run)
.await
.unwrap()
.unwrap()
.unwrap();
}