From 3f7a5e4be307a69324e4e842d8eada2236fcb693 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Tue, 19 Nov 2024 13:27:58 +0100 Subject: [PATCH 1/2] feat: Clear config cache in start_io() TODO: - adapt start_io() documentation - test? This is needed for iOS, see comment in the code. An alternative would be to add an API `invalidate_config_cache()` or to do nothing and just assume that things will be fine. --- src/context.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/context.rs b/src/context.rs index 40bf95fbd9..c0f596932f 100644 --- a/src/context.rs +++ b/src/context.rs @@ -472,6 +472,14 @@ impl Context { // Allow at least 1 message every second + a burst of 3. *lock = Ratelimit::new(Duration::new(3, 0), 3.0); } + + // The next line is mainly for iOS: + // iOS starts a separate process for receiving notifications and if the user concurrently + // starts the app, the UI process opens the database but waits with calling start_io() + // until the notifications process finishes. + // Now, some configs may have changed, so, we need to invalidate the cache. + self.sql.config_cache.write().await.clear(); + self.scheduler.start(self.clone()).await; } From 3de445c12a5089bd5be353e9d2bfe977fc724c8e Mon Sep 17 00:00:00 2001 From: Hocuri Date: Tue, 19 Nov 2024 16:37:50 +0100 Subject: [PATCH 2/2] Add test --- src/context.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/context.rs b/src/context.rs index c0f596932f..dd1b5e73ea 100644 --- a/src/context.rs +++ b/src/context.rs @@ -2072,4 +2072,41 @@ mod tests { Ok(()) } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn test_cache_is_cleared_when_io_is_started() -> Result<()> { + let alice = TestContext::new_alice().await; + assert_eq!( + alice.get_config(Config::ShowEmails).await?, + Some("2".to_string()) + ); + + // Change the config circumventing the cache + // This simulates what the notfication plugin on iOS might do + // because it runs in a different process + alice + .sql + .execute( + "INSERT OR REPLACE INTO config (keyname, value) VALUES ('show_emails', '0')", + (), + ) + .await?; + + // Alice's Delta Chat doesn't know about it yet: + assert_eq!( + alice.get_config(Config::ShowEmails).await?, + Some("2".to_string()) + ); + + // Starting IO will fail of course because no server settings are configured, + // but it should invalidate the caches: + alice.start_io().await; + + assert_eq!( + alice.get_config(Config::ShowEmails).await?, + Some("0".to_string()) + ); + + Ok(()) + } }