use redup_config::Config;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Config)]
pub struct MemoryConfig {
mmap_threshold: Option<u64>,
}
impl Default for MemoryConfig {
fn default() -> Self {
Self {
mmap_threshold: Some(2 * 1024 * 1024),
}
}
}
impl MemoryConfig {
pub fn mmap_threshold(&self) -> Option<u64> {
self.mmap_threshold.filter(|threshold| *threshold > 0)
}
}
#[cfg(test)]
mod tests {
use redup_config::{Source, load};
use super::MemoryConfig;
fn read(section: &str) -> MemoryConfig {
let directory = tempfile::tempdir().expect("a temporary directory");
std::fs::write(
directory.path().join("config.yaml"),
format!("memory:\n{section}"),
)
.expect("the configuration file");
load::<MemoryConfig>(&Source::directory(directory.path()))
.expect("the configuration is accepted")
.value
}
#[test]
fn threshold_is_two_mebibytes_by_default() {
assert_eq!(
MemoryConfig::default().mmap_threshold(),
Some(2 * 1024 * 1024)
);
assert_eq!(
read("\x20 mmapThreshold: 4Mi\n").mmap_threshold(),
Some(4 * 1024 * 1024)
);
}
#[test]
fn null_disables_the_threshold() {
assert_eq!(read("\x20 mmapThreshold: ~\n").mmap_threshold(), None);
}
#[test]
fn zero_disables_the_threshold() {
assert_eq!(read("\x20 mmapThreshold: 0\n").mmap_threshold(), None);
}
}