use std::path::Path;
use shep_core::config::ResolvedApp;
use shep_core::paths::ShepPaths;
use super::*;
struct RearmFixture {
paths: ShepPaths,
handle: SupervisorHandle,
rig: Rig,
registry: ExtrasRegistry,
_paths_dir: tempfile::TempDir,
_fixture_dir: tempfile::TempDir,
_rx: broadcast::Receiver<SharedEvent>,
}
impl RearmFixture {
fn new() -> Self {
let paths_dir = tempfile::tempdir().unwrap();
let paths = test_paths(&paths_dir);
let (handle, _rx, _fixture_dir) = spawn_test_fixture();
Self {
paths,
handle,
rig: rig(DEFAULT_MAX_CRON_SLEEP),
registry: ExtrasRegistry::default(),
_paths_dir: paths_dir,
_fixture_dir,
_rx,
}
}
async fn start_and_arm(&mut self, app: &ResolvedApp) -> ProcessEntry {
self.handle.start(vec![app.clone()]).await.unwrap();
let entry = armed_entry(0, 0, 1000, app.clone(), &self.paths);
self.registry
.arm(&entry, idle_prober(), &self.rig.extras, &self.handle);
tokio::task::yield_now().await;
entry
}
}
impl Drop for RearmFixture {
fn drop(&mut self) {
std::mem::take(&mut self.registry.groups)
.into_values()
.for_each(NameExtras::abort);
}
}
fn cron_watch_app(root: &Path) -> ResolvedApp {
app_with("web", |app| {
app.cron_restart = Some("0 * * * *".to_string());
app.watch = true;
app.cwd = Some(root.display().to_string());
})
}
#[tokio::test(start_paused = true)]
async fn rearm_name_replaces_a_live_group_task() {
let root = tempfile::tempdir().unwrap();
let mut fixture = RearmFixture::new();
let app = cron_watch_app(root.path());
let entry = fixture.start_and_arm(&app).await;
let before_cron = fixture.registry.groups["web"]
.cron
.as_ref()
.unwrap()
.abort_handle();
let before_watch = fixture.registry.groups["web"]
.watch
.as_ref()
.unwrap()
.abort_handle();
fixture.registry.rearm_name(
"web",
&[&entry],
|_| idle_prober(),
&fixture.rig.extras,
&fixture.handle,
);
tokio::task::yield_now().await;
let after_cron = fixture.registry.groups["web"]
.cron
.as_ref()
.unwrap()
.abort_handle();
let after_watch = fixture.registry.groups["web"]
.watch
.as_ref()
.unwrap()
.abort_handle();
assert_ne!(
before_cron.id(),
after_cron.id(),
"the cron worker survived a rearm"
);
assert_ne!(
before_watch.id(),
after_watch.id(),
"the watch task survived a rearm"
);
}
#[tokio::test(start_paused = true)]
async fn rearm_name_leaves_the_group_armed() {
let root = tempfile::tempdir().unwrap();
let mut fixture = RearmFixture::new();
let app = cron_watch_app(root.path());
let entry = fixture.start_and_arm(&app).await;
fixture.registry.rearm_name(
"web",
&[&entry],
|_| idle_prober(),
&fixture.rig.extras,
&fixture.handle,
);
tokio::task::yield_now().await;
let group = &fixture.registry.groups["web"];
assert!(
group.cron.as_ref().is_some_and(|cron| !cron.is_finished()),
"the group must still have a live cron worker after a rearm"
);
assert!(
group
.watch
.as_ref()
.is_some_and(|watch| !watch.is_finished()),
"the group must still have a live watch task after a rearm"
);
}
#[tokio::test(start_paused = true)]
async fn rearm_name_leaves_another_apps_group_alone() {
let web_root = tempfile::tempdir().unwrap();
let worker_root = tempfile::tempdir().unwrap();
let mut fixture = RearmFixture::new();
let web = app_with("web", |app| {
app.watch = true;
app.cwd = Some(web_root.path().display().to_string());
});
let worker = app_with("worker", |app| {
app.watch = true;
app.cwd = Some(worker_root.path().display().to_string());
});
fixture
.handle
.start(vec![web.clone(), worker.clone()])
.await
.unwrap();
let web_entry = armed_entry(0, 0, 1000, web.clone(), &fixture.paths);
let worker_entry = armed_entry(1, 0, 1001, worker.clone(), &fixture.paths);
fixture.registry.arm(
&web_entry,
idle_prober(),
&fixture.rig.extras,
&fixture.handle,
);
fixture.registry.arm(
&worker_entry,
idle_prober(),
&fixture.rig.extras,
&fixture.handle,
);
tokio::task::yield_now().await;
let worker_watch_before = fixture.registry.groups["worker"]
.watch
.as_ref()
.unwrap()
.abort_handle();
fixture.registry.rearm_name(
"web",
&[&web_entry],
|_| idle_prober(),
&fixture.rig.extras,
&fixture.handle,
);
tokio::task::yield_now().await;
let worker_watch_after = fixture.registry.groups["worker"]
.watch
.as_ref()
.unwrap()
.abort_handle();
assert_eq!(
worker_watch_before.id(),
worker_watch_after.id(),
"rearming \"web\" must not touch \"worker\"'s group"
);
}
#[tokio::test(start_paused = true)]
async fn rearm_name_rebuilds_a_multi_instance_group_exactly_once() {
let root = tempfile::tempdir().unwrap();
let mut fixture = RearmFixture::new();
let app = app_with("web", |app| {
app.instances = 2;
app.cron_restart = Some("0 * * * *".to_string());
app.watch = true;
app.cwd = Some(root.path().display().to_string());
});
fixture.handle.start(vec![app.clone()]).await.unwrap();
let entry_a = armed_entry(0, 0, 1000, app.clone(), &fixture.paths);
let entry_b = armed_entry(1, 1, 1001, app.clone(), &fixture.paths);
fixture.registry.arm(
&entry_a,
idle_prober(),
&fixture.rig.extras,
&fixture.handle,
);
fixture.registry.arm(
&entry_b,
idle_prober(),
&fixture.rig.extras,
&fixture.handle,
);
tokio::task::yield_now().await;
let before_cron = fixture.registry.groups["web"]
.cron
.as_ref()
.unwrap()
.abort_handle();
let before_watch = fixture.registry.groups["web"]
.watch
.as_ref()
.unwrap()
.abort_handle();
let probed = std::sync::Mutex::new(Vec::new());
fixture.registry.rearm_name(
"web",
&[&entry_a, &entry_b],
|entry| {
probed
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.push(entry.id);
idle_prober()
},
&fixture.rig.extras,
&fixture.handle,
);
tokio::task::yield_now().await;
assert_eq!(
probed
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.as_slice(),
[0, 1],
"each instance must get its own prober, in id order"
);
let group = &fixture.registry.groups["web"];
assert_eq!(
group.members,
HashSet::from([0, 1]),
"both instances must still be members after a rearm, not just the last one arm'd"
);
assert!(
group.cron.is_some(),
"the group must hold exactly one cron task, not zero"
);
assert!(
group.watch.is_some(),
"the group must hold exactly one watch task, not zero"
);
let after_cron = group.cron.as_ref().unwrap().abort_handle();
let after_watch = group.watch.as_ref().unwrap().abort_handle();
assert_ne!(
before_cron.id(),
after_cron.id(),
"the cron worker must be rebuilt by the rearm, not left over"
);
assert_ne!(
before_watch.id(),
after_watch.id(),
"the watch task must be rebuilt by the rearm, not left over"
);
}