Skip to content

Commit

Permalink
Fix failing tests
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 2d02fc5 commit 4ffc395
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/timer/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Closure<dyn FnMut()>>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -50,6 +53,7 @@ impl Timer {
waker: AtomicWaker::new(),
}),
ongoing_timeout: TimerId::NoTimer,
closure: None,
}
}

Expand Down Expand Up @@ -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(_) => {
Expand Down Expand Up @@ -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(_) => {
Expand Down
27 changes: 11 additions & 16 deletions tests/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Send + 'static>(
f: impl Future<Output = T> + Send + 'static,
) -> impl Future<Output = T> + 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<T: 'static>(f: impl Future<Output = T> + 'static) -> impl Future<Output = T> + '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();
Expand All @@ -40,17 +47,6 @@ fn spawn<T: Send + 'static>(
Box::pin(async move { r.recv().await.unwrap() })
}

#[cfg(not(target_family = "wasm"))]
fn block_on<T>(f: impl Future<Output = T>) -> T {
future::block_on(f)
}

#[cfg(target_family = "wasm")]
fn block_on(f: impl Future<Output = ()> + '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 {
(
Expand Down Expand Up @@ -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();
Expand All @@ -107,7 +102,7 @@ test! {
}
}

test!{
test! {
async fn poll_across_tasks() {
let start = Instant::now();
let (sender, receiver) = async_channel::bounded(1);
Expand Down Expand Up @@ -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))));

Expand Down

0 comments on commit 4ffc395

Please sign in to comment.