#[cfg(target_os = "linux")]
const CALLS: usize = 200;
#[cfg(target_os = "linux")]
fn open_fds() -> usize {
std::fs::read_dir("/proc/self/fd").map_or(0, Iterator::count)
}
#[cfg(target_os = "linux")]
fn assert_flat(label: &str, mut probe: impl FnMut()) {
for _ in 0..10 {
probe();
}
let before = open_fds();
for _ in 0..CALLS {
probe();
}
let after = open_fds();
let growth = after.saturating_sub(before);
assert!(
growth <= 2,
"{label} leaked {growth} fds over {CALLS} calls ({before} -> {after})",
);
}
#[cfg(target_os = "linux")]
#[test]
fn probing_does_not_leak_file_descriptors() {
assert_flat("detect()", || {
let _ = gpu_probe::detect();
});
assert_flat("cuda_host()", || {
let _ = gpu_probe::cuda_host();
});
}