Skip to content

Commit

Permalink
tokio-timer: fix DelayQueue bug when inserting shorter delay (#863)
Browse files Browse the repository at this point in the history
Reset the delay of the queue in case an item that expires sooner than the last inserted is put
into the queue.
  • Loading branch information
zaharidichev authored and carllerche committed Jan 24, 2019
1 parent fbad629 commit 12546d1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tokio-timer/src/delay_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,16 @@ impl<T> DelayQueue<T> {

self.insert_idx(when, key);

// Set a new delay if the current's deadline is later than the one of the new item
let should_set_delay = if let Some(ref delay) = self.delay {
let current_exp = self.normalize_deadline(delay.deadline());
current_exp > when
} else { false };

if should_set_delay {
self.delay = Some(self.handle.delay(self.start + Duration::from_millis(when)));
}

Key::new(key)
}

Expand Down
32 changes: 32 additions & 0 deletions tokio-timer/tests/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,35 @@ fn remove_expired_item() {
assert_eq!(entry.into_inner(), "foo");
})
}

#[test]
fn expires_before_last_insert() {
mocked(|timer, time| {
let mut queue = DelayQueue::new();
let mut task = MockTask::new();


let epoch = time.now();

queue.insert_at("foo", epoch + ms(10_000));

// Delay should be set to 8.192s here.
task.enter(|| {
assert_not_ready!(queue);
});

// Delay should be set to the delay of the new item here
queue.insert_at("bar", epoch + ms(600));

task.enter(|| {
assert_not_ready!(queue);
});

advance(timer, ms(600));

assert!(task.is_notified());
let entry = assert_ready!(queue).unwrap().into_inner();
assert_eq!(entry, "bar");

})
}

0 comments on commit 12546d1

Please sign in to comment.