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. --- tests/time.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/time.rs (limited to 'tests/time.rs') 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(); +} -- cgit v1.2.3