| 1 | use std::io; |
| 2 | use std::time::{Duration, Instant}; |
| 3 | |
| 4 | use polling::{Events, Poller}; |
| 5 | |
| 6 | #[test] |
| 7 | fn below_ms() -> io::Result<()> { |
| 8 | let poller = Poller::new()?; |
| 9 | let mut events = Events::new(); |
| 10 | |
| 11 | let dur = Duration::from_micros(100); |
| 12 | let margin = Duration::from_micros(500); |
| 13 | let mut lowest = Duration::from_secs(1000); |
| 14 | |
| 15 | for _ in 0..1_000 { |
| 16 | let now = Instant::now(); |
| 17 | let n = poller.wait(&mut events, Some(dur))?; |
| 18 | let elapsed = now.elapsed(); |
| 19 | |
| 20 | assert_eq!(n, 0); |
| 21 | assert!(elapsed >= dur, "{elapsed:?} < {dur:?}"); |
| 22 | lowest = lowest.min(elapsed); |
| 23 | } |
| 24 | |
| 25 | if cfg!(all( |
| 26 | any( |
| 27 | target_os = "linux", |
| 28 | target_os = "android", |
| 29 | target_vendor = "apple", |
| 30 | target_os = "freebsd", |
| 31 | ), |
| 32 | not(polling_test_poll_backend) |
| 33 | )) { |
| 34 | assert!(lowest < dur + margin); |
| 35 | } |
| 36 | Ok(()) |
| 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | fn above_ms() -> io::Result<()> { |
| 41 | let poller = Poller::new()?; |
| 42 | let mut events = Events::new(); |
| 43 | |
| 44 | let dur = Duration::from_micros(3_100); |
| 45 | let margin = Duration::from_micros(500); |
| 46 | let mut lowest = Duration::from_secs(1000); |
| 47 | |
| 48 | for _ in 0..1_000 { |
| 49 | let now = Instant::now(); |
| 50 | let n = poller.wait(&mut events, Some(dur))?; |
| 51 | let elapsed = now.elapsed(); |
| 52 | |
| 53 | assert_eq!(n, 0); |
| 54 | assert!(elapsed >= dur, "{elapsed:?} < {dur:?}"); |
| 55 | lowest = lowest.min(elapsed); |
| 56 | } |
| 57 | |
| 58 | if cfg!(all( |
| 59 | any( |
| 60 | target_os = "linux", |
| 61 | target_os = "android", |
| 62 | target_os = "illumos", |
| 63 | target_os = "solaris", |
| 64 | target_vendor = "apple", |
| 65 | target_os = "freebsd", |
| 66 | ), |
| 67 | not(polling_test_poll_backend) |
| 68 | )) { |
| 69 | assert!(lowest < dur + margin); |
| 70 | } |
| 71 | Ok(()) |
| 72 | } |