Cotton code
106 lines · 4.5 KB · RustRaw
use cotton_store::process_lock::{Holder, ProcessLock, LOCK_FILE};use std::io::{Read, Write};fn holder(bundle: &str) -> Holder {    Holder {        pid: std::process::id(),        bundle_path: bundle.into(),        started_at: "2026-10-18T01:00:00Z".into(),    }}/// RULE: Two apps must not open one folder: 75 and 151 held one Mac's data together for three minutes on 18 Oct 2026; a refusal must name the actual holder's bundle.#[test]fn a_second_open_refuses_and_names_the_holder() {    let root = tempfile::tempdir().unwrap();    let first = ProcessLock::acquire(root.path(), holder("/synthetic/Recovery.app")).unwrap();    let refused = ProcessLock::acquire(root.path(), holder("/synthetic/New.app")).unwrap_err();    assert!(refused.to_string().contains("/synthetic/Recovery.app"));    assert_eq!(first.holder().pid, std::process::id());    assert!(root.path().join(LOCK_FILE).is_file());}/// RULE: A dead pid in an unlocked file cannot strand a folder after a crash; the kernel lock, not metadata, prevents another 75/151 overlap (18 Oct 2026).#[test]fn a_stale_lock_is_taken_over_without_refusal() {    let root = tempfile::tempdir().unwrap();    std::fs::write(        root.path().join(LOCK_FILE),        r#"{"pid":4294967295,"bundle_path":"/synthetic/Dead.app","started_at":"old"}"#,    )    .unwrap();    let lock = ProcessLock::acquire(root.path(), holder("/synthetic/Current.app")).unwrap();    assert_eq!(lock.holder().bundle_path, "/synthetic/Current.app");    let on_disk: Holder =        serde_json::from_slice(&std::fs::read(root.path().join(LOCK_FILE)).unwrap()).unwrap();    assert_eq!(on_disk, *lock.holder());}#[test]fn child_holds_folder() {    let Some(folder) = std::env::var_os("COTTON_SYNTHETIC_LOCK_CHILD") else {        return;    };    let _lock = ProcessLock::acquire(        std::path::Path::new(&folder),        holder("/synthetic/Child.app"),    )    .unwrap();    println!("LOCKED");    std::io::stdout().flush().unwrap();    let mut byte = [0];    let _ = std::io::stdin().read(&mut byte);}/// RULE: The exclusion is between processes, not merely Rust handles; the 18 Oct 2026 collision on one Mac crossed two running bundles.#[test]fn another_process_cannot_open_until_the_holder_exits() {    use std::io::BufRead;    let root = tempfile::tempdir().unwrap();    let mut child = std::process::Command::new(std::env::current_exe().unwrap())        .args(["--exact", "child_holds_folder", "--nocapture"])        .env("COTTON_SYNTHETIC_LOCK_CHILD", root.path())        .stdin(std::process::Stdio::piped())        .stdout(std::process::Stdio::piped())        .spawn()        .unwrap();    let mut out = std::io::BufReader::new(child.stdout.take().unwrap());    loop {        let mut line = String::new();        assert!(out.read_line(&mut line).unwrap() > 0);        if line.trim() == "LOCKED" {            break;        }    }    let refusal = ProcessLock::acquire(root.path(), holder("/synthetic/Other.app")).unwrap_err();    assert!(refusal.to_string().contains("/synthetic/Child.app"));    let store = root.path().join("life.db");    assert!(cotton_store::Driver::open(&store).is_err());    assert!(!store.exists(), "a refused process opened a store");    drop(child.stdin.take());    assert!(child.wait().unwrap().success());    ProcessLock::acquire(root.path(), holder("/synthetic/Other.app")).unwrap();}/// RULE: A store connection outliving core shutdown must retain the folder lease; releasing it early permits the 18 Oct 2026 75/151 collision on one Mac during shutdown.#[test]fn an_open_store_retains_its_process_lease() {    let root = tempfile::tempdir().unwrap();    let lease = ProcessLock::claim(root.path(), holder("/synthetic/Recovery.app")).unwrap();    let db = cotton_store::Driver::open(root.path().join("life.db")).unwrap();    drop(lease);    assert!(ProcessLock::acquire(root.path(), holder("/synthetic/Other.app")).is_err());    drop(db);    ProcessLock::acquire(root.path(), holder("/synthetic/Other.app")).unwrap();}/// RULE: A late store open after core close must reclaim the lease before SQLite, or shutdown work can recreate the 18 Oct 2026 two-process collision on one Mac.#[test]fn a_late_store_open_reclaims_the_process_lease() {    let root = tempfile::tempdir().unwrap();    let lease = ProcessLock::claim(root.path(), holder("/synthetic/Recovery.app")).unwrap();    drop(lease);    let db = cotton_store::Driver::open(root.path().join("late.db")).unwrap();    assert!(ProcessLock::acquire(root.path(), holder("/synthetic/Other.app")).is_err());    drop(db);}