pointlock-store 0.1.8

Pointlock's event-sourced RunLog, SQLite/WAL checkpoints, evidence store, and read-side projections.
Documentation
//! Writer-lease tests (07 ยง3.3 rule 5): mutual exclusion between two
//! handles, the probe's hygiene, and the explicit release on drop.

use pointlock_store::{StoreError, WriterLease};

fn temp_root(tag: &str) -> std::path::PathBuf {
    let path = std::env::temp_dir().join(format!("pointlock-lease-{tag}-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&path);
    path
}

#[test]
fn two_simultaneous_writers_exactly_one_acquires() {
    let root = temp_root("two-writers");
    let first = WriterLease::acquire(&root, "run-1").expect("first writer acquires");
    assert_eq!(first.run_id(), "run-1");
    // flock is per open-file description: a second open in the same
    // process conflicts exactly like a second process would.
    match WriterLease::acquire(&root, "run-1") {
        Err(StoreError::WriterBusy { run_id }) => assert_eq!(run_id, "run-1"),
        other => panic!("second writer must be WriterBusy, got {other:?}"),
    }
    // Another run is an independent lease.
    let _other = WriterLease::acquire(&root, "run-2").expect("other run is free");
    drop(first);
    let _again = WriterLease::acquire(&root, "run-1").expect("released on drop");
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn the_probe_never_leaves_a_lock_behind() {
    let root = temp_root("probe");
    assert!(!WriterLease::is_held(&root, "run"), "fresh run is free");
    assert!(
        !WriterLease::is_held(&root, "run"),
        "probe did not pin the lock"
    );
    assert!(
        !root.join("locks").exists(),
        "a probe on a never-leased run creates neither locks/ nor a lock file"
    );
    let held = WriterLease::acquire(&root, "run").expect("acquire after probes");
    assert!(WriterLease::is_held(&root, "run"), "probe sees the holder");
    assert!(
        WriterLease::is_held(&root, "run"),
        "probe did not steal the lease"
    );
    drop(held);
    assert!(
        !WriterLease::is_held(&root, "run"),
        "drop released the lease"
    );
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn exotic_run_ids_get_a_path_safe_lock_file() {
    let root = temp_root("exotic");
    let lease = WriterLease::acquire(&root, "a/b:c").expect("acquire");
    let path = pointlock_store::lease::lock_path(&root, "a/b:c");
    assert!(path.ends_with("locks/a_b_c.lock"), "{}", path.display());
    assert!(path.exists());
    drop(lease);
    let _ = std::fs::remove_dir_all(&root);
}