time: wake DelayQueue when cleared (#8320)

This commit is contained in:
Minh Vu
2026-08-09 16:45:35 +02:00
committed by GitHub
parent 7f86b2ace5
commit ed25636141
2 changed files with 22 additions and 0 deletions
+8
View File
@@ -1031,10 +1031,18 @@ impl<T> DelayQueue<T> {
/// # }
/// ```
pub fn clear(&mut self) {
let had_entries = !self.slab.is_empty();
self.slab.clear();
self.expired = Stack::default();
self.wheel = Wheel::new();
self.delay = None;
if had_entries {
if let Some(waker) = self.waker.take() {
waker.wake();
}
}
}
/// Returns the number of elements the queue can hold without reallocating.
+14
View File
@@ -915,6 +915,20 @@ async fn wake_after_remove_last() {
assert!(assert_ready!(poll!(queue)).is_none());
}
#[tokio::test(start_paused = true)]
async fn wake_after_clear() {
let mut queue = task::spawn(DelayQueue::new());
queue.insert("foo", ms(1000));
assert_pending!(poll!(queue));
assert!(!queue.is_woken());
queue.clear();
assert!(queue.is_woken());
assert!(assert_ready!(poll!(queue)).is_none());
}
fn ms(n: u64) -> Duration {
Duration::from_millis(n)
}