From c4727d93c47b4956c0afcfffba51edd4b7d21c1e Mon Sep 17 00:00:00 2001 From: threema-donat <129288638+threema-donat@users.noreply.github.com> Date: Tue, 2 Jul 2024 17:18:26 +0200 Subject: [PATCH 1/2] Accept path buffer when creating disl storage --- src/storage.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/storage.rs b/src/storage.rs index c3fc00670..ed750e859 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -346,7 +346,8 @@ pub(crate) struct DiskStorage { } impl DiskStorage { - pub(crate) async fn new(filename: PathBuf) -> Result { + pub(crate) async fn new(filename: impl Into) -> Result { + let filename = filename.into(); let tokens = match JSONTokens::load_from_file(&filename).await { Ok(tokens) => tokens, Err(e) if e.kind() == io::ErrorKind::NotFound => JSONTokens::new(), From 053d9d86d236d6895ef51e747548d8e4dccc0480 Mon Sep 17 00:00:00 2001 From: threema-donat <129288638+threema-donat@users.noreply.github.com> Date: Tue, 2 Jul 2024 17:18:52 +0200 Subject: [PATCH 2/2] Fix flaky disk storage tests through waiting for file creation --- src/storage.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/storage.rs b/src/storage.rs index ed750e859..f9d9fda50 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -415,6 +415,8 @@ async fn open_writeable_file( #[cfg(test)] mod tests { + use std::time::Duration; + use super::*; #[test] @@ -453,10 +455,10 @@ mod tests { .tempdir() .unwrap(); + let filename = tempdir.path().join("tokenstorage.json"); + { - let storage = DiskStorage::new(tempdir.path().join("tokenstorage.json")) - .await - .unwrap(); + let storage = DiskStorage::new(&filename).await.unwrap(); assert!(storage.get(scope_set).await.is_none()); storage .set(scope_set, new_token("my_access_token")) @@ -467,11 +469,24 @@ mod tests { Some(new_token("my_access_token")) ); } + async fn find_file(path: &Path) { + loop { + if tokio::fs::metadata(path).await.is_ok() { + break; + } + } + } + + tokio::time::timeout(Duration::from_secs(1), find_file(&filename)) + .await + .expect(&format!( + "File not created at {}", + filename.to_string_lossy() + )); + { // Create a new DiskStorage instance and verify the tokens were read from disk correctly. - let storage = DiskStorage::new(tempdir.path().join("tokenstorage.json")) - .await - .unwrap(); + let storage = DiskStorage::new(&filename).await.unwrap(); assert_eq!( storage.get(scope_set).await, Some(new_token("my_access_token"))