Skip to content

Commit

Permalink
Fix flaky disk storage tests through waiting for file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
threema-donat committed Jul 2, 2024
1 parent c4727d9 commit 053d9d8
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ async fn open_writeable_file(

#[cfg(test)]
mod tests {
use std::time::Duration;

use super::*;

#[test]
Expand Down Expand Up @@ -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"))
Expand All @@ -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"))
Expand Down

0 comments on commit 053d9d8

Please sign in to comment.