Skip to content

Commit 4ffc395

Browse files
committed
Fix failing tests
Signed-off-by: John Nunley <[email protected]>
1 parent 2d02fc5 commit 4ffc395

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/timer/web.rs

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ pub(super) struct Timer {
2222

2323
/// The ongoing timeout or interval.
2424
ongoing_timeout: TimerId,
25+
26+
/// Keep the closure alive so we don't drop it.
27+
closure: Option<Closure<dyn FnMut()>>,
2528
}
2629

2730
#[derive(Debug)]
@@ -50,6 +53,7 @@ impl Timer {
5053
waker: AtomicWaker::new(),
5154
}),
5255
ongoing_timeout: TimerId::NoTimer,
56+
closure: None,
5357
}
5458
}
5559

@@ -95,6 +99,9 @@ impl Timer {
9599
duration.as_millis().try_into().expect("timeout too long"),
96100
);
97101

102+
// Make sure we don't drop the closure before it's called.
103+
self.closure = Some(closure);
104+
98105
match result {
99106
Ok(id) => id,
100107
Err(_) => {
@@ -124,6 +131,9 @@ impl Timer {
124131
period.as_millis().try_into().expect("timeout too long"),
125132
);
126133

134+
// Make sure we don't drop the closure before it's called.
135+
self.closure = Some(closure);
136+
127137
match result {
128138
Ok(id) => id,
129139
Err(_) => {

tests/timer.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,25 @@ use futures_lite::{FutureExt, StreamExt};
2020
#[cfg(not(target_family = "wasm"))]
2121
use futures_lite::future;
2222

23+
#[cfg(not(target_family = "wasm"))]
2324
fn spawn<T: Send + 'static>(
2425
f: impl Future<Output = T> + Send + 'static,
2526
) -> impl Future<Output = T> + Send + 'static {
2627
let (s, r) = async_channel::bounded(1);
2728

28-
#[cfg(not(target_family = "wasm"))]
2929
thread::spawn(move || {
3030
future::block_on(async {
3131
s.send(f.await).await.ok();
3232
})
3333
});
3434

35+
Box::pin(async move { r.recv().await.unwrap() })
36+
}
37+
38+
#[cfg(target_family = "wasm")]
39+
fn spawn<T: 'static>(f: impl Future<Output = T> + 'static) -> impl Future<Output = T> + 'static {
40+
let (s, r) = async_channel::bounded(1);
41+
3542
#[cfg(target_family = "wasm")]
3643
wasm_bindgen_futures::spawn_local(async move {
3744
s.send(f.await).await.ok();
@@ -40,17 +47,6 @@ fn spawn<T: Send + 'static>(
4047
Box::pin(async move { r.recv().await.unwrap() })
4148
}
4249

43-
#[cfg(not(target_family = "wasm"))]
44-
fn block_on<T>(f: impl Future<Output = T>) -> T {
45-
future::block_on(f)
46-
}
47-
48-
#[cfg(target_family = "wasm")]
49-
fn block_on(f: impl Future<Output = ()> + 'static) {
50-
// TODO: better way of waiting for a future to finish
51-
wasm_bindgen_futures::spawn_local(f)
52-
}
53-
5450
#[cfg(not(target_family = "wasm"))]
5551
macro_rules! test {
5652
(
@@ -79,11 +75,10 @@ macro_rules! test {
7975
async fn $name() {
8076
console_error_panic_hook::set_once();
8177
$bl
82-
}
78+
}
8379
};
8480
}
8581

86-
8782
test! {
8883
async fn smoke() {
8984
let start = Instant::now();
@@ -107,7 +102,7 @@ test! {
107102
}
108103
}
109104

110-
test!{
105+
test! {
111106
async fn poll_across_tasks() {
112107
let start = Instant::now();
113108
let (sender, receiver) = async_channel::bounded(1);
@@ -140,7 +135,7 @@ test!{
140135
#[cfg(not(target_family = "wasm"))]
141136
#[test]
142137
fn set() {
143-
block_on(async {
138+
future::block_on(async {
144139
let start = Instant::now();
145140
let timer = Arc::new(Mutex::new(Timer::after(Duration::from_secs(10))));
146141

0 commit comments

Comments
 (0)