Skip to content

Commit

Permalink
Manager tests for safe-mode backend
Browse files Browse the repository at this point in the history
  • Loading branch information
badboy authored and saschanaz committed Nov 1, 2023
1 parent b367f7c commit efe48ec
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ lazy_static! {
/// is true by default, because it helps enforce the constraints guaranteed by
/// this manager. However, path canonicalization might crash in some fringe
/// circumstances, so the `no-canonicalize-path` feature offers the possibility of
/// disabling it. See: https://bugzilla.mozilla.org/show_bug.cgi?id=1531887
/// disabling it. See: <https://bugzilla.mozilla.org/show_bug.cgi?id=1531887>
///
/// When path canonicalization is disabled, you *must* ensure an RKV environment is
/// always created or retrieved with the same path.
Expand Down Expand Up @@ -201,6 +201,7 @@ mod tests {

#[cfg(feature = "lmdb")]
use backend::Lmdb;
use backend::SafeMode;

/// Test that one can mutate managed Rkv instances in surprising ways.
#[cfg(feature = "lmdb")]
Expand Down Expand Up @@ -244,4 +245,46 @@ mod tests {
.expect("success");
assert!(!Arc::ptr_eq(&path2_arc, &arc));
}

/// Test that one can mutate managed Rkv instances in surprising ways.
#[test]
fn test_mutate_managed_rkv_safemode() {
let mut manager = Manager::<SafeModeEnvironment>::new();

let root1 = Builder::new()
.prefix("test_mutate_managed_rkv_1")
.tempdir()
.expect("tempdir");
fs::create_dir_all(root1.path()).expect("dir created");
let path1 = root1.path();
let arc = manager
.get_or_create(path1, Rkv::new::<SafeMode>)
.expect("created");

// Arc<RwLock<>> has interior mutability, so we can replace arc's Rkv instance with a new
// instance that has a different path.
let root2 = Builder::new()
.prefix("test_mutate_managed_rkv_2")
.tempdir()
.expect("tempdir");
fs::create_dir_all(root2.path()).expect("dir created");
let path2 = root2.path();
{
let mut rkv = arc.write().expect("guard");
let rkv2 = Rkv::new::<SafeMode>(path2).expect("Rkv");
*rkv = rkv2;
}

// Arc now has a different internal Rkv with path2, but it's still mapped to path1 in
// manager, so its pointer is equal to a new Arc for path1.
let path1_arc = manager.get(path1).expect("success").expect("existed");
assert!(Arc::ptr_eq(&path1_arc, &arc));

// Meanwhile, a new Arc for path2 has a different pointer, even though its Rkv's path is
// the same as arc's current path.
let path2_arc = manager
.get_or_create(path2, Rkv::new::<SafeMode>)
.expect("success");
assert!(!Arc::ptr_eq(&path2_arc, &arc));
}
}

0 comments on commit efe48ec

Please sign in to comment.