From 4ffc39548ec67584892d93b0f545c532d4064bdd Mon Sep 17 00:00:00 2001 From: John Nunley Date: Sun, 24 Sep 2023 12:35:47 -0700 Subject: [PATCH] Fix failing tests Signed-off-by: John Nunley --- src/timer/web.rs | 10 ++++++++++ tests/timer.rs | 27 +++++++++++---------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/timer/web.rs b/src/timer/web.rs index 2c40761..4fdfc19 100644 --- a/src/timer/web.rs +++ b/src/timer/web.rs @@ -22,6 +22,9 @@ pub(super) struct Timer { /// The ongoing timeout or interval. ongoing_timeout: TimerId, + + /// Keep the closure alive so we don't drop it. + closure: Option>, } #[derive(Debug)] @@ -50,6 +53,7 @@ impl Timer { waker: AtomicWaker::new(), }), ongoing_timeout: TimerId::NoTimer, + closure: None, } } @@ -95,6 +99,9 @@ impl Timer { duration.as_millis().try_into().expect("timeout too long"), ); + // Make sure we don't drop the closure before it's called. + self.closure = Some(closure); + match result { Ok(id) => id, Err(_) => { @@ -124,6 +131,9 @@ impl Timer { period.as_millis().try_into().expect("timeout too long"), ); + // Make sure we don't drop the closure before it's called. + self.closure = Some(closure); + match result { Ok(id) => id, Err(_) => { diff --git a/tests/timer.rs b/tests/timer.rs index 470cf97..5a16089 100644 --- a/tests/timer.rs +++ b/tests/timer.rs @@ -20,18 +20,25 @@ use futures_lite::{FutureExt, StreamExt}; #[cfg(not(target_family = "wasm"))] use futures_lite::future; +#[cfg(not(target_family = "wasm"))] fn spawn( f: impl Future + Send + 'static, ) -> impl Future + Send + 'static { let (s, r) = async_channel::bounded(1); - #[cfg(not(target_family = "wasm"))] thread::spawn(move || { future::block_on(async { s.send(f.await).await.ok(); }) }); + Box::pin(async move { r.recv().await.unwrap() }) +} + +#[cfg(target_family = "wasm")] +fn spawn(f: impl Future + 'static) -> impl Future + 'static { + let (s, r) = async_channel::bounded(1); + #[cfg(target_family = "wasm")] wasm_bindgen_futures::spawn_local(async move { s.send(f.await).await.ok(); @@ -40,17 +47,6 @@ fn spawn( Box::pin(async move { r.recv().await.unwrap() }) } -#[cfg(not(target_family = "wasm"))] -fn block_on(f: impl Future) -> T { - future::block_on(f) -} - -#[cfg(target_family = "wasm")] -fn block_on(f: impl Future + 'static) { - // TODO: better way of waiting for a future to finish - wasm_bindgen_futures::spawn_local(f) -} - #[cfg(not(target_family = "wasm"))] macro_rules! test { ( @@ -79,11 +75,10 @@ macro_rules! test { async fn $name() { console_error_panic_hook::set_once(); $bl - } + } }; } - test! { async fn smoke() { let start = Instant::now(); @@ -107,7 +102,7 @@ test! { } } -test!{ +test! { async fn poll_across_tasks() { let start = Instant::now(); let (sender, receiver) = async_channel::bounded(1); @@ -140,7 +135,7 @@ test!{ #[cfg(not(target_family = "wasm"))] #[test] fn set() { - block_on(async { + future::block_on(async { let start = Instant::now(); let timer = Arc::new(Mutex::new(Timer::after(Duration::from_secs(10))));