Skip to content

Commit

Permalink
Finalize test
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Mar 23, 2024
1 parent eab97d2 commit 36dc5e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions tests/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,18 +44,30 @@ 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")]
#[cfg(target_os = "linux")]
async fn test_average_of_mid_interval() {
test_interval_average(60, time::Duration::from_secs_f32(135. / 120.)).await;
}

0 comments on commit 36dc5e7

Please sign in to comment.