@@ -11,18 +11,15 @@ use std::time::{Duration, Instant};
11
11
#[ cfg( target_family = "wasm" ) ]
12
12
use web_time:: { Duration , Instant } ;
13
13
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) ;
16
16
17
17
use async_io:: Timer ;
18
18
use futures_lite:: { FutureExt , StreamExt } ;
19
19
20
20
#[ cfg( not( target_family = "wasm" ) ) ]
21
21
use futures_lite:: future;
22
22
23
- #[ cfg( target_family = "wasm" ) ]
24
- use wasm_bindgen_test:: wasm_bindgen_test as test;
25
-
26
23
fn spawn < T : Send + ' static > (
27
24
f : impl Future < Output = T > + Send + ' static ,
28
25
) -> impl Future < Output = T > + Send + ' static {
@@ -54,18 +51,49 @@ fn block_on(f: impl Future<Output = ()> + 'static) {
54
51
wasm_bindgen_futures:: spawn_local ( f)
55
52
}
56
53
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( ) {
60
89
let start = Instant :: now( ) ;
61
90
Timer :: after( Duration :: from_secs( 1 ) ) . await ;
62
91
assert!( start. elapsed( ) >= Duration :: from_secs( 1 ) ) ;
63
- } ) ;
92
+ }
64
93
}
65
94
66
- #[ test]
67
- fn interval ( ) {
68
- block_on ( async {
95
+ test ! {
96
+ async fn interval( ) {
69
97
let period = Duration :: from_secs( 1 ) ;
70
98
let jitter = Duration :: from_millis( 500 ) ;
71
99
let start = Instant :: now( ) ;
@@ -76,12 +104,11 @@ fn interval() {
76
104
timer. next( ) . await ;
77
105
let elapsed = start. elapsed( ) ;
78
106
assert!( elapsed >= period * 2 && elapsed - period * 2 < jitter) ;
79
- } ) ;
107
+ }
80
108
}
81
109
82
- #[ test]
83
- fn poll_across_tasks ( ) {
84
- block_on ( async {
110
+ test ! {
111
+ async fn poll_across_tasks( ) {
85
112
let start = Instant :: now( ) ;
86
113
let ( sender, receiver) = async_channel:: bounded( 1 ) ;
87
114
@@ -107,7 +134,7 @@ fn poll_across_tasks() {
107
134
task2. await ;
108
135
109
136
assert!( start. elapsed( ) >= Duration :: from_secs( 1 ) ) ;
110
- } ) ;
137
+ }
111
138
}
112
139
113
140
#[ cfg( not( target_family = "wasm" ) ) ]
0 commit comments