polling/tests/precision.rs

79 lines
1.9 KiB
Rust
Raw Normal View History

2020-08-14 11:46:58 +00:00
use std::io;
2020-08-14 12:31:26 +00:00
use std::time::{Duration, Instant};
2020-08-14 11:46:58 +00:00
use polling::Poller;
#[test]
fn below_ms() -> io::Result<()> {
let poller = Poller::new()?;
let mut events = Vec::new();
2020-08-14 12:31:26 +00:00
2020-08-14 12:38:02 +00:00
let dur = Duration::from_micros(100);
2020-08-14 11:46:58 +00:00
let margin = Duration::from_micros(500);
2020-08-14 12:31:26 +00:00
let mut lowest = Duration::from_secs(1000);
2020-08-14 11:46:58 +00:00
for _ in 0..1_000 {
let now = Instant::now();
let n = poller.wait(&mut events, Some(dur))?;
let elapsed = now.elapsed();
2020-08-14 12:38:02 +00:00
2020-08-14 11:46:58 +00:00
assert_eq!(n, 0);
assert!(elapsed >= dur, "{:?} < {:?}", elapsed, dur);
2020-08-14 12:31:26 +00:00
lowest = lowest.min(elapsed);
2020-08-14 11:46:58 +00:00
}
if cfg!(all(
any(
target_os = "linux",
target_os = "android",
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "freebsd",
),
not(polling_test_poll_backend)
)) {
2020-08-14 13:18:44 +00:00
assert!(lowest < dur + margin);
}
2020-08-14 12:31:26 +00:00
Ok(())
2020-08-14 11:46:58 +00:00
}
#[test]
fn above_ms() -> io::Result<()> {
let poller = Poller::new()?;
let mut events = Vec::new();
2020-08-14 12:31:26 +00:00
2020-08-14 12:38:02 +00:00
let dur = Duration::from_micros(3_100);
2020-08-14 11:46:58 +00:00
let margin = Duration::from_micros(500);
2020-08-14 12:31:26 +00:00
let mut lowest = Duration::from_secs(1000);
2020-08-14 11:46:58 +00:00
for _ in 0..1_000 {
let now = Instant::now();
let n = poller.wait(&mut events, Some(dur))?;
let elapsed = now.elapsed();
2020-08-14 12:38:02 +00:00
2020-08-14 11:46:58 +00:00
assert_eq!(n, 0);
assert!(elapsed >= dur, "{:?} < {:?}", elapsed, dur);
2020-08-14 12:31:26 +00:00
lowest = lowest.min(elapsed);
2020-08-14 11:46:58 +00:00
}
if cfg!(all(
any(
target_os = "linux",
target_os = "android",
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "freebsd",
),
not(polling_test_poll_backend)
)) {
2020-08-14 13:18:44 +00:00
assert!(lowest < dur + margin);
}
2020-08-14 12:31:26 +00:00
Ok(())
2020-08-14 11:46:58 +00:00
}