From e7922f3dd1d985dcb993b624146b2492183f3a11 Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 17:04:57 +0000 Subject: time: relative arithmetic honors the pinned clock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `now()` returned the pinned instant when a test had pinned one, but `secs_ago`/`secs_ahead` called `Utc::now()` directly (m-1). Anything built from them — every lease expiry, every `next_check_at`, every cooldown cutoff — was therefore computed against the real wall clock and then compared against the pinned one. A test that pinned 2026-01-01 and took a 90-second lease got an expiry 90 seconds after *today*, so the reaper comparing `lease_expires_at < now()` saw a lease that would not expire for a century, or one that had expired millions of seconds ago, depending on which way the pin pointed. The test still passed, because nothing asserted on the number. Both now go through a `base()` that reads the pinned instant, so a pinned test computes and compares on the same clock. `tests/time.rs` pins the arithmetic directly and then does the thing the bug actually broke: claim a lease under a pinned clock, assert the expiry is 90 seconds after the pinned now, assert the reaper sees nothing yet, advance the pinned clock 91 seconds, and assert it reaps. --- src/time.rs | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/time.rs b/src/time.rs index 69fbc3c..88a27c7 100644 --- a/src/time.rs +++ b/src/time.rs @@ -10,10 +10,25 @@ use chrono::{DateTime, SecondsFormat, TimeZone, Utc}; /// Current time as a canonical DB timestamp: `2026-11-17T20:43:24Z`. pub fn now() -> String { // A test may pin the clock (see `pin`); production reads the real clock. - if let Some(pinned) = pinned() { - return pinned; + match pinned() { + Some(pinned) => pinned, + None => fmt(Utc::now()), + } +} + +/// The instant relative arithmetic should use. +/// +/// This is what makes `secs_ago` and `secs_ahead` honest under a pinned clock +/// (§6 m-1): a test that pins the clock and then asks for a 90-second lease +/// means a lease expiring 90 seconds after *the pinned instant*, not after the +/// real one. Reading the wall clock here instead would hand a pinned test a +/// lease that had already expired — or would not expire for a century — and the +/// test would pass while proving nothing about expiry at all. +fn base() -> DateTime { + match pinned().and_then(|p| parse(&p)) { + Some(t) => t, + None => Utc::now(), } - fmt(Utc::now()) } fn fmt(t: DateTime) -> String { @@ -58,12 +73,14 @@ pub fn delta_secs(a: &str, b: &str) -> Option { Some(parse(b)?.signed_duration_since(parse(a)?).num_seconds()) } -/// A timestamp `secs` seconds in the past, canonical format. +/// A timestamp `secs` seconds in the past, canonical format. Honors the pinned +/// clock — see [`base`]. pub fn secs_ago(secs: i64) -> String { - fmt(Utc::now() - chrono::Duration::seconds(secs)) + fmt(base() - chrono::Duration::seconds(secs)) } -/// A timestamp `secs` seconds in the future, canonical format. +/// A timestamp `secs` seconds in the future, canonical format. Honors the +/// pinned clock — see [`base`]. pub fn secs_ahead(secs: i64) -> String { - fmt(Utc::now() + chrono::Duration::seconds(secs)) + fmt(base() + chrono::Duration::seconds(secs)) } -- cgit v1.2.3