From 4ae2702240241b9cedd91d0266e0d966a4d39db6 Mon Sep 17 00:00:00 2001 From: piquel Date: Sat, 7 Nov 2026 00:22:15 +0100 Subject: [PATCH] test(database): lock tests wait for the lock instead of sleeping, so a slow machine can't fail them --- crates/database/src/repositories/handles/tests.rs | 8 ++++---- crates/database/src/repositories/users/tests.rs | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/database/src/repositories/handles/tests.rs b/crates/database/src/repositories/handles/tests.rs index cfdfa4d00..faf55140d 100644 --- a/crates/database/src/repositories/handles/tests.rs +++ b/crates/database/src/repositories/handles/tests.rs @@ -172,17 +172,17 @@ async fn locked_lookups_read_a_rename_they_waited_for() { .await .expect("rename") ); + let mut tx = crate::DatabaseTransaction::new(pool.begin().await.expect("lookup transaction")); + let backend = Database::backend(&mut tx.inner).await; let lookup = tokio::spawn(async move { - let database = Database { pool }; - let mut tx = database.begin().await.expect("lookup transaction"); tx.users() .find_active_by_id_for_update(account.id) .await .expect("lookup") .expect("user") }); - tokio::time::sleep(std::time::Duration::from_millis(200)).await; - assert!(!lookup.is_finished(), "the lookup must wait for the rename"); + // The lookup waits for the rename. + Database::wait_for_lock(&pool, backend).await; rename.commit().await.expect("commit"); let user = lookup.await.expect("lookup task"); assert_eq!((user.handle, user.profile_version), (renamed, 2)); diff --git a/crates/database/src/repositories/users/tests.rs b/crates/database/src/repositories/users/tests.rs index 0e6877db7..1b57ac7e4 100644 --- a/crates/database/src/repositories/users/tests.rs +++ b/crates/database/src/repositories/users/tests.rs @@ -132,15 +132,16 @@ async fn locked_session_lookup_waits_for_concurrent_logout() { .expect("revoke"), [session] ); + let mut exchange = pool.begin().await.expect("exchange transaction"); + let backend = Database::backend(&mut exchange).await; let exchange = tokio::spawn(async move { - let mut exchange = pool.begin().await.expect("exchange transaction"); SessionRepository::from_connection(&mut exchange) .find_active_by_id_for_update(session) .await .expect("lookup") }); - tokio::time::sleep(Duration::from_millis(200)).await; - assert!(!exchange.is_finished(), "lookup must wait for the logout"); + // The lookup waits for the logout. + Database::wait_for_lock(&pool, backend).await; logout.commit().await.expect("commit logout"); assert!(exchange.await.expect("exchange task").is_none()); }