summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-18 17:04:57 +0000
committeraur-build <aur-build@localhost>2026-11-18 17:04:57 +0000
commite7922f3dd1d985dcb993b624146b2492183f3a11 (patch)
treeb385961b9799c66240c9655a65236133199d0a71
parent41fa1684de8eb13c5b76c3a3e174fece04ac5afa (diff)
downloadaur-build-e7922f3dd1d985dcb993b624146b2492183f3a11.tar.gz
aur-build-e7922f3dd1d985dcb993b624146b2492183f3a11.zip
time: relative arithmetic honors the pinned clock
`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.
-rw-r--r--src/time.rs31
-rw-r--r--tests/common/mod.rs20
-rw-r--r--tests/time.rs53
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();
+}