use super::*;
fn entry(content: Arc<str>) -> CachedRobots {
CachedRobots {
content,
expires_at: Instant::now() + Duration::from_secs(60),
fetch_failed: false,
}
}
#[test]
fn test_memory_cache_entry_cap_is_enforced() {
let mut cache = MemoryCache::default();
for i in 0..(MAX_MEMORY_CACHE_ENTRIES + 8) {
cache.insert(format!("http://h{i}.test/robots.txt"), entry(Arc::from("")));
}
assert_eq!(cache.entries.len(), MAX_MEMORY_CACHE_ENTRIES);
assert_eq!(cache.total_bytes, 0);
}
#[test]
fn test_memory_cache_byte_cap_evicts_oldest() {
let mut cache = MemoryCache::default();
let big: Arc<str> = Arc::from("x".repeat(9 * 1024 * 1024));
cache.insert("http://a.test/robots.txt".to_string(), entry(big.clone()));
cache.insert("http://b.test/robots.txt".to_string(), entry(big));
assert_eq!(cache.entries.len(), 1);
assert_eq!(cache.total_bytes, 9 * 1024 * 1024);
assert!(cache.entries.contains_key("http://b.test/robots.txt"));
}
#[test]
fn test_memory_cache_overwrite_reclaims_bytes() {
let mut cache = MemoryCache::default();
cache.insert(
"http://a.test/robots.txt".to_string(),
entry(Arc::from("x".repeat(1024 * 1024))),
);
cache.insert(
"http://a.test/robots.txt".to_string(),
entry(Arc::from("y".repeat(1024))),
);
assert_eq!(cache.entries.len(), 1);
assert_eq!(cache.total_bytes, 1024);
}
#[test]
fn test_memory_cache_rejects_oversized_entry() {
let mut cache = MemoryCache::default();
let oversized: Arc<str> = Arc::from("x".repeat(MAX_MEMORY_CACHE_BYTES + 1));
cache.insert("http://big.test/robots.txt".to_string(), entry(oversized));
assert!(cache.entries.is_empty());
assert_eq!(cache.total_bytes, 0);
}