fn test_get_sockets_to_evict_some_closed()

in nfm-controller/src/events/sock_cache.rs [731:799]


    fn test_get_sockets_to_evict_some_closed() {
        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()
                    },
                },
            );

            // Also add a context in cache.
            let context = SockContext {
                address_family: AF_INET,
                ..Default::default()
            };
            sock_cache.add_context(*sock_key, context, now_us);
        }

        // Close two of them.
        sock_stream
            .get_mut(&sock_keys[0])
            .unwrap()
            .stats
            .state_flags
            .insert(SockStateFlags::CLOSED);
        sock_stream
            .get_mut(&sock_keys[1])
            .unwrap()
            .stats
            .state_flags
            .insert(SockStateFlags::CLOSED);

        // 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!(socks_evicted.len(), 2);
        assert_eq!(num_stale, 0);
        assert_eq!(sock_cache.len(), sock_keys.len() - 2);
        assert_eq!(
            &socks_evicted.get(&sock_keys[0]).unwrap().agg_stats,
            sock_stream.get(&sock_keys[0]).unwrap()
        );
        assert_eq!(
            &socks_evicted.get(&sock_keys[1]).unwrap().agg_stats,
            sock_stream.get(&sock_keys[1]).unwrap()
        );
    }