Skip to content

Commit

Permalink
Merge commit 'refs/pull/258/head' of github.com:infinyon/future-aio i…
Browse files Browse the repository at this point in the history
…nto nacardin/deps
  • Loading branch information
nacardin committed Jul 11, 2024
2 parents 0c5a462 + c990b24 commit 4aa9dc0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Release Notes

## Unreleased

* Timer sleeper only fires once ([#258](https://github.com/infinyon/future-aio/pull/258))
* Bump tokio from 1.33.0 to 1.37.0
* Bump async-lock from 2.8.0 to 3.3.0
* Bump async-fs from 1.6.0 to 2.1.2
* Bump bytes from 1.4.0 to 1.6.0
* Bump once_cell from 1.18.0 to 1.19.0
* Bump async-net from 1.7.0 to 2.0.0
* Bump async-native-tls from 0.4.0 to 0.5.0
* Bump memmap2 from 0.5.10 to 0.9.3
* Bump async-io from 1.13.0 to 2.3.2

## 0.5.0
* Move to `memmap2` because `memmap` is unmaintained.
* Enable `rustls` on `windows`
Expand Down
31 changes: 12 additions & 19 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,18 @@ mod inner {

/// same as `after` but return () to make it compatible as previous
pub fn sleep(duration: Duration) -> Sleeper {
Sleeper {
timer: after(duration),
has_fired: false,
}
Sleeper(after(duration))
}

#[pin_project]
pub struct Sleeper {
#[pin]
timer: Timer,
has_fired: bool,
}
pub struct Sleeper(#[pin] Timer);

impl Future for Sleeper {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();

if *this.has_fired {
return Poll::Ready(());
}

if this.timer.poll(cx).is_ready() {
*this.has_fired = true;
if this.0.poll(cx).is_ready() {
Poll::Ready(())
} else {
Poll::Pending
Expand Down Expand Up @@ -90,7 +77,8 @@ mod test {
/// test timer loop
#[fluvio_future::test]
async fn test_sleep() {
let mut sleep_count: u16 = 0;
let mut times_fired: u16 = 0;
let mut times_not_fired: u16 = 0;
let time_now = Instant::now();

let mut sleep_ft = sleep(Duration::from_millis(10));
Expand All @@ -99,9 +87,13 @@ mod test {
select! {
_ = &mut sleep_ft => {
// fire everytime but won't make cause more delay than initial 10 ms
sleep_count += 1;
times_fired += 1;
debug!("timer fired");
}

_ = sleep(Duration::from_millis(40)) => {
times_not_fired += 1;
}
}
}

Expand All @@ -111,6 +103,7 @@ mod test {

assert!(elapsed < Duration::from_millis(1000)); // make this generous to handle slow CI
assert!(elapsed > Duration::from_millis(10));
assert_eq!(sleep_count, 10);
assert_eq!(times_fired, 1);
assert_eq!(times_not_fired, 9);
}
}

0 comments on commit 4aa9dc0

Please sign in to comment.