From d823e6b15ce5b5a8e48cadda467f7cf5456b5f4c Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 13:29:01 +0100 Subject: [PATCH 1/2] test(audit): audit tests wait their turn for the chain's lock and move the sealer's clock instead of sleeping --- apps/auth-service/Cargo.toml | 2 + apps/auth-service/src/audit/sealer.rs | 5 +- apps/auth-service/src/audit/tests.rs | 103 +++++++++++++------------- 3 files changed, 57 insertions(+), 53 deletions(-) diff --git a/apps/auth-service/Cargo.toml b/apps/auth-service/Cargo.toml index e6b276589..9b8b40ed6 100644 --- a/apps/auth-service/Cargo.toml +++ b/apps/auth-service/Cargo.toml @@ -50,6 +50,8 @@ rcgen.workspace = true tokio-rustls.workspace = true sourceorigin-repository = { workspace = true, features = ["test-util"] } sourceorigin-telemetry = { workspace = true, features = ["test-util"] } +# Moves the audit sealer's clock in its tests. +tokio = { workspace = true, features = ["test-util"] } insta.workspace = true webauthn-authenticator-rs = { version = "0.5.5", features = ["softpasskey"] } diff --git a/apps/auth-service/src/audit/sealer.rs b/apps/auth-service/src/audit/sealer.rs index 10f5f9162..6382cf9e3 100644 --- a/apps/auth-service/src/audit/sealer.rs +++ b/apps/auth-service/src/audit/sealer.rs @@ -17,8 +17,9 @@ use sourceorigin_repository::{ChainLink, Checkpoint, ExclusiveConnection, LockKe use sourceorigin_telemetry::Counter; use std::{ sync::{Arc, Mutex, PoisonError}, - time::{Duration, Instant}, + time::Duration, }; +use tokio::time::Instant; /// Checkpoints the sealer passed over because none of the environment's audit /// keys verifies them, counted each time it looks for one to build on. @@ -44,7 +45,7 @@ pub(crate) struct Sealer { batch: u32, /// When an attempt to sign a checkpoint last ended in failure. The next try /// waits as long as the interval, so a broken chain is not recomputed on - /// every pass. + /// every pass. Tokio's clock, which tests move. failed_at: Mutex>, } diff --git a/apps/auth-service/src/audit/tests.rs b/apps/auth-service/src/audit/tests.rs index fe5a466e4..f0b8bee31 100644 --- a/apps/auth-service/src/audit/tests.rs +++ b/apps/auth-service/src/audit/tests.rs @@ -116,35 +116,46 @@ impl Fixture { /// Seals, holding the sealer's `lock`, until every committed event is sealed. async fn seal_with(&self, lock: &mut ExclusiveConnection) { + self.drain(lock, async |lock| { + self.sealer.seal(lock).await.expect("seal"); + }) + .await; + } + + /// Runs `pass` with the sealer's `lock` until every committed event is + /// sealed, and returns how many passes sealed some. Events are sealed only + /// once every older transaction on the cluster has ended, and other tests' + /// can delay that, so a pass may seal nothing. + async fn drain( + &self, + lock: &mut ExclusiveConnection, + mut pass: impl AsyncFnMut(&mut ExclusiveConnection), + ) -> usize { + let mut sealing = 0; tokio::time::timeout(Duration::from_secs(30), async { - while self.unsealed().await > 0 { - if self.sealer.seal(lock).await.expect("seal") == 0 { + while let unsealed @ 1.. = self.unsealed().await { + pass(&mut *lock).await; + if self.unsealed().await < unsealed { + sealing += 1; + } else { + // wall-clock: other tests' transactions on the shared cluster + // hold sealing back, and PostgreSQL signals nothing when they end. tokio::time::sleep(Duration::from_millis(20)).await; } } }) .await .expect("every committed event is sealed"); + sealing } - /// Takes the sealer's lock. Advisory locks are per database, not per schema, so - /// the other tests' fixtures take turns with it. + /// Takes the sealer's lock, waiting for its turn: advisory locks are per + /// database, not per schema, so the other tests' fixtures take turns with it. async fn lock(&self) -> ExclusiveConnection { - tokio::time::timeout(Duration::from_secs(30), async { - loop { - if let Some(lock) = self - .database - .try_exclusive(LockKey::AuditChain) - .await - .expect("lock") - { - return lock; - } - tokio::time::sleep(Duration::from_millis(20)).await; - } - }) - .await - .expect("the sealer lock") + self.database + .exclusive(LockKey::AuditChain) + .await + .expect("the sealer lock") } /// Counts the events not sealed yet. @@ -1053,22 +1064,12 @@ async fn sealing_goes_on_when_a_checkpoint_fails() { fixture.record_in(&mut tx).await; } tx.commit().await.expect("commit"); - // Events are sealed only once every older transaction has ended, and other - // tests' transactions can delay that, so passes that seal nothing don't count. - let mut sealing = 0; - tokio::time::timeout(Duration::from_secs(30), async { - while let unsealed @ 1.. = fixture.unsealed().await { - let pass = fixture.sealer.pass(&mut lock, None).await; + let sealing = fixture + .drain(&mut lock, async |lock| { + let pass = fixture.sealer.pass(lock, None).await; assert!(matches!(pass, Err(AuditError::Broken(_))), "{pass:?}"); - if fixture.unsealed().await < unsealed { - sealing += 1; - } else { - tokio::time::sleep(Duration::from_millis(20)).await; - } - } - }) - .await - .expect("every committed event is sealed"); + }) + .await; lock.release().await.expect("release"); assert_eq!(sealing, 1, "{sealing} passes sealed events"); } @@ -1108,23 +1109,26 @@ async fn signing_resumes_above_too_many_untrusted_checkpoints() { #[tokio::test] #[ignore = "requires TEST_DATABASE_URL"] async fn a_failed_checkpoint_is_retried_after_the_interval() { - let every = Duration::from_millis(1_500); - let mut fixture = Fixture::sealed_six().await; - fixture.sealer = fixture - .sealer - .checkpointing_every(TimeDelta::from_std(every).expect("interval")); - fixture.record(1).await; + let every = TimeDelta::minutes(5); + let mut fixture = Fixture::new().await; + fixture.sealer = fixture.sealer.checkpointing_every(every); + fixture.record(7).await; let mut lock = fixture.lock().await; fixture.seal_with(&mut lock).await; + // The sealer's own checkpoint at 6, signed an interval ago, so the next is due. + fixture + .store_checkpoint(&fixture.audit.key, 6, -every) + .await; fixture .tamper(Tamper::EditPayload(7, serde_json::json!({}))) .await; - // The checkpoint at 6 was just signed, and checkpoints have whole seconds. - tokio::time::sleep(every * 2).await; let broken = |result: &Result<(), AuditError>| matches!(result, Err(AuditError::Broken(_))); assert!(broken(&fixture.sealer.checkpoint(&mut lock).await)); assert!(fixture.sealer.checkpoint(&mut lock).await.is_ok(), "waits"); - tokio::time::sleep(every).await; + // Paused only for the jump, so the queries keep their real timeouts. + tokio::time::pause(); + tokio::time::advance(every.to_std().expect("interval")).await; + tokio::time::resume(); assert!( broken(&fixture.sealer.checkpoint(&mut lock).await), "tries again" @@ -1148,14 +1152,11 @@ async fn checkpoints_keep_their_interval_while_batches_stay_full() { } tx.commit().await.expect("commit"); let mut lock = fixture.lock().await; - tokio::time::timeout(Duration::from_secs(30), async { - while fixture.unsealed().await > 0 { - fixture.sealer.pass(&mut lock, None).await.expect("pass"); - tokio::time::sleep(Duration::from_millis(20)).await; - } - }) - .await - .expect("every committed event is sealed"); + fixture + .drain(&mut lock, async |lock| { + fixture.sealer.pass(lock, None).await.expect("pass"); + }) + .await; lock.release().await.expect("release"); let checkpoints = fixture .database From 8fdd92e6b495255758c231bdddb78394e46ca3f6 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 15:44:55 +0100 Subject: [PATCH 2/2] test(audit): the tokio test-util comment names every paused-time use --- apps/auth-service/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/auth-service/Cargo.toml b/apps/auth-service/Cargo.toml index 9b8b40ed6..6e8521e53 100644 --- a/apps/auth-service/Cargo.toml +++ b/apps/auth-service/Cargo.toml @@ -50,7 +50,7 @@ rcgen.workspace = true tokio-rustls.workspace = true sourceorigin-repository = { workspace = true, features = ["test-util"] } sourceorigin-telemetry = { workspace = true, features = ["test-util"] } -# Moves the audit sealer's clock in its tests. +# Paused tokio time in tests: `start_paused`, `pause` and `advance`. tokio = { workspace = true, features = ["test-util"] } insta.workspace = true webauthn-authenticator-rs = { version = "0.5.5", features = ["softpasskey"] }