Skip to content

Commit

Permalink
fix: update interval next deadline (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Feb 10, 2025
1 parent 54fab10 commit 45d8adf
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
4 changes: 3 additions & 1 deletion fastimer-driver/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use std::time::Instant;
use fastimer::make_instant_from_now;
use fastimer_driver::binary_heap_driver;

#[track_caller]
fn assert_duration_eq(actual: Duration, expected: Duration) {
if expected.abs_diff(expected) > Duration::from_millis(5) {
if expected.abs_diff(actual) > Duration::from_millis(250) {
panic!("expected: {:?}, actual: {:?}", expected, actual);
}
}
Expand All @@ -42,6 +43,7 @@ fn test_binary_heap_driver() {
context.delay(Duration::from_secs(2)).await;
assert_duration_eq(now.elapsed(), Duration::from_secs(2));

let now = Instant::now();
let future = make_instant_from_now(Duration::from_secs(3));
let f1 = context.delay_until(future);
let f2 = context.delay_until(future);
Expand Down
1 change: 1 addition & 0 deletions fastimer/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ impl<D: MakeDelay> Interval<D> {
self.delay = Box::pin(self.make_delay.delay_util(next));

// Return the time when we were scheduled to tick
self.deadline = next;
Poll::Ready(timeout)
}
}
Expand Down
71 changes: 71 additions & 0 deletions fastimer/tests/interval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2024 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::time::Duration;
use std::time::Instant;

use fastimer::Interval;
use fastimer::MakeDelay;
use fastimer::MakeDelayExt;

#[track_caller]
fn assert_duration_eq(actual: Duration, expected: Duration) {
if expected.abs_diff(actual) > Duration::from_millis(250) {
panic!("expected: {:?}, actual: {:?}", expected, actual);
}
}

async fn assert_tick_about<D: MakeDelay>(interval: &mut Interval<D>, expected: Duration) {
let start = Instant::now();
interval.tick().await;
let elapsed = start.elapsed();
assert_duration_eq(elapsed, expected);
}

#[derive(Clone, Copy, Debug, Default)]
pub struct MakeTokioDelay;

impl MakeDelay for MakeTokioDelay {
type Delay = tokio::time::Sleep;

fn delay_util(&self, at: Instant) -> Self::Delay {
tokio::time::sleep_until(tokio::time::Instant::from_std(at))
}

fn delay(&self, duration: Duration) -> Self::Delay {
tokio::time::sleep(duration)
}
}

#[tokio::test]
async fn test_interval_ticks() {
let mut interval = MakeTokioDelay.interval(Duration::from_secs(1));
assert_tick_about(&mut interval, Duration::ZERO).await;

for _ in 0..5 {
assert_tick_about(&mut interval, Duration::from_secs(1)).await;
}
}

#[tokio::test]
async fn test_interval_at_ticks() {
let first_tick = Instant::now() + Duration::from_secs(2);

let mut interval = MakeTokioDelay.interval_at(first_tick, Duration::from_secs(1));
assert_tick_about(&mut interval, Duration::from_secs(2)).await;

for _ in 0..5 {
assert_tick_about(&mut interval, Duration::from_secs(1)).await;
}
}

0 comments on commit 45d8adf

Please sign in to comment.