From 6f2f8404db4630cd5885227443b8995d0b0ee8a7 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Sun, 6 Dec 2026 16:02:50 +0800 Subject: [PATCH] test(task): start only keys the queue returned in the queue tests Two queue tests called `start` on an armed key that `next_expired` had not returned, which the documented contract rules out. They now let the key expire first and take it from the queue. --- crates/clash-verge-task/tests/timers.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/clash-verge-task/tests/timers.rs b/crates/clash-verge-task/tests/timers.rs index bf349f538..5b2a9e3f0 100644 --- a/crates/clash-verge-task/tests/timers.rs +++ b/crates/clash-verge-task/tests/timers.rs @@ -139,19 +139,21 @@ async fn apply_reports_removals_first_and_reenable_changes_nothing() { timers.apply(HashMap::from([ ("a".to_owned(), plan(MINUTE, MINUTE)), ("b".to_owned(), plan(MINUTE, 2 * MINUTE)), - ("c".to_owned(), plan(MINUTE, 3 * MINUTE)), + ("c".to_owned(), plan(10 * MINUTE, 3 * MINUTE)), ])); let mut tail = timers.apply(HashMap::from([ ("a".to_owned(), plan(2 * MINUTE, 2 * MINUTE)), - ("c".to_owned(), plan(MINUTE, 3 * MINUTE)), - ("d".to_owned(), plan(MINUTE, MINUTE)), + ("c".to_owned(), plan(10 * MINUTE, 3 * MINUTE)), + ("d".to_owned(), plan(4 * MINUTE, MINUTE)), ])); assert_eq!(tail.remove(0), TimerChange::Removed("b".to_owned())); assert_eq!(tail.len(), 2); // HashMap order decides which of these comes first assert!(tail.contains(&TimerChange::Added("d".to_owned()))); assert!(tail.contains(&TimerChange::Updated("a".to_owned()))); + tokio::time::advance(2 * MINUTE).await; + assert_eq!(take_expired(&mut timers).await.as_deref(), Some("a")); assert!(timers.start(&"a".to_owned())); - let retired = timers.apply(HashMap::from([("c".to_owned(), plan(MINUTE, 3 * MINUTE))])); + let retired = timers.apply(HashMap::from([("c".to_owned(), plan(10 * MINUTE, 3 * MINUTE))])); assert_eq!(retired.len(), 2, "the running key is retired and the idle one removed"); assert!(retired.contains(&TimerChange::Retired("a".to_owned()))); assert!(retired.contains(&TimerChange::Removed("d".to_owned()))); @@ -159,7 +161,7 @@ async fn apply_reports_removals_first_and_reenable_changes_nothing() { timers .apply(HashMap::from([ ("a".to_owned(), plan(2 * MINUTE, 2 * MINUTE)), - ("c".to_owned(), plan(MINUTE, 3 * MINUTE)), + ("c".to_owned(), plan(10 * MINUTE, 3 * MINUTE)), ])) .is_empty() ); @@ -184,6 +186,8 @@ async fn finishing_an_unknown_or_cleared_key_does_nothing() { let mut timers = KeyedTimers::new(); assert!(!timers.finish(&"missing".to_owned())); timers.apply(HashMap::from([("a".to_owned(), plan(MINUTE, MINUTE))])); + tokio::time::advance(MINUTE).await; + assert_eq!(take_expired(&mut timers).await.as_deref(), Some("a")); assert!(timers.start(&"a".to_owned())); timers.clear(); assert!(!timers.finish(&"a".to_owned()));