use std::{net::IpAddr, time::Duration};
use futures_util::StreamExt;
use massping::DualstackPinger;
use tokio::time;
#[tokio::test(flavor = "current_thread")]
async fn ping_localhost_current_thread() {
let pinger = DualstackPinger::new().unwrap();
let localhost: IpAddr = "127.0.0.1".parse().unwrap();
let mut stream = pinger.measure_many([localhost].into_iter());
let result = time::timeout(Duration::from_secs(5), stream.next()).await;
match result {
Ok(Some((addr, rtt))) => {
assert_eq!(addr, localhost);
assert!(rtt < Duration::from_secs(1), "RTT too high: {rtt:?}");
}
Ok(None) => {
panic!("stream ended unexpectedly");
}
Err(_) => {
panic!(
"timeout waiting for ping response - \
this indicates the current_thread runtime bug (issue #1) has regressed"
);
}
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn ping_localhost_multi_thread() {
let pinger = DualstackPinger::new().unwrap();
let localhost: IpAddr = "127.0.0.1".parse().unwrap();
let mut stream = pinger.measure_many([localhost].into_iter());
let result = time::timeout(Duration::from_secs(5), stream.next()).await;
match result {
Ok(Some((addr, rtt))) => {
assert_eq!(addr, localhost);
assert!(rtt < Duration::from_secs(1), "RTT too high: {rtt:?}");
}
Ok(None) => {
panic!("stream ended unexpectedly");
}
Err(_) => {
panic!("timeout waiting for ping response");
}
}
}
#[tokio::test(flavor = "current_thread")]
async fn ping_sequential_current_thread() {
let pinger = DualstackPinger::new().unwrap();
let localhost: IpAddr = "127.0.0.1".parse().unwrap();
for i in 0..3 {
let mut stream = pinger.measure_many([localhost].into_iter());
let result = time::timeout(Duration::from_secs(5), stream.next()).await;
match result {
Ok(Some((addr, rtt))) => {
assert_eq!(addr, localhost);
assert!(
rtt < Duration::from_secs(1),
"RTT too high on ping {i}: {rtt:?}"
);
}
Ok(None) => {
panic!("stream ended unexpectedly on ping {i}");
}
Err(_) => {
panic!(
"timeout on ping {i} - \
current_thread runtime bug may have regressed"
);
}
}
}
}