Skip to content

Commit

Permalink
chore: clippy + review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjbvr committed Sep 5, 2023
1 parent ea2826a commit d6f0635
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
14 changes: 9 additions & 5 deletions crates/matrix-sdk-ui/src/encryption_sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,19 @@ use tracing::{debug, trace};
pub struct EncryptionSyncPermit(());

impl EncryptionSyncPermit {
/// Create a new [`EncryptionSyncPermit`].
///
/// Note: in general, you'd want to get such a permit from a [`SyncService`]
/// instead of creating it yourself.
pub fn new() -> Self {
pub(crate) fn new() -> Self {
Self(())
}
}

impl EncryptionSyncPermit {
/// Test-only.
#[doc(hidden)]
pub fn new_for_testing() -> Self {
Self::new()
}
}

/// Should the `EncryptionSync` make use of locking?
pub enum WithLocking {
Yes,
Expand Down
15 changes: 14 additions & 1 deletion crates/matrix-sdk-ui/src/notification_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,36 @@ impl NotificationClient {
// means we were racing against the encryption sync. Wait a bit, attempt to
// decrypt, and carry on.

let mut wait = 200; // we get to wait 7 times that number at most.
// We repeat the sleep 3 times at most, each iteration we
// double the amount of time waited, so overall we may wait up to 7 times this
// amount.
let mut wait = 200;

for _ in 0..3 {
tracing::debug!("Sync running in background while getting a notification; waiting for decryption…");

tokio::time::sleep(Duration::from_millis(wait)).await; // heuristics~~~
//
let new_event = room.decrypt_event(raw_event.cast_ref()).await?;

if !is_event_encrypted(
new_event
.event
.deserialize()
.map_err(|_| Error::InvalidRumaEvent)?
.event_type(),
) {
tracing::debug!("Waiting succeeded!");
return Ok(Some(new_event));
}

wait *= 2;
}

// We couldn't decrypt the event after waiting a few times, abort.
tracing::debug!(
"Timeout waiting for the sync service to decrypt the notification event."
);
return Ok(None);
}
}
Expand Down
9 changes: 4 additions & 5 deletions crates/matrix-sdk-ui/tests/integration/encryption_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
async fn test_smoke_encryption_sync_works() -> anyhow::Result<()> {
let (client, server) = logged_in_client().await;

let sync_permit = Arc::new(AsyncMutex::new(EncryptionSyncPermit::new()));
let sync_permit = Arc::new(AsyncMutex::new(EncryptionSyncPermit::new_for_testing()));
let sync_permit_guard = sync_permit.clone().lock_owned().await;
let encryption_sync =
EncryptionSync::new("tests".to_owned(), client, None, WithLocking::Yes).await?;
Expand Down Expand Up @@ -111,7 +111,6 @@ async fn test_smoke_encryption_sync_works() -> anyhow::Result<()> {
assert!(stream.next().await.is_none());

// Start a new sync.
drop(stream);
let sync_permit_guard = sync_permit.clone().lock_owned().await;
let stream = encryption_sync.sync(sync_permit_guard);
pin_mut!(stream);
Expand Down Expand Up @@ -198,7 +197,7 @@ async fn test_encryption_sync_one_fixed_iteration() -> anyhow::Result<()> {

let _guard = setup_mocking_sliding_sync_server(&server).await;

let sync_permit = Arc::new(AsyncMutex::new(EncryptionSyncPermit::new()));
let sync_permit = Arc::new(AsyncMutex::new(EncryptionSyncPermit::new_for_testing()));
let sync_permit_guard = sync_permit.lock_owned().await;
let encryption_sync =
EncryptionSync::new("tests".to_owned(), client, None, WithLocking::Yes).await?;
Expand Down Expand Up @@ -230,7 +229,7 @@ async fn test_encryption_sync_two_fixed_iterations() -> anyhow::Result<()> {

let _guard = setup_mocking_sliding_sync_server(&server).await;

let sync_permit = Arc::new(AsyncMutex::new(EncryptionSyncPermit::new()));
let sync_permit = Arc::new(AsyncMutex::new(EncryptionSyncPermit::new_for_testing()));
let sync_permit_guard = sync_permit.lock_owned().await;
let encryption_sync =
EncryptionSync::new("tests".to_owned(), client, None, WithLocking::Yes).await?;
Expand Down Expand Up @@ -265,7 +264,7 @@ async fn test_encryption_sync_two_fixed_iterations() -> anyhow::Result<()> {
async fn test_encryption_sync_always_reloads_todevice_token() -> anyhow::Result<()> {
let (client, server) = logged_in_client().await;

let sync_permit = Arc::new(AsyncMutex::new(EncryptionSyncPermit::new()));
let sync_permit = Arc::new(AsyncMutex::new(EncryptionSyncPermit::new_for_testing()));
let sync_permit_guard = sync_permit.lock_owned().await;
let encryption_sync =
EncryptionSync::new("tests".to_owned(), client.clone(), None, WithLocking::Yes).await?;
Expand Down

0 comments on commit d6f0635

Please sign in to comment.