Skip to content

Commit 2d02fc5

Browse files
committed
Add a better test harness for timers
Signed-off-by: John Nunley <[email protected]>
1 parent 2f0d091 commit 2d02fc5

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ signal-hook = "0.3"
5555
tempfile = "3"
5656

5757
[target.'cfg(target_family = "wasm")'.dev-dependencies]
58-
spin_on = "0.1.1"
58+
console_error_panic_hook = "0.1.7"
5959
wasm-bindgen-futures = "0.4.37"
6060
wasm-bindgen-test = "0.3.37"
6161
web-time = "0.2.0"

tests/timer.rs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ use std::time::{Duration, Instant};
1111
#[cfg(target_family = "wasm")]
1212
use web_time::{Duration, Instant};
1313

14-
//#[cfg(target_family = "wasm")]
15-
//wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
14+
#[cfg(target_family = "wasm")]
15+
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
1616

1717
use async_io::Timer;
1818
use futures_lite::{FutureExt, StreamExt};
1919

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

23-
#[cfg(target_family = "wasm")]
24-
use wasm_bindgen_test::wasm_bindgen_test as test;
25-
2623
fn spawn<T: Send + 'static>(
2724
f: impl Future<Output = T> + Send + 'static,
2825
) -> impl Future<Output = T> + Send + 'static {
@@ -54,18 +51,49 @@ fn block_on(f: impl Future<Output = ()> + 'static) {
5451
wasm_bindgen_futures::spawn_local(f)
5552
}
5653

57-
#[test]
58-
fn smoke() {
59-
block_on(async {
54+
#[cfg(not(target_family = "wasm"))]
55+
macro_rules! test {
56+
(
57+
$(#[$meta:meta])*
58+
async fn $name:ident () $bl:block
59+
) => {
60+
#[test]
61+
$(#[$meta])*
62+
fn $name() {
63+
futures_lite::future::block_on(async {
64+
$bl
65+
})
66+
}
67+
};
68+
}
69+
70+
#[cfg(target_family = "wasm")]
71+
macro_rules! test {
72+
(
73+
$(#[$meta:meta])*
74+
async fn $name:ident () $bl:block
75+
) => {
76+
// wasm-bindgen-test handles waiting on the future for us
77+
#[wasm_bindgen_test::wasm_bindgen_test]
78+
$(#[$meta])*
79+
async fn $name() {
80+
console_error_panic_hook::set_once();
81+
$bl
82+
}
83+
};
84+
}
85+
86+
87+
test! {
88+
async fn smoke() {
6089
let start = Instant::now();
6190
Timer::after(Duration::from_secs(1)).await;
6291
assert!(start.elapsed() >= Duration::from_secs(1));
63-
});
92+
}
6493
}
6594

66-
#[test]
67-
fn interval() {
68-
block_on(async {
95+
test! {
96+
async fn interval() {
6997
let period = Duration::from_secs(1);
7098
let jitter = Duration::from_millis(500);
7199
let start = Instant::now();
@@ -76,12 +104,11 @@ fn interval() {
76104
timer.next().await;
77105
let elapsed = start.elapsed();
78106
assert!(elapsed >= period * 2 && elapsed - period * 2 < jitter);
79-
});
107+
}
80108
}
81109

82-
#[test]
83-
fn poll_across_tasks() {
84-
block_on(async {
110+
test!{
111+
async fn poll_across_tasks() {
85112
let start = Instant::now();
86113
let (sender, receiver) = async_channel::bounded(1);
87114

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

109136
assert!(start.elapsed() >= Duration::from_secs(1));
110-
});
137+
}
111138
}
112139

113140
#[cfg(not(target_family = "wasm"))]

0 commit comments

Comments
 (0)