in nfm-controller/src/events/sock_cache.rs [864:923]
fn test_get_sockets_to_evict_all_closed() {
let now_us = 100;
let notrack_us = 10;
let mut sock_cache = SockCache::new();
// Create many sockets, all closed.
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),
state_flags: SockStateFlags::CLOSED,
..Default::default()
},
},
);
// Also add a context in cache.
let context = SockContext {
address_family: AF_INET,
..Default::default()
};
let result = sock_cache.add_context(*sock_key, context, now_us);
assert_eq!(
result,
SockOperationResult {
completed: 1,
partial: 0,
failed: 0,
}
);
}
// 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: sock_keys.len().try_into().unwrap(),
partial: 0,
failed: 0,
}
);
// Perform eviction and validate.
let (socks_evicted, num_stale) = sock_cache.perform_eviction();
assert_eq!(num_stale, 0);
assert_eq!(sock_cache.len(), 0);
assert_eq!(socks_evicted.len(), sock_keys.len());
for key in sock_keys.iter() {
assert!(socks_evicted.get(key).is_some());
}
}