use std::sync::atomic::AtomicBool;
use znippy_plugin_git::exploded_arrow::ExplodePolicy;
use znippy_plugin_git::resolve::{DEFAULT_LOOKAHEAD_BYTES, ResolveLimits};
use znippy_plugin_git::{
explode_default, set_explode_budget_default, set_explode_default,
set_resolve_lookahead_default, set_resolve_workers_default,
};
const TWIN_WORKERS: usize = 2;
const TWIN_LOOKAHEAD: u64 = 16 << 20;
const TWIN_EXPLODE_BUDGET: u64 = 16 << 20;
fn become_a_twin() {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
set_explode_default(ExplodePolicy::Off);
set_explode_budget_default(Some(TWIN_EXPLODE_BUDGET));
set_resolve_workers_default(TWIN_WORKERS);
set_resolve_lookahead_default(TWIN_LOOKAHEAD);
});
}
#[test]
fn the_resolver_takes_its_width_and_lookahead_from_the_setters() {
become_a_twin();
let cancel = AtomicBool::new(false);
let progress = znippy_plugin_git::progress::IndexProgress::new("twin-runtime-limits");
let dog = std::sync::Arc::new(znippy_plugin_git::memguard::MemWatchdog::from_env());
let spill = || 0u64;
let limits = ResolveLimits::for_indexer("twin", &cancel, &progress, dog, &spill);
assert_eq!(
limits.workers, TWIN_WORKERS,
"the resolver width the indexer will run at is not what set_resolve_workers_default said"
);
assert_eq!(
limits.lookahead_bytes, TWIN_LOOKAHEAD,
"the bytes the workers may run ahead by is not what set_resolve_lookahead_default said"
);
assert_ne!(
limits.lookahead_bytes, DEFAULT_LOOKAHEAD_BYTES,
"the assertion above would pass on the shipped default; it must not be able to"
);
}
#[test]
fn a_store_opened_after_the_setter_carries_the_policy_it_set() {
become_a_twin();
assert_eq!(explode_default(), ExplodePolicy::Off);
assert_eq!(
ExplodePolicy::from_env().unwrap(),
ExplodePolicy::Off,
"the policy a store opens with is not what set_explode_default said"
);
}
static BUDGET_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[test]
fn the_explode_budget_comes_from_the_setter_and_not_from_the_filesystem() {
become_a_twin();
let _g = BUDGET_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = std::env::temp_dir();
assert_eq!(
znippy_plugin_git::exploded_cache::budget_from_env(&dir).unwrap(),
TWIN_EXPLODE_BUDGET,
"the budget a store opens with is not what set_explode_budget_default said"
);
assert_ne!(
znippy_plugin_git::exploded_cache::default_budget(&dir),
TWIN_EXPLODE_BUDGET,
"this filesystem's derived budget happens to equal the twin's figure, so the assertion \
above proves nothing here — pick another number"
);
}
#[test]
fn clearing_the_explode_budget_default_restores_the_filesystem_derived_one() {
become_a_twin();
let _g = BUDGET_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = std::env::temp_dir();
set_explode_budget_default(None);
let back = znippy_plugin_git::exploded_cache::budget_from_env(&dir).unwrap();
set_explode_budget_default(Some(TWIN_EXPLODE_BUDGET));
assert_eq!(back, znippy_plugin_git::exploded_cache::default_budget(&dir));
}