Skip to content

Commit

Permalink
remove deadline check in reset_at
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-timofei committed Jul 28, 2023
1 parent 033aaed commit be4406b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
7 changes: 1 addition & 6 deletions tokio/src/time/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,6 @@ impl Interval {
}

/// Resets the interval to a [`crate::time::Instant`] deadline.
/// If deadline is in the past it behaves like `reset_immediately`.
///
/// This method ignores [`MissedTickBehavior`] strategy.
///
Expand Down Expand Up @@ -611,11 +610,7 @@ impl Interval {
/// }
/// ```
pub fn reset_at(&mut self, deadline: Instant) {
if deadline > Instant::now() {
self.delay.as_mut().reset(deadline);
} else {
self.reset_immediately();
}
self.delay.as_mut().reset(deadline);
}

/// Returns the [`MissedTickBehavior`] strategy currently being used.
Expand Down
36 changes: 36 additions & 0 deletions tokio/tests/time_interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,42 @@ async fn reset_after() {
check_interval_poll!(i, start, 721);
}

#[tokio::test(start_paused = true)]
async fn reset_at() {
let start = Instant::now();

// This is necessary because the timer is only so granular, and in order for
// all our ticks to resolve, the time needs to be 1ms ahead of what we
// expect, so that the runtime will see that it is time to resolve the timer
time::advance(ms(1)).await;

let mut i = task::spawn(time::interval_at(start, ms(300)));

check_interval_poll!(i, start, 0);

time::advance(ms(100)).await;
check_interval_poll!(i, start);

time::advance(ms(200)).await;
check_interval_poll!(i, start, 300);

time::advance(ms(100)).await;
check_interval_poll!(i, start);

i.reset_at(Instant::now() + Duration::from_millis(40));

// We add one because when using `reset` method, `Interval` adds the
// `period` from `Instant::now()`, which will always be off by one
time::advance(ms(40)).await;
check_interval_poll!(i, start, 441);

time::advance(ms(100)).await;
check_interval_poll!(i, start);

time::advance(ms(200)).await;
check_interval_poll!(i, start, 741);
}

fn poll_next(interval: &mut task::Spawn<time::Interval>) -> Poll<Instant> {
interval.enter(|cx, mut interval| interval.poll_tick(cx))
}
Expand Down

0 comments on commit be4406b

Please sign in to comment.