Skip to content

Commit

Permalink
Fixing interval does not tick immediately in wasm (#20)
Browse files Browse the repository at this point in the history
whizsid authored Oct 17, 2024

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent 2e3d434 commit bea0749
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/tokio/interval.rs
Original file line number Diff line number Diff line change
@@ -29,12 +29,12 @@ impl Interval {
unsafe_pinned!(sleep: Sleep);

/// Creates a new interval which will fire at `dur` time into the future,
/// and will repeat every `dur` interval after
/// and will repeat every `dur` interval after. The first tick completes immediately.
///
/// The returned object will be bound to the default timer for this thread.
/// The default timer will be spun up in a helper thread on first use.
pub(crate) fn new(dur: Duration) -> Interval {
Interval::new_at(Instant::now() + dur, dur)
Interval::new_at(Instant::now(), dur)
}

/// Creates a new interval which will fire at the time specified by `at`,
20 changes: 14 additions & 6 deletions tests/web.rs
Original file line number Diff line number Diff line change
@@ -115,24 +115,29 @@ pub mod tokio_tests {
let mut interval = interval(Duration::from_millis(500));
let mut fut = interval.tick();
unsafe {
assert_eq!(Pin::new_unchecked(&mut fut).poll(&mut cx), Poll::Pending);
assert_ne!(Pin::new_unchecked(&mut fut).poll(&mut cx), Poll::Pending);
}
drop(fut);
let mut fut2 = interval.tick();
unsafe {
assert_eq!(Pin::new_unchecked(&mut fut2).poll(&mut cx), Poll::Pending);
}
advance(Duration::from_millis(501)).await;
unsafe {
assert!(matches!(
Pin::new_unchecked(&mut fut).poll(&mut cx),
Pin::new_unchecked(&mut fut2).poll(&mut cx),
Poll::Ready(_)
));
}
drop(fut);
let mut fut2 = interval.tick();
drop(fut2);
let mut fut3 = interval.tick();
unsafe {
assert_eq!(Pin::new_unchecked(&mut fut2).poll(&mut cx), Poll::Pending);
assert_eq!(Pin::new_unchecked(&mut fut3).poll(&mut cx), Poll::Pending);
}
advance(Duration::from_millis(501)).await;
unsafe {
assert!(matches!(
Pin::new_unchecked(&mut fut2).poll(&mut cx),
Pin::new_unchecked(&mut fut3).poll(&mut cx),
Poll::Ready(_)
));
}
@@ -186,6 +191,7 @@ pub mod tokio_tests {
let mut cx = Context::from_waker(waker);

let mut interval = interval(Duration::from_millis(500));
assert_ne!(interval.poll_tick(&mut cx), Poll::Pending);
assert_eq!(interval.poll_tick(&mut cx), Poll::Pending);
advance(Duration::from_millis(501)).await;
assert!(matches!(interval.poll_tick(&mut cx), Poll::Ready(_)));
@@ -223,6 +229,7 @@ pub mod tokio_tests {
let mut cx = Context::from_waker(waker);

let mut interval = interval(Duration::from_millis(500));
assert_ne!(interval.poll_tick(&mut cx), Poll::Pending);
assert_eq!(interval.poll_tick(&mut cx), Poll::Pending);
advance(Duration::from_millis(301)).await;
assert_eq!(interval.poll_tick(&mut cx), Poll::Pending);
@@ -267,6 +274,7 @@ pub mod tokio_tests {
assert!(matches!(interval.poll_tick(&mut cx), Poll::Ready(_)));
assert!(matches!(interval.poll_tick(&mut cx), Poll::Ready(_)));
assert!(matches!(interval.poll_tick(&mut cx), Poll::Ready(_)));
assert!(matches!(interval.poll_tick(&mut cx), Poll::Ready(_)));
assert_eq!(interval.poll_tick(&mut cx), Poll::Pending);
}

0 comments on commit bea0749

Please sign in to comment.