Skip to content

Commit

Permalink
test: Testing the cross-process event cache store.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Oct 30, 2024
1 parent 9004932 commit 4e2bea3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
73 changes: 73 additions & 0 deletions crates/matrix-sdk-base/src/event_cache_store/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,5 @@ mod tests {
}

event_cache_store_integration_tests!();
event_cache_store_integration_tests_time!();
}
5 changes: 4 additions & 1 deletion crates/matrix-sdk-sqlite/src/event_cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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};
Expand All @@ -383,4 +385,5 @@ mod encrypted_tests {
}

event_cache_store_integration_tests!();
event_cache_store_integration_tests_time!();
}

0 comments on commit 4e2bea3

Please sign in to comment.