mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 05:50:04 +08:00
A caller could sample the clock at 1000 ms and pause while another caller was admitted at 2000 ms. On resuming, the first caller could load the newer last_run_ms, mistake its stale sample for a clock rollback, and replace 2000 with 1000. This reopened the 100 ms period and let another check at 2000 ms pass. Load last_run_ms before sampling the clock so the stale caller's compare-exchange fails if a newer admission has changed that value. Preserve sequential clock rollback behavior and the existing atomic orderings. The period check also added the period to last_run_ms. A last admission at u64::MAX - 55 with a 56 ms period overflowed on the next check: debug builds panicked and release builds wrapped the deadline to zero, admitting a duplicate timestamp. Compare elapsed time against the period only after confirming the clock has not moved backward, which makes the subtraction safe and preserves the exact period boundary. Enable the stale-sample and overflow regressions, keep the saved Proptest seed, and add Loom coverage for advancing time and multiple stale callers plus a deterministic test at u64::MAX.
212 lines
7.1 KiB
Rust
212 lines
7.1 KiB
Rust
#![cfg(not(loom))]
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
use std::time::Duration;
|
|
|
|
use clash_verge_limiter::{Clock, Limiter};
|
|
use proptest::prelude::*;
|
|
use proptest::test_runner::FileFailurePersistence;
|
|
|
|
const CASES: u32 = 10_000;
|
|
const MAX_PERIOD_MS: u64 = 100_000;
|
|
const MAX_START_MS: u64 = 1_000_000;
|
|
const MAX_DELTA_MS: u64 = 10_000;
|
|
|
|
/// Returns one clock value per call, in order. The test supplies exactly as
|
|
/// many values as the limiter will read.
|
|
struct SequenceClock {
|
|
values: Vec<u64>,
|
|
next: AtomicUsize,
|
|
}
|
|
|
|
impl SequenceClock {
|
|
const fn new(values: Vec<u64>) -> Self {
|
|
Self {
|
|
values,
|
|
next: AtomicUsize::new(0),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Clock for SequenceClock {
|
|
fn now_ms(&self) -> u64 {
|
|
let index = self.next.fetch_add(1, Ordering::SeqCst);
|
|
self.values[index]
|
|
}
|
|
}
|
|
|
|
fn timestamps(start: u64, deltas: &[u64]) -> Vec<u64> {
|
|
let mut now = start;
|
|
let mut values = Vec::with_capacity(deltas.len() + 1);
|
|
values.push(now);
|
|
for delta in deltas {
|
|
now += delta;
|
|
values.push(now);
|
|
}
|
|
values
|
|
}
|
|
|
|
proptest! {
|
|
#![proptest_config(ProptestConfig {
|
|
cases: CASES,
|
|
failure_persistence: Some(Box::new(FileFailurePersistence::Direct(concat!(
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
"/tests/properties.proptest-regressions"
|
|
)))),
|
|
..ProptestConfig::default()
|
|
})]
|
|
|
|
/// For positive periods and nondecreasing time, admission is equivalent to
|
|
/// reaching the next allowed timestamp after the previous admission.
|
|
#[test]
|
|
fn sequential_results_match_the_next_allowed_time_contract(
|
|
period in 1u64..=MAX_PERIOD_MS,
|
|
start in MAX_PERIOD_MS..=MAX_START_MS,
|
|
deltas in prop::collection::vec(0u64..=MAX_DELTA_MS, 0..64),
|
|
) {
|
|
let times = timestamps(start, &deltas);
|
|
let clock = SequenceClock::new(times.clone());
|
|
let limiter = Limiter::new(Duration::from_millis(period), &clock);
|
|
let mut last_admitted = None;
|
|
|
|
for now in times {
|
|
let expected = match last_admitted {
|
|
None => true,
|
|
Some(last) => now >= last + period,
|
|
};
|
|
let actual = limiter.check();
|
|
prop_assert!(
|
|
actual == expected,
|
|
"period={} now={} last={:?}",
|
|
period,
|
|
now,
|
|
last_admitted
|
|
);
|
|
if actual {
|
|
last_admitted = Some(now);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// An admission at the period boundary is accepted, while a repeated or
|
|
/// pre-boundary check is rejected.
|
|
#[test]
|
|
fn period_boundary_and_repeated_checks_follow_the_period(
|
|
period in 1u64..=MAX_PERIOD_MS,
|
|
start in MAX_PERIOD_MS..=MAX_START_MS,
|
|
) {
|
|
let boundary = start + period;
|
|
let clock = SequenceClock::new(vec![start, start, boundary - 1, boundary]);
|
|
let limiter = Limiter::new(Duration::from_millis(period), &clock);
|
|
|
|
prop_assert!(limiter.check(), "first check at {}", start);
|
|
prop_assert!(!limiter.check(), "repeated check at {}", start);
|
|
prop_assert!(!limiter.check(), "pre-boundary check at {}", boundary - 1);
|
|
prop_assert!(limiter.check(), "boundary check at {}", boundary);
|
|
}
|
|
|
|
/// Any two admitted timestamps in a monotonic sequence are separated by at
|
|
/// least one configured period.
|
|
#[test]
|
|
fn admitted_timestamps_are_at_least_one_period_apart(
|
|
period in 1u64..=MAX_PERIOD_MS,
|
|
start in MAX_PERIOD_MS..=MAX_START_MS,
|
|
deltas in prop::collection::vec(0u64..=MAX_DELTA_MS, 0..64),
|
|
) {
|
|
let times = timestamps(start, &deltas);
|
|
let clock = SequenceClock::new(times.clone());
|
|
let limiter = Limiter::new(Duration::from_millis(period), &clock);
|
|
let mut admitted = Vec::new();
|
|
|
|
for now in times {
|
|
if limiter.check() {
|
|
admitted.push(now);
|
|
}
|
|
}
|
|
|
|
for pair in admitted.windows(2) {
|
|
prop_assert!(
|
|
pair[1] - pair[0] >= period,
|
|
"period={} admitted pair={:?}",
|
|
period,
|
|
pair
|
|
);
|
|
}
|
|
}
|
|
|
|
/// A rejected duplicate at the current admission timestamp must not change
|
|
/// the outcomes of later checks.
|
|
#[test]
|
|
fn rejected_checks_do_not_change_future_admissions(
|
|
period in 1u64..=MAX_PERIOD_MS,
|
|
start in MAX_PERIOD_MS..=MAX_START_MS,
|
|
deltas in prop::collection::vec(0u64..=MAX_DELTA_MS, 0..64),
|
|
) {
|
|
let base_times = timestamps(start, &deltas);
|
|
let base_clock = SequenceClock::new(base_times.clone());
|
|
let base = Limiter::new(Duration::from_millis(period), &base_clock);
|
|
let base_results: Vec<bool> = base_times.iter().map(|_| base.check()).collect();
|
|
|
|
let mut repeated_times = vec![start, start];
|
|
repeated_times.extend_from_slice(&base_times[1..]);
|
|
let repeated_len = repeated_times.len();
|
|
let repeated_clock = SequenceClock::new(repeated_times);
|
|
let repeated = Limiter::new(Duration::from_millis(period), &repeated_clock);
|
|
let repeated_results: Vec<bool> = (0..repeated_len).map(|_| repeated.check()).collect();
|
|
|
|
prop_assert!(repeated_results[0]);
|
|
prop_assert!(!repeated_results[1]);
|
|
prop_assert_eq!(&base_results[1..], &repeated_results[2..]);
|
|
}
|
|
|
|
/// Every sub-millisecond period is represented as zero milliseconds, so it
|
|
/// must produce the same admissions as `Duration::ZERO`. This deliberately
|
|
/// does not assert a policy for zero periods.
|
|
#[test]
|
|
fn zero_and_submillisecond_periods_are_equivalent(
|
|
nanos in 0u64..1_000_000,
|
|
start in 0u64..=MAX_START_MS,
|
|
deltas in prop::collection::vec(0u64..=MAX_DELTA_MS, 0..64),
|
|
) {
|
|
let times = timestamps(start, &deltas);
|
|
let zero_clock = SequenceClock::new(times.clone());
|
|
let zero = Limiter::new(Duration::ZERO, &zero_clock);
|
|
let submillisecond_clock = SequenceClock::new(times.clone());
|
|
let submillisecond =
|
|
Limiter::new(Duration::from_nanos(nanos), &submillisecond_clock);
|
|
|
|
for now in times {
|
|
prop_assert_eq!(
|
|
zero.check(),
|
|
submillisecond.check(),
|
|
"nanos={} now={}",
|
|
nanos,
|
|
now
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Regression: timestamp arithmetic must not overflow near `u64::MAX`.
|
|
#[test]
|
|
fn extreme_timestamp_and_period_do_not_overflow(
|
|
period in 1u64..=1_000,
|
|
start in (u64::MAX - 1_000)..=u64::MAX,
|
|
) {
|
|
let clock = SequenceClock::new(vec![start, start]);
|
|
let limiter = Limiter::new(Duration::from_millis(period), &clock);
|
|
|
|
prop_assert!(limiter.check());
|
|
prop_assert!(!limiter.check());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn max_timestamp_boundary_does_not_overflow() {
|
|
let clock = SequenceClock::new(vec![u64::MAX - 1, u64::MAX, u64::MAX]);
|
|
let limiter = Limiter::new(Duration::from_millis(1), &clock);
|
|
|
|
assert!(limiter.check());
|
|
assert!(limiter.check());
|
|
assert!(!limiter.check());
|
|
}
|