1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#[cfg(test)]
mod tests {
use super::super::PeriodicStore;
use super::super::Store;
use std::time::{Duration, SystemTime};
#[test]
fn test_cleanup_actually_happens() {
let mut store = PeriodicStore::with_capacity(100);
let now = SystemTime::now();
// Add 1000 entries with 1 second TTL
for i in 0..1000 {
let key = format!("key_{i}");
store
.set_if_not_exists_with_ttl(&key, i, Duration::from_secs(1), now)
.unwrap();
}
// Verify all entries exist
assert_eq!(store.len(), 1000);
// Move time forward by 61 seconds (past TTL and cleanup interval)
let future = now + Duration::from_secs(61);
// Trigger cleanup by performing an operation after the cleanup interval
store
.set_if_not_exists_with_ttl("trigger", 999, Duration::from_secs(60), future)
.unwrap();
// Verify expired entries were removed
// Should only have the trigger entry
assert!(
store.len() < 50,
"Cleanup didn't remove expired entries. Size: {}",
store.len()
);
// Verify the trigger entry exists
assert!(store.get("trigger", future).unwrap().is_some());
}
#[test]
fn test_cleanup_with_memory_pressure() {
let mut store = PeriodicStore::with_capacity(100);
let now = SystemTime::now();
// Fill store with mixed TTL entries
for i in 0..500 {
let key = format!("key_{i}");
let ttl = if i % 2 == 0 {
Duration::from_secs(1) // Half expire quickly
} else {
Duration::from_secs(3600) // Half have long TTL
};
store.set_if_not_exists_with_ttl(&key, i, ttl, now).unwrap();
}
// Move time forward past cleanup interval
let later = now + Duration::from_secs(61);
// Trigger cleanup by performing an operation after the cleanup interval
store
.set_if_not_exists_with_ttl("trigger", 999, Duration::from_secs(60), later)
.unwrap();
// Verify cleanup happened - should have ~250 long-TTL entries + trigger
assert!(
store.len() < 300 && store.len() > 200,
"Expected ~251 entries after cleanup, got {}",
store.len()
);
// Verify that long-TTL entries still exist
for i in (1..100).step_by(2) {
let key = format!("key_{i}");
assert!(
store.get(&key, later).unwrap().is_some(),
"Long-TTL entry {i} should still exist"
);
}
}
#[test]
fn test_no_cleanup_without_triggers() {
let mut store = PeriodicStore::with_capacity(100);
let now = SystemTime::now();
// Add entries with long TTL
for i in 0..100 {
let key = format!("key_{i}");
store
.set_if_not_exists_with_ttl(&key, i, Duration::from_secs(3600), now)
.unwrap();
}
// Do operations but not enough to trigger cleanup
for i in 0..10 {
let key = format!("key_{i}");
let _ = store.get(&key, now);
}
// Verify no cleanup happened (all entries still valid)
assert_eq!(store.len(), 100);
assert_eq!(store.expired_count(), 0);
}
}