Skip to content

Commit

Permalink
Merge pull request #240 from threema-donat/fix-flaky-tests
Browse files Browse the repository at this point in the history
Fix flaky tests
  • Loading branch information
dermesser authored Jul 2, 2024
2 parents 08e2695 + 053d9d8 commit 732f25d
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ pub(crate) struct DiskStorage {
}

impl DiskStorage {
pub(crate) async fn new(filename: PathBuf) -> Result<Self, io::Error> {
pub(crate) async fn new(filename: impl Into<PathBuf>) -> Result<Self, io::Error> {
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(),
Expand Down Expand Up @@ -414,6 +415,8 @@ async fn open_writeable_file(

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

use super::*;

#[test]
Expand Down Expand Up @@ -452,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 @@ -466,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 732f25d

Please sign in to comment.