use std::{
env,
process::{Command, exit},
};
use wal_db::Wal;
const CHILD_ENV: &str = "WALDB_DURABILITY_CHILD_PATH";
const RECORDS: &[&[u8]] = &[b"alpha", b"", b"gamma record", &[0u8; 200]];
#[test]
fn records_survive_a_process_restart() {
if let Ok(path) = env::var(CHILD_ENV) {
let wal = Wal::open(&path).expect("child opens the log");
for record in RECORDS {
let _ = wal.append(record).expect("child appends");
}
wal.sync().expect("child syncs to stable storage");
exit(0);
}
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("cross_process.wal");
let exe = env::current_exe().unwrap();
let status = Command::new(exe)
.args([
"records_survive_a_process_restart",
"--exact",
"--nocapture",
])
.env(CHILD_ENV, &path)
.status()
.unwrap();
assert!(status.success(), "child writer process exited with failure");
let wal = Wal::open(&path).unwrap();
let recovered: Vec<Vec<u8>> = wal
.iter()
.unwrap()
.map(|entry| entry.unwrap().into_data())
.collect();
let expected: Vec<Vec<u8>> = RECORDS.iter().map(|record| record.to_vec()).collect();
assert_eq!(recovered, expected);
}