Skip to content

Commit

Permalink
time: update deadline on removal in DelayQueue (tokio-rs#4178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn authored and Oliver Giersch committed Oct 28, 2021
1 parent d2d2341 commit 5c6e839
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tokio-util/src/time/delay_queue.rs
Expand Up @@ -498,9 +498,20 @@ impl<T> DelayQueue<T> {
/// # }
/// ```
pub fn remove(&mut self, key: &Key) -> Expired<T> {
let prev_deadline = self.next_deadline();

self.remove_key(key);
let data = self.slab.remove(key.index);

let next_deadline = self.next_deadline();
if prev_deadline != next_deadline {
match (next_deadline, &mut self.delay) {
(None, _) => self.delay = None,
(Some(deadline), Some(delay)) => delay.as_mut().reset(deadline),
(Some(deadline), None) => self.delay = Some(Box::pin(sleep_until(deadline))),
}
}

Expired {
key: Key::new(key.index),
data: data.inner,
Expand Down
10 changes: 10 additions & 0 deletions tokio-util/tests/time_delay_queue.rs
Expand Up @@ -630,6 +630,16 @@ async fn insert_in_past_after_poll_fires_immediately() {
assert_eq!(entry, "bar");
}

#[tokio::test]
async fn delay_queue_poll_expired_when_empty() {
let mut delay_queue = task::spawn(DelayQueue::new());
let key = delay_queue.insert(0, std::time::Duration::from_secs(10));
assert_pending!(poll!(delay_queue));

delay_queue.remove(&key);
assert!(assert_ready!(poll!(delay_queue)).is_none());
}

fn ms(n: u64) -> Duration {
Duration::from_millis(n)
}

0 comments on commit 5c6e839

Please sign in to comment.