diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/common/mod.rs | 20 | ||||
| -rw-r--r-- | tests/time.rs | 53 |
2 files changed, 73 insertions, 0 deletions
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(); +} |
