in nfm-controller/src/events/sock_cache.rs [802:861]
fn test_get_sockets_to_evict_some_inactive() {
let now_us = 100;
let notrack_us = 10;
let mut sock_cache = SockCache::new();
// Create many active sockets.
let sock_keys: Vec<u64> = vec![99, 101, 4, 55, 19, 79];
let mut sock_stream: HashMap<SockKey, AggSockStats> = HashMap::new();
for sock_key in sock_keys.iter() {
let cpu_id = *sock_key % 2;
sock_stream.insert(
*sock_key,
AggSockStats {
cpus: vec![cpu_id as u32],
stats: SockStats {
last_touched_us: now_us - (notrack_us / 2),
..Default::default()
},
},
);
}
// Make two of them inactive.
sock_stream
.get_mut(&sock_keys[2])
.unwrap()
.stats
.last_touched_us = now_us - notrack_us;
sock_stream
.get_mut(&sock_keys[5])
.unwrap()
.stats
.last_touched_us = now_us - (notrack_us * 2);
// Store the full set in cache.
let staleness_timestamp: u64 = now_us - notrack_us;
let result = sock_cache.update_stats_and_get_deltas(&mut sock_stream, staleness_timestamp);
assert_eq!(
result,
SockOperationResult {
completed: 0,
partial: sock_keys.len().try_into().unwrap(),
failed: 0,
}
);
// Perform eviction and validate.
let (socks_evicted, num_stale) = sock_cache.perform_eviction();
assert_eq!(socks_evicted.len(), 2);
assert_eq!(num_stale, 2);
assert_eq!(sock_cache.len(), sock_keys.len() - 2);
assert_eq!(
&socks_evicted.get(&sock_keys[2]).unwrap().agg_stats,
sock_stream.get(&sock_keys[2]).unwrap()
);
assert_eq!(
&socks_evicted.get(&sock_keys[5]).unwrap().agg_stats,
sock_stream.get(&sock_keys[5]).unwrap()
);
}