From d6f0635023290fd62baf84b8da9bbc363d6ffedf Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Mon, 4 Sep 2023 18:00:04 +0200 Subject: [PATCH] chore: clippy + review feedback --- crates/matrix-sdk-ui/src/encryption_sync/mod.rs | 14 +++++++++----- crates/matrix-sdk-ui/src/notification_client.rs | 15 ++++++++++++++- .../tests/integration/encryption_sync.rs | 9 ++++----- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/crates/matrix-sdk-ui/src/encryption_sync/mod.rs b/crates/matrix-sdk-ui/src/encryption_sync/mod.rs index 9464e8a52..ef57e458b 100644 --- a/crates/matrix-sdk-ui/src/encryption_sync/mod.rs +++ b/crates/matrix-sdk-ui/src/encryption_sync/mod.rs @@ -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, diff --git a/crates/matrix-sdk-ui/src/notification_client.rs b/crates/matrix-sdk-ui/src/notification_client.rs index 062713f10..650b9c655 100644 --- a/crates/matrix-sdk-ui/src/notification_client.rs +++ b/crates/matrix-sdk-ui/src/notification_client.rs @@ -192,10 +192,18 @@ 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 @@ -203,12 +211,17 @@ impl NotificationClient { .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); } } diff --git a/crates/matrix-sdk-ui/tests/integration/encryption_sync.rs b/crates/matrix-sdk-ui/tests/integration/encryption_sync.rs index e530961fd..19f510660 100644 --- a/crates/matrix-sdk-ui/tests/integration/encryption_sync.rs +++ b/crates/matrix-sdk-ui/tests/integration/encryption_sync.rs @@ -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?; @@ -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); @@ -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?; @@ -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?; @@ -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?;