#![cfg(feature = "armour")]
use armdb::{Config, ConstTree, DbError, FixedConfig, FixedTree};
use tempfile::tempdir;
type Tree = ConstTree<[u8; 8], 8>;
#[test]
fn bitcask_collection_open_twice_errors() {
let dir = tempdir().unwrap();
let _first = Tree::open(dir.path(), Config::test()).expect("first open");
let res = Tree::open(dir.path(), Config::test());
let err = match res {
Err(e) => e,
Ok(_) => panic!("second open of a live collection must fail"),
};
assert!(matches!(err, DbError::Locked { .. }), "got {err:?}");
}
#[test]
fn bitcask_collection_lock_released_on_drop() {
let dir = tempdir().unwrap();
let first = Tree::open(dir.path(), Config::test()).expect("first open");
drop(first);
Tree::open(dir.path(), Config::test()).expect("reopen after drop must succeed");
}
#[cfg(feature = "replication")]
#[test]
fn bitcask_replication_server_retains_the_lock_after_the_collection_drops() {
use armdb::ShutdownSignal;
let dir = tempdir().unwrap();
let tree = Tree::open(dir.path(), Config::test()).expect("open");
let signal = ShutdownSignal::new();
let server = tree
.start_replication_server("127.0.0.1:0".parse().unwrap(), signal.clone())
.expect("start replication server");
drop(tree);
let res = Tree::open(dir.path(), Config::test());
let err = match res {
Err(e) => e,
Ok(_) => panic!("lock must hold while replication still owns the shards"),
};
assert!(matches!(err, DbError::Locked { .. }), "got {err:?}");
signal.shutdown();
drop(server);
Tree::open(dir.path(), Config::test()).expect("lock must release once replication is gone");
}
type FxTree = FixedTree<[u8; 8], 8>;
#[test]
fn fixed_collection_open_twice_errors() {
let dir = tempdir().unwrap();
let _first = FxTree::open(dir.path(), FixedConfig::test()).expect("first open");
let res = FxTree::open(dir.path(), FixedConfig::test());
let err = match res {
Err(e) => e,
Ok(_) => panic!("second open of a live fixed collection must fail"),
};
assert!(matches!(err, DbError::Locked { .. }), "got {err:?}");
}
#[test]
fn fixed_collection_lock_released_on_drop() {
let dir = tempdir().unwrap();
let first = FxTree::open(dir.path(), FixedConfig::test()).expect("first open");
drop(first);
FxTree::open(dir.path(), FixedConfig::test()).expect("reopen after drop must succeed");
}