From 4e2bea3b5693a5951eda505cedd6c317b7dfaa70 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 30 Oct 2024 16:13:53 +0100 Subject: [PATCH] test: Testing the cross-process event cache store. --- .../event_cache_store/integration_tests.rs | 73 +++++++++++++++++++ .../src/event_cache_store/memory_store.rs | 1 + .../src/event_cache_store.rs | 5 +- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/crates/matrix-sdk-base/src/event_cache_store/integration_tests.rs b/crates/matrix-sdk-base/src/event_cache_store/integration_tests.rs index 2a0cc30faf0..7377cb3b461 100644 --- a/crates/matrix-sdk-base/src/event_cache_store/integration_tests.rs +++ b/crates/matrix-sdk-base/src/event_cache_store/integration_tests.rs @@ -187,3 +187,76 @@ macro_rules! event_cache_store_integration_tests { } }; } + +#[allow(unused_macros)] +#[macro_export] +macro_rules! event_cache_store_integration_tests_time { + () => { + mod event_cache_store_integration_tests_time { + use std::time::Duration; + + use matrix_sdk_test::async_test; + use $crate::event_cache_store::IntoEventCacheStore; + + use super::get_event_cache_store; + + #[async_test] + async fn test_lease_locks() { + let store = get_event_cache_store().await.unwrap().into_event_cache_store(); + + let acquired0 = store.try_take_leased_lock(0, "key", "alice").await.unwrap(); + assert!(acquired0); + + // Should extend the lease automatically (same holder). + let acquired2 = store.try_take_leased_lock(300, "key", "alice").await.unwrap(); + assert!(acquired2); + + // Should extend the lease automatically (same holder + time is ok). + let acquired3 = store.try_take_leased_lock(300, "key", "alice").await.unwrap(); + assert!(acquired3); + + // Another attempt at taking the lock should fail, because it's taken. + let acquired4 = store.try_take_leased_lock(300, "key", "bob").await.unwrap(); + assert!(!acquired4); + + // Even if we insist. + let acquired5 = store.try_take_leased_lock(300, "key", "bob").await.unwrap(); + assert!(!acquired5); + + // That's a nice test we got here, go take a little nap. + tokio::time::sleep(Duration::from_millis(50)).await; + + // Still too early. + let acquired55 = store.try_take_leased_lock(300, "key", "bob").await.unwrap(); + assert!(!acquired55); + + // Ok you can take another nap then. + tokio::time::sleep(Duration::from_millis(250)).await; + + // At some point, we do get the lock. + let acquired6 = store.try_take_leased_lock(0, "key", "bob").await.unwrap(); + assert!(acquired6); + + tokio::time::sleep(Duration::from_millis(1)).await; + + // The other gets it almost immediately too. + let acquired7 = store.try_take_leased_lock(0, "key", "alice").await.unwrap(); + assert!(acquired7); + + tokio::time::sleep(Duration::from_millis(1)).await; + + // But when we take a longer lease... + let acquired8 = store.try_take_leased_lock(300, "key", "bob").await.unwrap(); + assert!(acquired8); + + // It blocks the other user. + let acquired9 = store.try_take_leased_lock(300, "key", "alice").await.unwrap(); + assert!(!acquired9); + + // We can hold onto our lease. + let acquired10 = store.try_take_leased_lock(300, "key", "bob").await.unwrap(); + assert!(acquired10); + } + } + }; +} diff --git a/crates/matrix-sdk-base/src/event_cache_store/memory_store.rs b/crates/matrix-sdk-base/src/event_cache_store/memory_store.rs index ac571cab333..f4c0563da1f 100644 --- a/crates/matrix-sdk-base/src/event_cache_store/memory_store.rs +++ b/crates/matrix-sdk-base/src/event_cache_store/memory_store.rs @@ -168,4 +168,5 @@ mod tests { } event_cache_store_integration_tests!(); + event_cache_store_integration_tests_time!(); } diff --git a/crates/matrix-sdk-sqlite/src/event_cache_store.rs b/crates/matrix-sdk-sqlite/src/event_cache_store.rs index 7e3af8e24de..c47b3829ae2 100644 --- a/crates/matrix-sdk-sqlite/src/event_cache_store.rs +++ b/crates/matrix-sdk-sqlite/src/event_cache_store.rs @@ -254,7 +254,7 @@ mod tests { use matrix_sdk_base::{ event_cache_store::{EventCacheStore, EventCacheStoreError}, - event_cache_store_integration_tests, + event_cache_store_integration_tests, event_cache_store_integration_tests_time, media::{MediaFormat, MediaRequest, MediaThumbnailSettings}, }; use matrix_sdk_test::async_test; @@ -278,6 +278,7 @@ mod tests { } event_cache_store_integration_tests!(); + event_cache_store_integration_tests_time!(); async fn get_event_cache_store_content_sorted_by_last_access( event_cache_store: &SqliteEventCacheStore, @@ -359,6 +360,7 @@ mod encrypted_tests { use matrix_sdk_base::{ event_cache_store::EventCacheStoreError, event_cache_store_integration_tests, + event_cache_store_integration_tests_time, }; use once_cell::sync::Lazy; use tempfile::{tempdir, TempDir}; @@ -383,4 +385,5 @@ mod encrypted_tests { } event_cache_store_integration_tests!(); + event_cache_store_integration_tests_time!(); }