Watch
0
0
Fork
You've already forked hyperhive
0

hive-agent: test the vacuum sweep

This commit is contained in:
damocles 2026-11-16 20:35:38 +01:00 • committed by mara
commit 8a8788cd35

View file

@ -172,7 +172,183 @@ fn remove_old_event_log(harness: &Path) {
#[cfg(test)]
mod tests {
use super::remove_old_event_log;
use super::*;
fn write_task(dir: &Path, stem: &str, json: &str) {
std::fs::write(dir.join(format!("{stem}.json")), json).unwrap();
std::fs::write(dir.join(format!("{stem}.out")), b"out").unwrap();
std::fs::write(dir.join(format!("{stem}.err")), b"err").unwrap();
}
fn present(dir: &Path, stem: &str) -> [bool; 3] {
["json", "out", "err"].map(|e| dir.join(format!("{stem}.{e}")).exists())
}
const CUTOFF: i64 = 1_800_000_000;
const OLD: &str = "2026-01-01T00:00:00Z";
const NEW: &str = "2999-01-01T00:00:00Z";
/// A finished task past the cutoff loses all three of its files, and is
/// counted once; everything that is not a clearly expired finished task
/// stays, including the files it cannot read.
#[test]
fn only_expired_finished_tasks_lose_their_files() {
let dir = tempfile::tempdir().unwrap();
let d = dir.path();
for (stem, status) in [
("old-done", "done"),
("old-timed", "timed_out"),
("old-int", "interrupted"),
] {
write_task(
d,
stem,
&format!(r#"{{"status":"{status}","completed_at":"{OLD}"}}"#),
);
}
write_task(
d,
"recent",
&format!(r#"{{"status":"done","completed_at":"{NEW}"}}"#),
);
write_task(
d,
"running",
&format!(r#"{{"status":"running","completed_at":"{OLD}"}}"#),
);
write_task(
d,
"killed",
&format!(r#"{{"status":"killed","completed_at":"{OLD}"}}"#),
);
write_task(d, "no-time", r#"{"status":"done"}"#);
write_task(
d,
"bad-time",
r#"{"status":"done","completed_at":"yesterday"}"#,
);
write_task(d, "epoch-int", r#"{"status":"done","completed_at":1}"#);
write_task(d, "corrupt", "not json");
std::fs::write(d.join("notes.txt"), b"x").unwrap();
assert_eq!(vacuum_bash_tasks(d, CUTOFF), 3);
for gone in ["old-done", "old-timed", "old-int"] {
assert_eq!(present(d, gone), [false; 3], "{gone}");
}
for kept in [
"recent",
"running",
"killed",
"no-time",
"bad-time",
"epoch-int",
"corrupt",
] {
assert_eq!(present(d, kept), [true; 3], "{kept}");
}
assert!(d.join("notes.txt").exists());
}
/// A trio missing its `.out` or `.err` still goes, and a missing directory
/// is an empty sweep, not an error.
#[test]
fn a_partial_trio_goes_and_a_missing_dir_sweeps_nothing() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("half.json"),
format!(r#"{{"status":"done","completed_at":"{OLD}"}}"#),
)
.unwrap();
assert_eq!(vacuum_bash_tasks(dir.path(), CUTOFF), 1);
assert_eq!(present(dir.path(), "half"), [false; 3]);
assert_eq!(vacuum_bash_tasks(&dir.path().join("absent"), CUTOFF), 0);
}
/// Delivered reminders and acked todos past their keep time are reaped;
/// pending reminders and open todos never are.
#[test]
fn the_stores_lose_only_old_delivered_and_acked_rows() {
let dir = tempfile::tempdir().unwrap();
let db = dir.path().join("state.sqlite");
let reminders = crate::reminders::Reminders::open(&db).unwrap();
let old_sent = reminders.store("old", None, 0).unwrap();
let new_sent = reminders.store("new", None, 0).unwrap();
let pending = reminders.store("pending", None, i64::MAX / 2).unwrap();
reminders.mark_delivered(old_sent).unwrap();
reminders.mark_delivered(new_sent).unwrap();
let todos = crate::todos::Todos::open(&db).unwrap();
let (old_acked, _) = todos.upsert("a", Some("1"), "old", None, false).unwrap();
let (new_acked, _) = todos.upsert("a", Some("2"), "new", None, false).unwrap();
todos.upsert("a", Some("3"), "open", None, false).unwrap();
todos.mark_done(old_acked).unwrap();
todos.mark_done(new_acked).unwrap();
let conn = rusqlite::Connection::open(&db).unwrap();
conn.execute("UPDATE reminders SET sent_at = 1 WHERE id = ?1", [old_sent])
.unwrap();
conn.execute("UPDATE todos SET acked_at = 1 WHERE id = ?1", [old_acked])
.unwrap();
assert_eq!(vacuum_reminders(&db).unwrap(), 1);
assert_eq!(vacuum_todos(&db).unwrap(), 1);
assert_eq!(reminders.count_pending().unwrap(), 1);
assert_eq!(todos.open_count().unwrap(), 1);
let left: i64 = conn
.query_row("SELECT COUNT(*) FROM reminders", [], |r| r.get(0))
.unwrap();
assert_eq!(left, 2, "the pending and the freshly delivered stay");
let left: i64 = conn
.query_row("SELECT COUNT(*) FROM todos", [], |r| r.get(0))
.unwrap();
assert_eq!(left, 2, "the open and the freshly acked stay");
let _ = pending;
}
/// One sweep does all of it under the harness dir: the bash tasks, the old
/// event log and the state db's rows.
#[test]
fn a_sweep_prunes_the_harness_dir() {
let _dir = crate::paths::harness_dir_guard();
let harness = crate::paths::harness_dir();
let tasks = harness.join("bash-tasks");
std::fs::create_dir_all(&tasks).unwrap();
write_task(
&tasks,
"old",
&format!(r#"{{"status":"done","completed_at":"{OLD}"}}"#),
);
write_task(&tasks, "live", r#"{"status":"running"}"#);
std::fs::write(harness.join("hyperhive-events.sqlite"), b"x").unwrap();
let reminders = crate::reminders::Reminders::open(&crate::paths::state_db()).unwrap();
let id = reminders.store("x", None, 0).unwrap();
reminders.mark_delivered(id).unwrap();
rusqlite::Connection::open(crate::paths::state_db())
.unwrap()
.execute("UPDATE reminders SET sent_at = 1", [])
.unwrap();
sweep_once();
assert_eq!(present(&tasks, "old"), [false; 3]);
assert_eq!(present(&tasks, "live"), [true; 3]);
assert!(!harness.join("hyperhive-events.sqlite").exists());
assert_eq!(reminders.prune_delivered_older_than(i64::MAX).unwrap(), 0);
}
/// A sweep over a harness dir with nothing in it does nothing and does not
/// create anything.
#[test]
fn a_sweep_of_an_empty_harness_dir_is_a_no_op() {
let _dir = crate::paths::harness_dir_guard();
sweep_once();
assert_eq!(
std::fs::read_dir(crate::paths::harness_dir())
.unwrap()
.count(),
0
);
}
#[test]
fn the_old_event_log_and_its_wal_files_are_removed_and_nothing_else() {