Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On same deadline, items queued first have priority #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use proper capitalization and punctuation for comments.

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
Loading