use powdb_query::executor::Engine;
const LOCK_FILE: &str = "LOCK";
#[test]
fn open_refused_when_another_live_process_holds_the_lock() {
let dir = tempfile::tempdir().unwrap();
drop(Engine::new(dir.path()).unwrap());
let mut child = std::process::Command::new("sleep")
.arg("30")
.spawn()
.unwrap();
std::fs::write(dir.path().join(LOCK_FILE), child.id().to_string()).unwrap();
let result = Engine::new(dir.path());
let _ = child.kill();
let _ = child.wait();
assert!(
result.is_err(),
"opening a dir locked by another live process must fail, got Ok"
);
}
#[test]
fn open_takes_over_stale_lock_from_dead_process() {
let dir = tempfile::tempdir().unwrap();
drop(Engine::new(dir.path()).unwrap());
let mut child = std::process::Command::new("true").spawn().unwrap();
let dead_pid = child.id();
child.wait().unwrap();
std::fs::write(dir.path().join(LOCK_FILE), dead_pid.to_string()).unwrap();
assert!(
Engine::new(dir.path()).is_ok(),
"a stale lock from a dead process must be taken over"
);
}
#[test]
fn clean_close_releases_lock_for_reopen() {
let dir = tempfile::tempdir().unwrap();
drop(Engine::new(dir.path()).unwrap());
assert!(
Engine::new(dir.path()).is_ok(),
"reopen after a clean close must succeed"
);
}