Clean up tests

This commit is contained in:
Silas Brack 2026-03-08 13:31:35 +01:00
parent fa4dc716db
commit 5cdaeddc0e
3 changed files with 12 additions and 169 deletions

View file

@ -243,88 +243,7 @@ pub async fn list_keys(
Ok((StatusCode::OK, keys.join("\n")).into_response())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_volumes_for_key_sufficient() {
let volumes: Vec<String> = (1..=3).map(|i| format!("http://vol{i}")).collect();
let selected = crate::hasher::volumes_for_key("test-key", &volumes, 2);
assert_eq!(selected.len(), 2);
}
#[test]
fn test_volumes_for_key_insufficient() {
let volumes: Vec<String> = vec!["http://vol1".into()];
let selected = crate::hasher::volumes_for_key("test-key", &volumes, 2);
assert_eq!(selected.len(), 1);
}
#[test]
fn test_first_healthy_volume_finds_first() {
let volumes = vec!["http://vol1".into(), "http://vol2".into(), "http://vol3".into()];
let results = vec![true, true, true];
assert_eq!(
first_healthy_volume("key", &volumes, &results),
ProbeResult::Found("http://vol1/key".into())
);
}
#[test]
fn test_first_healthy_volume_skips_unhealthy() {
let volumes = vec!["http://vol1".into(), "http://vol2".into(), "http://vol3".into()];
let results = vec![false, false, true];
assert_eq!(
first_healthy_volume("key", &volumes, &results),
ProbeResult::Found("http://vol3/key".into())
);
}
#[test]
fn test_first_healthy_volume_all_failed() {
let volumes = vec!["http://vol1".into(), "http://vol2".into()];
let results = vec![false, false];
assert_eq!(
first_healthy_volume("key", &volumes, &results),
ProbeResult::AllFailed
);
}
#[test]
fn test_first_healthy_volume_early_exit() {
// Simulates early exit: only first two volumes were probed
let volumes = vec!["http://vol1".into(), "http://vol2".into(), "http://vol3".into()];
let results = vec![false, true]; // Only 2 results because we stopped early
assert_eq!(
first_healthy_volume("key", &volumes, &results),
ProbeResult::Found("http://vol2/key".into())
);
}
#[test]
fn test_shuffle_volumes_deterministic_with_seed() {
let volumes: Vec<String> = (1..=5).map(|i| format!("http://vol{i}")).collect();
let a = shuffle_volumes(volumes.clone(), 42);
let b = shuffle_volumes(volumes.clone(), 42);
assert_eq!(a, b, "same seed should produce same order");
}
#[test]
fn test_shuffle_volumes_different_seeds() {
let volumes: Vec<String> = (1..=10).map(|i| format!("http://vol{i}")).collect();
let a = shuffle_volumes(volumes.clone(), 1);
let b = shuffle_volumes(volumes.clone(), 2);
assert_ne!(a, b, "different seeds should produce different orders");
}
#[test]
fn test_shuffle_volumes_preserves_elements() {
let volumes: Vec<String> = (1..=5).map(|i| format!("http://vol{i}")).collect();
let mut shuffled = shuffle_volumes(volumes.clone(), 123);
shuffled.sort();
let mut original = volumes;
original.sort();
assert_eq!(shuffled, original);
}
}
// Note: first_healthy_volume and shuffle_volumes are trivial functions
// (essentially .find() and .shuffle()). Testing them would just test
// that standard library functions work. The real test is integration:
// does failover work with actual down volumes?