Skip to content

Commit 48c54e1

Browse files
committed
test(base): Improve test coverage.
1 parent 5073cd1 commit 48c54e1

File tree

1 file changed

+49
-7
lines changed
  • crates/matrix-sdk-base/src/event_cache/store

1 file changed

+49
-7
lines changed

crates/matrix-sdk-base/src/event_cache/store/mod.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ impl<T> EventCacheStoreLockPoisonError<T> {
182182
}
183183
}
184184

185+
#[cfg(not(tarpaulin_include))]
185186
impl<T> fmt::Debug for EventCacheStoreLockPoisonError<T> {
186187
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187188
f.debug_struct("EventCacheStoreLockPoisonError").finish_non_exhaustive()
@@ -194,11 +195,7 @@ impl<T> fmt::Display for EventCacheStoreLockPoisonError<T> {
194195
}
195196
}
196197

197-
impl<T> Error for EventCacheStoreLockPoisonError<T> {
198-
fn description(&self) -> &str {
199-
"Poisoned lock: lock has been acquired from another process"
200-
}
201-
}
198+
impl<T> Error for EventCacheStoreLockPoisonError<T> {}
202199

203200
/// Event cache store specific error type.
204201
#[derive(Debug, thiserror::Error)]
@@ -312,14 +309,15 @@ impl BackingStore for LockableEventCacheStore {
312309

313310
#[cfg(all(test, not(target_arch = "wasm32")))] // because time is a thing
314311
mod tests {
315-
use std::{sync::Arc, time::Duration};
312+
use std::{error::Error, fmt, sync::Arc, time::Duration};
316313

314+
use assert_matches::assert_matches;
317315
use matrix_sdk_common::store_locks::MAX_BACKOFF_MS;
318316
use matrix_sdk_test::async_test;
319317
use ruma::user_id;
320318
use tokio::time::sleep;
321319

322-
use super::MemoryStore;
320+
use super::{EventCacheStoreError, EventCacheStoreLockPoisonError, MemoryStore};
323321
use crate::{store::StoreConfig, test_utils::logged_in_base_client_with_store_config};
324322

325323
#[async_test]
@@ -408,5 +406,49 @@ mod tests {
408406
assert!(guard.is_ok()); // lock has been acquired
409407
assert!(guard.unwrap().is_ok()); // lock is not poisoned
410408
}
409+
410+
// For the sake of improving test coverage.
411+
// Client B cannot take the lock because A holds it.
412+
{
413+
// `lock` is not okay.
414+
let guard = event_cache_store_lock_b.lock().await;
415+
416+
assert!(guard.is_err()); // lock has not been acquired
417+
}
418+
}
419+
420+
#[test]
421+
fn test_poison_error() {
422+
let error = EventCacheStoreLockPoisonError(42);
423+
424+
fn error_to_string<E>(error: E) -> String
425+
where
426+
E: Error,
427+
{
428+
error.to_string()
429+
}
430+
431+
assert_eq!(
432+
error_to_string(error),
433+
"Poisoned lock: lock has been acquired from another process".to_owned(),
434+
);
435+
}
436+
437+
#[test]
438+
fn test_backend_error() {
439+
#[derive(Debug)]
440+
struct Foo;
441+
442+
impl fmt::Display for Foo {
443+
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
444+
formatter.debug_struct("Foo").finish()
445+
}
446+
}
447+
448+
impl Error for Foo {}
449+
450+
let error = EventCacheStoreError::backend(Foo);
451+
452+
assert_matches!(error, EventCacheStoreError::Backend(_));
411453
}
412454
}

0 commit comments

Comments
 (0)