diff options
| -rw-r--r-- | src/time.rs | 31 | ||||
| -rw-r--r-- | tests/common/mod.rs | 20 | ||||
| -rw-r--r-- | tests/time.rs | 53 |
3 files changed, 97 insertions, 7 deletions
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<Utc> { + match pinned().and_then(|p| parse(&p)) { + Some(t) => t, + None => Utc::now(), } - fmt(Utc::now()) } fn fmt(t: DateTime<Utc>) -> String { @@ -58,12 +73,14 @@ pub fn delta_secs(a: &str, b: &str) -> Option<i64> { 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)) } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index de5f87e..58c6dae 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -155,6 +155,26 @@ impl Env { }) .unwrap() } + + /// The same for a text column, so a test can read a timestamp back. + pub fn query_str(&self, sql: &str, params: &[P]) -> Option<String> { + let sql = sql.to_string(); + let params: Vec<P> = params.to_vec(); + self.db + .read(move |c| { + let refs: Vec<&dyn rusqlite::ToSql> = params + .iter() + .map(|p| match p { + P::I(i) => i as &dyn rusqlite::ToSql, + P::S(s) => s as &dyn rusqlite::ToSql, + }) + .collect(); + use rusqlite::OptionalExtension; + Ok(c.query_row(&sql, rusqlite::params_from_iter(refs.iter()), |r| r.get::<_, String>(0)) + .optional()?) + }) + .unwrap() + } } /// A test parameter. Owned so it can cross the writer-actor channel. diff --git a/tests/time.rs b/tests/time.rs new file mode 100644 index 0000000..281d78a --- /dev/null +++ b/tests/time.rs @@ -0,0 +1,53 @@ +//! The clock helpers (§16) and the pinned clock the lease tests depend on (§6 m-1). + +mod common; + +use aur_build::db::{builds, BuildState}; +use aur_build::time; +use common::Env; + +#[test] +fn a_pinned_clock_moves_relative_arithmetic_with_it() { + // The finding: `now()` honored the pinned clock but `secs_ago` and + // `secs_ahead` read the wall clock, so a pinned test computed real-clock + // lease expiries and deadlines while comparing them against the pinned + // "now" — the two never met, and the test proved nothing about expiry. + time::pin("2026-01-01T00:00:00Z"); + assert_eq!(time::now(), "2026-01-01T00:00:00Z"); + assert_eq!(time::secs_ahead(90), "2026-01-01T00:01:30Z"); + assert_eq!(time::secs_ago(90), "2025-12-31T23:58:30Z"); + time::unpin(); +} + +#[test] +fn an_unpinned_clock_is_the_real_one() { + let now = time::now(); + assert_eq!(time::delta_secs(&now, &time::secs_ahead(60)), Some(60)); + assert_eq!(time::delta_secs(&now, &time::secs_ago(60)), Some(-60)); +} + +#[test] +fn a_lease_taken_under_a_pinned_clock_expires_against_that_clock() { + let env = Env::new(); + env.exec( + "INSERT INTO pkgbase (name, aur_url, created_at) VALUES ('x', 'u', '2026-01-01T00:00:00Z')", + &[], + ); + let pb = env.query_i64("SELECT id FROM pkgbase WHERE name='x'", &[]).unwrap(); + builds::enqueue(&env.db, "b1", pb, None, "manual", None, "default", 0).unwrap(); + + time::pin("2026-01-01T00:00:00Z"); + builds::claim(&env.db, "w1", "b1", 90).unwrap(); + assert_eq!( + env.query_str("SELECT lease_expires_at FROM build WHERE id='b1'", &[]).as_deref(), + Some("2026-01-01T00:01:30Z"), + "the lease must be 90 seconds after the pinned now, not the real one" + ); + // Still inside the lease: nothing to reap. + assert_eq!(builds::reap_expired(&env.db).unwrap(), 0); + // Ninety-one seconds of pinned time pass, and the reaper sees it. + time::pin("2026-01-01T00:01:31Z"); + assert_eq!(builds::reap_expired(&env.db).unwrap(), 1); + assert_eq!(builds::get(&env.db, "b1").unwrap().unwrap().state, BuildState::Queued); + time::unpin(); +} |
