Files
clash-verge-rev/crates/clash-verge-task/tests/timers.rs
T
Tunglies 725c5941d0 test(task): tell the first delay from the interval after a change
The case used a plan whose first delay equalled its interval, so re-arming at the full interval passed as well. In production the first delay is the remaining time and the interval is the full period, so the test now uses different values.
2026-12-06 16:03:28 +08:00

222 lines
8.9 KiB
Rust

use clash_verge_task::timers::{KeyedTimers, TimerChange, TimerPlan};
use std::{collections::HashMap, task::Poll, time::Duration};
const MINUTE: Duration = Duration::from_secs(60);
const fn plan(first_delay: Duration, interval: Duration) -> TimerPlan {
TimerPlan { first_delay, interval }
}
/// Polled, not awaited: a not-yet-due entry would park the test on the paused clock.
async fn take_expired(timers: &mut KeyedTimers<String>) -> Option<String> {
match futures::poll!(Box::pin(timers.next_expired())) {
Poll::Ready(Some(key)) => Some(key),
_ => None,
}
}
/// Empty queue reports `Ready(None)`, not `Pending`, so both count as "nothing due".
async fn nothing_due(timers: &mut KeyedTimers<String>) -> bool {
matches!(
futures::poll!(Box::pin(timers.next_expired())),
Poll::Pending | Poll::Ready(None)
)
}
#[tokio::test(start_paused = true)]
async fn queue_fires_at_the_remaining_time_then_waits_a_whole_interval() {
let mut timers = KeyedTimers::new();
// 1439min into a 1440min interval: due in a minute, not a day.
timers.apply(HashMap::from([("remote".to_owned(), plan(MINUTE, 1440 * MINUTE))]));
tokio::time::advance(Duration::from_secs(61)).await;
assert_eq!(
take_expired(&mut timers).await.as_deref(),
Some("remote"),
"the raised interval must fire one minute in, not a full day in"
);
// After it reports back, the next one is a whole interval away.
assert!(timers.start(&"remote".to_owned()));
assert!(!timers.finish(&"remote".to_owned()));
tokio::time::advance(Duration::from_secs(1439 * 60)).await;
assert!(
nothing_due(&mut timers).await,
"a finished task waits the whole interval, not the remainder of the old one"
);
tokio::time::advance(Duration::from_secs(120)).await;
assert_eq!(take_expired(&mut timers).await.as_deref(), Some("remote"));
}
#[tokio::test(start_paused = true)]
async fn disabling_a_profile_mid_update_cannot_start_a_second_one() {
let mut timers = KeyedTimers::new();
// Overdue: every re-insert would land at zero delay.
let overdue = || plan(Duration::ZERO, 60 * MINUTE);
timers.apply(HashMap::from([("local".to_owned(), overdue())]));
assert_eq!(take_expired(&mut timers).await.as_deref(), Some("local"));
assert!(timers.start(&"local".to_owned()));
// Toggled off then on while the run is still in flight.
assert_eq!(
timers.apply(HashMap::new()),
vec![TimerChange::Retired("local".to_owned())]
);
assert!(
timers
.apply(HashMap::from([("local".to_owned(), overdue())]))
.is_empty(),
"the entry must survive the toggle so the in-flight run is still accounted for"
);
assert!(
!timers.start(&"local".to_owned()),
"a second run while the first is in flight is refused"
);
assert!(
nothing_due(&mut timers).await,
"re-enabling must not queue a second run alongside the one already going"
);
// Original run reports back, re-arms exactly once.
assert!(!timers.finish(&"local".to_owned()));
tokio::time::advance(Duration::from_secs(3601)).await;
assert_eq!(take_expired(&mut timers).await.as_deref(), Some("local"));
assert!(nothing_due(&mut timers).await, "exactly one key, no orphan");
}
#[tokio::test(start_paused = true)]
async fn a_profile_disabled_mid_update_is_dropped_when_it_reports_back() {
let mut timers = KeyedTimers::new();
timers.apply(HashMap::from([("local".to_owned(), plan(Duration::ZERO, 60 * MINUTE))]));
assert_eq!(take_expired(&mut timers).await.as_deref(), Some("local"));
assert!(timers.start(&"local".to_owned()));
assert_eq!(
timers.apply(HashMap::new()),
vec![TimerChange::Retired("local".to_owned())]
);
assert_eq!(timers.interval("local"), None, "a retired timer reports no deadline");
assert!(
timers.finish(&"local".to_owned()),
"a task retired mid-update must not outlive the update"
);
assert_eq!(timers.interval(&"local".to_owned()), None);
tokio::time::advance(Duration::from_secs(7200)).await;
assert!(nothing_due(&mut timers).await, "and must never be scheduled again");
// Re-enabling starts from scratch; a leftover entry would report no change.
assert_eq!(
timers.apply(HashMap::from([("local".to_owned(), plan(Duration::ZERO, 60 * MINUTE))])),
vec![TimerChange::Added("local".to_owned())]
);
}
#[tokio::test(start_paused = true)]
async fn an_idle_timer_with_a_changed_interval_fires_after_the_new_first_delay() {
let mut timers = KeyedTimers::new();
timers.apply(HashMap::from([("a".to_owned(), plan(60 * MINUTE, 60 * MINUTE))]));
assert_eq!(
timers.apply(HashMap::from([("a".to_owned(), plan(MINUTE, 90 * MINUTE))])),
vec![TimerChange::Updated("a".to_owned())]
);
assert!(nothing_due(&mut timers).await, "a changed timer does not fire at once");
tokio::time::advance(MINUTE).await;
assert_eq!(
take_expired(&mut timers).await.as_deref(),
Some("a"),
"the old fire time an hour out must not survive the change"
);
assert!(nothing_due(&mut timers).await);
}
#[tokio::test(start_paused = true)]
async fn apply_reports_removals_first_and_reenable_changes_nothing() {
let mut timers = KeyedTimers::new();
timers.apply(HashMap::from([
("a".to_owned(), plan(MINUTE, MINUTE)),
("b".to_owned(), plan(MINUTE, 2 * MINUTE)),
("c".to_owned(), plan(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)),
]));
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())));
assert!(timers.start(&"a".to_owned()));
let retired = timers.apply(HashMap::from([("c".to_owned(), plan(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())));
assert!(
timers
.apply(HashMap::from([
("a".to_owned(), plan(2 * MINUTE, 2 * MINUTE)),
("c".to_owned(), plan(MINUTE, 3 * MINUTE)),
]))
.is_empty()
);
assert!(!timers.finish(&"a".to_owned()));
assert_eq!(timers.interval(&"a".to_owned()), Some(2 * MINUTE));
}
#[tokio::test(start_paused = true)]
async fn interval_queries_answer_before_initialization() {
let mut timers = KeyedTimers::new();
timers.apply(HashMap::from([("a".to_owned(), plan(MINUTE, MINUTE))]));
assert_eq!(timers.interval(&"a".to_owned()), Some(MINUTE));
assert_eq!(timers.interval("a"), Some(MINUTE));
assert_eq!(timers.interval("b"), None);
assert_eq!(timers.apply(HashMap::new()), vec![TimerChange::Removed("a".to_owned())]);
assert_eq!(timers.interval("a"), None);
assert!(nothing_due(&mut timers).await);
}
#[tokio::test(start_paused = true)]
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))]));
assert!(timers.start(&"a".to_owned()));
timers.clear();
assert!(!timers.finish(&"a".to_owned()));
assert_eq!(timers.interval(&"a".to_owned()), None);
tokio::time::advance(24 * 60 * MINUTE).await;
assert!(nothing_due(&mut timers).await);
}
#[tokio::test(start_paused = true)]
async fn an_interval_changed_during_a_run_is_reported_and_used_on_finish() {
let mut timers = KeyedTimers::new();
timers.apply(HashMap::from([("a".to_owned(), plan(Duration::ZERO, MINUTE))]));
assert_eq!(take_expired(&mut timers).await.as_deref(), Some("a"));
assert!(timers.start(&"a".to_owned()));
assert_eq!(
timers.apply(HashMap::from([("a".to_owned(), plan(5 * MINUTE, 5 * MINUTE))])),
vec![TimerChange::Updated("a".to_owned())]
);
assert!(nothing_due(&mut timers).await);
assert_eq!(timers.interval(&"a".to_owned()), Some(5 * MINUTE));
// The new plan's first delay is not a fire time while the run is still in flight.
tokio::time::advance(5 * MINUTE).await;
assert!(
nothing_due(&mut timers).await,
"a changed interval must not queue a second run beside the one in flight"
);
assert!(!timers.finish(&"a".to_owned()));
tokio::time::advance(5 * MINUTE - Duration::from_secs(1)).await;
assert!(nothing_due(&mut timers).await);
tokio::time::advance(Duration::from_secs(1)).await;
assert_eq!(take_expired(&mut timers).await.as_deref(), Some("a"));
assert!(nothing_due(&mut timers).await, "exactly one run after the change");
}