use massping::V4Pinger;
fn count_fds() -> usize {
std::fs::read_dir("/proc/self/fd").unwrap().count()
}
#[tokio::test(flavor = "current_thread")]
async fn dropping_pinger_closes_socket_and_stops_task() {
drop(V4Pinger::new().unwrap());
for _ in 0..100 {
tokio::task::yield_now().await;
}
let baseline = count_fds();
let pingers: Vec<_> = (0..5).map(|_| V4Pinger::new().unwrap()).collect();
assert!(
count_fds() >= baseline + 5,
"each pinger should hold a socket fd"
);
drop(pingers);
let mut fds = count_fds();
for _ in 0..100 {
if fds == baseline {
break;
}
tokio::task::yield_now().await;
fds = count_fds();
}
assert_eq!(
fds, baseline,
"background task/socket leaked after dropping the pingers"
);
}