use hdf5_pure::{EditSession, Error, File, FileBuilder, FileLocking, SwmrWriter};
use tempfile::tempdir;
fn write_starter(path: &std::path::Path) {
let mut b = FileBuilder::new();
b.create_dataset("d").with_i32_data(&[1, 2, 3]);
b.write(path).unwrap();
}
fn write_appendable(path: &std::path::Path) {
let mut b = FileBuilder::new();
b.create_dataset("d")
.with_i32_data(&[0, 1, 2, 3])
.with_shape(&[4])
.with_maxshape(&[u64::MAX])
.with_chunks(&[1]);
b.write(path).unwrap();
}
#[test]
fn editor_lock_blocks_a_second_editor() {
let dir = tempdir().unwrap();
let path = dir.path().join("edit.h5");
write_starter(&path);
let session = EditSession::open(&path).unwrap();
assert!(
matches!(EditSession::open(&path), Err(Error::FileLocked(_))),
"second EditSession::open should be FileLocked"
);
drop(session);
EditSession::open(&path).expect("editor should open after the first is dropped");
File::open(&path).expect("read should succeed after the editor is dropped");
}
#[test]
fn disabled_locking_takes_no_lock() {
let dir = tempdir().unwrap();
let path = dir.path().join("edit.h5");
write_starter(&path);
let _editor = EditSession::open_with_locking(&path, FileLocking::Disabled).unwrap();
File::open(&path).expect("read should succeed: the Disabled editor took no lock");
EditSession::open_with_locking(&path, FileLocking::Disabled)
.expect("a second Disabled editor should open: neither took a lock");
}
#[test]
fn swmr_writer_does_not_lock_so_a_reader_can_follow_it() {
let dir = tempdir().unwrap();
let path = dir.path().join("swmr.h5");
write_appendable(&path);
let mut writer = SwmrWriter::open(&path).unwrap(); writer
.append_i32("d", &(4..20).collect::<Vec<_>>())
.unwrap();
let reader = File::open_swmr(&path).expect("open_swmr should coexist with a live SWMR writer");
assert_eq!(
reader.dataset("d").unwrap().read_i32().unwrap(),
(0..20).collect::<Vec<_>>()
);
drop(writer);
}