diff --git a/src/storage.rs b/src/storage.rs index c3fc00670..f9d9fda50 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(), @@ -414,6 +415,8 @@ async fn open_writeable_file( #[cfg(test)] mod tests { + use std::time::Duration; + use super::*; #[test] @@ -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")) @@ -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"))