From 30894e5eb99a408bd89b12aac8eaeb7b52b31186 Mon Sep 17 00:00:00 2001 From: Pablo Guardiola Date: Mon, 1 Jun 2026 14:01:32 -0400 Subject: [PATCH] test: make vault listener notification test deterministic --- .../src/storage/credential_storage.rs | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/walletkit-core/src/storage/credential_storage.rs b/walletkit-core/src/storage/credential_storage.rs index 566a36fa..e09064fa 100644 --- a/walletkit-core/src/storage/credential_storage.rs +++ b/walletkit-core/src/storage/credential_storage.rs @@ -877,6 +877,30 @@ mod tests { } } + fn wait_for_listener_count(count: &AtomicU32, expected: u32) { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(1); + + loop { + let actual = count.load(Ordering::SeqCst); + if actual >= expected { + assert_eq!( + actual, expected, + "listener should be notified {expected} times" + ); + return; + } + + if std::time::Instant::now() >= deadline { + assert_eq!( + actual, expected, + "listener should be notified {expected} times" + ); + } + + std::thread::sleep(std::time::Duration::from_millis(10)); + } + } + #[test] fn test_replay_guard_field_element_serialization() { let root = temp_root_path(); @@ -1620,14 +1644,7 @@ mod tests { .store_credential(&cred, &FieldElement::from(7u64), 9999, None, 1000) .expect("store credential"); - // Give the delivery thread time to process. - std::thread::sleep(std::time::Duration::from_millis(50)); - - assert_eq!( - count.load(Ordering::SeqCst), - 1, - "listener should be notified once" - ); + wait_for_listener_count(&count, 1); cleanup_test_storage(&root); } @@ -1653,17 +1670,11 @@ mod tests { let id = store .store_credential(&cred, &FieldElement::from(7u64), 9999, None, 1000) .expect("store credential"); + wait_for_listener_count(&count, 1); store.delete_credential(id).expect("delete credential"); - std::thread::sleep(std::time::Duration::from_millis(50)); - - // store + delete = 2 notifications - assert_eq!( - count.load(Ordering::SeqCst), - 2, - "listener should be notified twice" - ); + wait_for_listener_count(&count, 2); cleanup_test_storage(&root); } @@ -1789,6 +1800,7 @@ mod tests { store .store_credential(&cred, &FieldElement::from(7u64), 9999, None, 1000) .expect("store credential"); + wait_for_listener_count(&count, 1); store.destroy_storage().expect("destroy storage");