Skip to content

Commit

Permalink
On same deadline, items queued first have priority
Browse files Browse the repository at this point in the history
  • Loading branch information
amcandio committed Aug 12, 2024
1 parent 5ba1370 commit 10465dc
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/sources/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,11 @@ impl TimerWheel {
impl std::cmp::Ord for TimeoutData {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// earlier values have priority
self.deadline.cmp(&other.deadline).reverse()
// earlier values have priority, on same deadline items queued first have priority
self.deadline
.cmp(&other.deadline)
.then_with(|| self.counter.cmp(&other.counter))
.reverse()
}
}

Expand Down Expand Up @@ -381,6 +384,31 @@ mod tests {
use crate::*;
use std::time::Duration;

#[test]
fn timeout_data_cmp() {
let now = Instant::now();
let timeout_now_0 = TimeoutData {
deadline: now,
token: RefCell::new(None),
counter: 0,
};
let timeout_now_1 = TimeoutData {
deadline: now,
token: RefCell::new(None),
counter: 1,
};
assert!(timeout_now_0 > timeout_now_1);
assert!(timeout_now_1 < timeout_now_0);

let timeout_future_0 = TimeoutData {
deadline: now + Duration::from_millis(100),
token: RefCell::new(None),
counter: 0,
};
assert!(timeout_now_1 > timeout_future_0);
assert!(timeout_future_0 < timeout_now_1);
}

#[test]
fn simple_timer() {
let mut event_loop = EventLoop::try_new().unwrap();
Expand Down

0 comments on commit 10465dc

Please sign in to comment.