From 9176c3b8c0edc3783cfcb56b3a50b4cfbbf50923 Mon Sep 17 00:00:00 2001 From: DoumanAsh Date: Sat, 23 Mar 2024 11:04:23 +0900 Subject: [PATCH] Finalize test --- .github/workflows/rust.yml | 2 +- tests/interval.rs | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 92ffd11..bd9ab4c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -53,7 +53,7 @@ jobs: - name: Test with tokio 1.0 if: runner.os != 'Windows' - run: cargo test --features tokio1 --release -- --nocapture + run: cargo test --features tokio1 --release - name: Test run: cargo test --all --features std diff --git a/tests/interval.rs b/tests/interval.rs index 5d15dd9..a055e7a 100644 --- a/tests/interval.rs +++ b/tests/interval.rs @@ -23,17 +23,17 @@ async fn test_interval() { } async fn test_interval_average(num_runs: usize, interval: time::Duration) { + const ACCURACY: time::Duration = time::Duration::from_nanos(133333); + let mut times = Vec::with_capacity(num_runs); println!("interval={:?}", interval); let mut timer = async_timer::interval(interval); - // let mut timer = tokio::time::interval(Duration::from_secs_f32(1. / 120.)); for _ in 0..num_runs { let start = std::time::Instant::now(); timer.wait().await; - // timer.tick().await; // simulate some work, this doesn't have to be consistent // the timer is the only thing that needs to be consistent @@ -44,18 +44,29 @@ async fn test_interval_average(num_runs: usize, interval: time::Duration) { times.push(start.elapsed()); } - // print the average time let total: time::Duration = times.iter().sum(); let average = total / num_runs as u32; - panic!("Average time: {:?}", average); + + let min = interval - ACCURACY; + let max = interval + ACCURACY; + println!("Check {:?} <= average={:?} <= {:?}", min, average, max); + //Margin of error should be within interval-0.1ms..=interval+0.1ms + assert!(min <= average); + assert!(average <= max); } +//Windows timers are shite for small duration +//kevent() also behaves badly for some reason +//only linux's timerfd is reliable #[tokio::test] +#[cfg(feature = "tokio1")] +#[cfg(target_os = "linux")] async fn test_average_of_small_interval() { test_interval_average(6000, time::Duration::from_secs_f32(1. / 120.)).await; } #[tokio::test] +#[cfg(feature = "tokio1")] async fn test_average_of_mid_interval() { test_interval_average(60, time::Duration::from_secs_f32(135. / 120.)).await; }