Lines
100 %
Functions
Branches
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};
use super::{GRACE, hold, is_cluster_name, is_stale, sweep_stale_clusters};
fn cluster(base: &Path, name: &str) -> anyhow::Result<PathBuf> {
let root = base.join(name);
std::fs::create_dir_all(root.join("pgdata"))?;
Ok(root)
}
fn age(root: &Path) -> anyhow::Result<()> {
std::fs::File::open(root)?.set_modified(SystemTime::now() - GRACE * 2)?;
Ok(())
#[test]
fn only_the_names_xtask_creates_match() {
assert!(is_cluster_name("nomisync-ci-pgAb3dE9"));
assert!(!is_cluster_name("nomisync-ci-pg"));
assert!(!is_cluster_name("nomisync-ci-pgAb3dE9x"));
assert!(!is_cluster_name("nomisync-ci-pg-Ab3dE"));
assert!(!is_cluster_name("other-ci-pgAb3dE9"));
assert!(!is_cluster_name("my-nomisync-ci-pgAb3dE9"));
fn a_cluster_of_another_user_is_never_stale() {
assert!(!is_stale(1, 1000, GRACE * 2, || true));
fn a_young_or_locked_cluster_is_not_stale() {
assert!(!is_stale(1000, 1000, Duration::ZERO, || true));
assert!(!is_stale(1000, 1000, GRACE * 2, || false));
assert!(is_stale(1000, 1000, GRACE, || true));
fn a_held_lock_keeps_an_old_cluster_even_with_a_dead_postmaster_pid() -> anyhow::Result<()> {
let base = tempfile::tempdir()?;
let root = cluster(base.path(), "nomisync-ci-pgHeld01")?;
std::fs::write(root.join("pgdata/postmaster.pid"), "2147483646\n/data\n")?;
let _lock = hold(&root)?;
age(&root)?;
assert!(sweep_stale_clusters(base.path()).is_empty());
assert!(root.exists());
fn a_released_lock_and_an_old_mtime_remove_the_cluster() -> anyhow::Result<()> {
let root = cluster(base.path(), "nomisync-ci-pgGone01")?;
drop(hold(&root)?);
assert_eq!(sweep_stale_clusters(base.path()), vec![root.clone()]);
assert!(!root.exists());
fn a_fresh_cluster_is_kept_even_unlocked() -> anyhow::Result<()> {
let root = cluster(base.path(), "nomisync-ci-pgFresh1")?;
fn without_a_lock_file_only_an_old_cluster_goes() -> anyhow::Result<()> {
let fresh = cluster(base.path(), "nomisync-ci-pgFresh2")?;
let old = cluster(base.path(), "nomisync-ci-pgOld001")?;
age(&old)?;
assert_eq!(sweep_stale_clusters(base.path()), vec![old.clone()]);
assert!(fresh.exists());
assert!(!old.exists());
fn a_foreign_name_is_kept_even_when_abandoned() -> anyhow::Result<()> {
let foreign = cluster(base.path(), "someone-else")?;
let longer = cluster(base.path(), "nomisync-ci-pgDead012")?;
age(&foreign)?;
age(&longer)?;
assert!(foreign.exists());
assert!(longer.exists());
fn a_missing_base_sweeps_nothing() {
assert!(sweep_stale_clusters(Path::new("/nonexistent/nomisync-sweep")).is_empty());