Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/auth-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
# 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"] }

Expand Down
5 changes: 3 additions & 2 deletions apps/auth-service/src/audit/sealer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Option<Instant>>,
}

Expand Down
103 changes: 52 additions & 51 deletions apps/auth-service/src/audit/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down
Loading