Current section
Files
Jump to
Current section
Files
native/lean_lmdb_nif/src/tests.rs
use crate::crud::*;
use crate::lifecycle::*;
use crate::mutations::*;
use crate::scan::*;
use crate::version;
use heed::MdbError;
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::Ordering;
use std::sync::{Arc, Barrier, Mutex as TestMutex, Weak};
use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};
static TEST_LOCK: TestMutex<()> = TestMutex::new(());
fn options() -> EnvironmentOptions {
EnvironmentOptions {
map_size: 10 * 1024 * 1024,
max_dbs: 8,
max_readers: 16,
read_only: false,
durability: Durability::Sync,
fixed_map: false,
read_ahead: true,
}
}
fn fresh_dir(label: &str) -> PathBuf {
let sequence = NEXT_RESOURCE_ID.fetch_add(1, Ordering::Relaxed);
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |duration| duration.as_nanos());
std::env::temp_dir().join(format!(
"lean_lmdb_{label}_{}_{}_{}",
std::process::id(),
nanos,
sequence
))
}
fn cleanup_environment(path: &Path) {
registry().remove(path);
if let Some(closing) = heed::env_closing_event(path) {
closing.wait();
}
if path.exists() {
fs::remove_dir_all(path).expect("remove isolated test environment");
}
}
fn replace_scan_token_tag(token: &mut Vec<u8>) {
token.truncate(
token
.len()
.checked_sub(8)
.expect("token has an integrity tag"),
);
let tag = scan_token_tag(token);
token.extend_from_slice(&tag.to_be_bytes());
}
fn database_resource(state: &Arc<EnvironmentState>, name: &str) -> DatabaseResource {
DatabaseResource {
state: Arc::clone(state),
database: create_named_database(state, name).expect("create database"),
name: name.to_owned(),
}
}
#[path = "crud_tests.rs"]
mod crud_tests;
#[path = "lifecycle_tests.rs"]
mod lifecycle_tests;
#[path = "mutation_tests.rs"]
mod mutation_tests;
#[path = "scan_tests.rs"]
mod scan_tests;