Skip to content

Commit

Permalink
Add a better test harness for timers
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Sep 24, 2023
1 parent 2f0d091 commit 2d02fc5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ signal-hook = "0.3"
tempfile = "3"

[target.'cfg(target_family = "wasm")'.dev-dependencies]
spin_on = "0.1.1"
console_error_panic_hook = "0.1.7"
wasm-bindgen-futures = "0.4.37"
wasm-bindgen-test = "0.3.37"
web-time = "0.2.0"
Expand Down
61 changes: 44 additions & 17 deletions tests/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ use std::time::{Duration, Instant};
#[cfg(target_family = "wasm")]
use web_time::{Duration, Instant};

//#[cfg(target_family = "wasm")]
//wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
#[cfg(target_family = "wasm")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

use async_io::Timer;
use futures_lite::{FutureExt, StreamExt};

#[cfg(not(target_family = "wasm"))]
use futures_lite::future;

#[cfg(target_family = "wasm")]
use wasm_bindgen_test::wasm_bindgen_test as test;

fn spawn<T: Send + 'static>(
f: impl Future<Output = T> + Send + 'static,
) -> impl Future<Output = T> + Send + 'static {
Expand Down Expand Up @@ -54,18 +51,49 @@ fn block_on(f: impl Future<Output = ()> + 'static) {
wasm_bindgen_futures::spawn_local(f)
}

#[test]
fn smoke() {
block_on(async {
#[cfg(not(target_family = "wasm"))]
macro_rules! test {
(
$(#[$meta:meta])*
async fn $name:ident () $bl:block
) => {
#[test]
$(#[$meta])*
fn $name() {
futures_lite::future::block_on(async {
$bl
})
}
};
}

#[cfg(target_family = "wasm")]
macro_rules! test {
(
$(#[$meta:meta])*
async fn $name:ident () $bl:block
) => {
// wasm-bindgen-test handles waiting on the future for us
#[wasm_bindgen_test::wasm_bindgen_test]
$(#[$meta])*
async fn $name() {
console_error_panic_hook::set_once();
$bl
}
};
}


test! {
async fn smoke() {
let start = Instant::now();
Timer::after(Duration::from_secs(1)).await;
assert!(start.elapsed() >= Duration::from_secs(1));
});
}
}

#[test]
fn interval() {
block_on(async {
test! {
async fn interval() {
let period = Duration::from_secs(1);
let jitter = Duration::from_millis(500);
let start = Instant::now();
Expand All @@ -76,12 +104,11 @@ fn interval() {
timer.next().await;
let elapsed = start.elapsed();
assert!(elapsed >= period * 2 && elapsed - period * 2 < jitter);
});
}
}

#[test]
fn poll_across_tasks() {
block_on(async {
test!{
async fn poll_across_tasks() {
let start = Instant::now();
let (sender, receiver) = async_channel::bounded(1);

Expand All @@ -107,7 +134,7 @@ fn poll_across_tasks() {
task2.await;

assert!(start.elapsed() >= Duration::from_secs(1));
});
}
}

#[cfg(not(target_family = "wasm"))]
Expand Down

0 comments on commit 2d02fc5

Please sign in to comment.