Skip to content

Commit 04a349f

Browse files
committed
test(auth): cover token expiry boundaries in refresh
Assert which token the initiating caller receives immediately before, at, and after expiry so both surviving comparison mutations fail. Keep cfg(test) separate on the cancellation and soak modules so cargo-mutants recognises the test harnesses without weakening production mutation coverage. The full PR mutation sweep passes with 5 caught and 9 unviable mutants, no survivors or timeouts. Refs #1170.
1 parent 9e1bd85 commit 04a349f

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

‎packages/stack-auth/src/auto_refresh.rs‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,55 @@ mod expiry_crossing_regression {
19361936
}
19371937
}
19381938

1939+
#[tokio::test(start_paused = true)]
1940+
async fn initiating_caller_returns_the_old_token_only_before_its_expiry() {
1941+
for (elapsed, expected) in [
1942+
(9, "expiring-soon"),
1943+
(10, "refreshed-token"),
1944+
(11, "refreshed-token"),
1945+
] {
1946+
let clock = TestClock::new(1_000_000);
1947+
let started = Arc::new(Notify::new());
1948+
let gate = Arc::new(Notify::new());
1949+
let calls = Arc::new(AtomicUsize::new(0));
1950+
let strategy = Arc::new(AutoRefresh::with_token_and_clock(
1951+
GatedRefresher {
1952+
started: Arc::clone(&started),
1953+
gate: Arc::clone(&gate),
1954+
calls: Arc::clone(&calls),
1955+
refreshed_expires_at: clock.now() + 3600,
1956+
},
1957+
make_token("expiring-soon", clock.now() + 10),
1958+
clock.shared(),
1959+
));
1960+
let caller = tokio::spawn({
1961+
let strategy = Arc::clone(&strategy);
1962+
async move { strategy.get_token().await }
1963+
});
1964+
tokio::time::timeout(std::time::Duration::from_secs(1), started.notified())
1965+
.await
1966+
.expect("the usable token must start a refresh");
1967+
1968+
// Finish the exchange one second before, exactly at, and one
1969+
// second after the old token's expiry. Only the first case may
1970+
// return the old token to the caller that initiated the refresh.
1971+
clock.advance(elapsed);
1972+
gate.notify_one();
1973+
let returned = tokio::time::timeout(std::time::Duration::from_secs(1), caller)
1974+
.await
1975+
.expect("the initiating caller must finish")
1976+
.unwrap()
1977+
.unwrap();
1978+
assert_eq!(returned.as_str(), expected, "elapsed={elapsed}");
1979+
assert_eq!(
1980+
strategy.get_token().await.unwrap().as_str(),
1981+
"refreshed-token",
1982+
"the refreshed token must be cached in every case"
1983+
);
1984+
assert_eq!(calls.load(Ordering::SeqCst), 1);
1985+
}
1986+
}
1987+
19391988
#[tokio::test]
19401989
async fn waiters_wait_for_refresh_when_token_crosses_expiry() {
19411990
let clock = TestClock::new(1_000_000);
@@ -2357,10 +2406,14 @@ mod expiry_crossing_regression {
23572406
}
23582407
}
23592408

2360-
#[cfg(all(test, feature = "http", not(target_arch = "wasm32")))]
2409+
// Keep `test` as a standalone cfg so cargo-mutants recognises these modules
2410+
// as test harnesses and mutates the refresh engine they exercise instead.
2411+
#[cfg(test)]
2412+
#[cfg(all(feature = "http", not(target_arch = "wasm32")))]
23612413
#[path = "auto_refresh/tests/cancellation.rs"]
23622414
mod cancellation_tests;
23632415

2364-
#[cfg(all(test, feature = "http", not(target_arch = "wasm32")))]
2416+
#[cfg(test)]
2417+
#[cfg(all(feature = "http", not(target_arch = "wasm32")))]
23652418
#[path = "auto_refresh/tests/soak.rs"]
23662419
mod cancellation_soak;

0 commit comments

Comments
 (0)