-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db88c9e
commit 65e9bf6
Showing
3 changed files
with
100 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,107 @@ | ||
pub async fn retry<'a, F>(retries: u16, interval_ms: u64, f: impl Fn() -> F) -> bool | ||
pub async fn retry<'a, F>( | ||
retries: u16, | ||
interval_ms: u64, | ||
f: impl Fn() -> F + Sync, | ||
) -> bool | ||
where | ||
F: std::future::Future<Output = anyhow::Result<bool>> + Send + 'a, | ||
{ | ||
let duration = tokio::time::Duration::from_millis(interval_ms); | ||
for _ in 0..retries { | ||
tokio::time::sleep(duration).await; | ||
poll(retries, interval_ms, || async { | ||
if let Ok(v) = f().await { | ||
if v { | ||
return true; | ||
return Some(true); | ||
} | ||
} | ||
None | ||
}) | ||
.await | ||
.unwrap_or(false) | ||
} | ||
|
||
pub async fn poll<'a, F, T>( | ||
max_attempts: u16, | ||
interval_ms: u64, | ||
f: impl Fn() -> F, | ||
) -> Option<T> | ||
where | ||
F: std::future::Future<Output = Option<T>> + Send + 'a, | ||
{ | ||
let duration = tokio::time::Duration::from_millis(interval_ms); | ||
for _ in 0..max_attempts { | ||
tokio::time::sleep(duration).await; | ||
if let Some(v) = f().await { | ||
return Some(v); | ||
} | ||
} | ||
None | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::sync::Arc; | ||
use tokio::sync::Mutex; | ||
|
||
#[tokio::test] | ||
async fn retry() { | ||
async fn check(locked: Arc<Mutex<i32>>, result: bool) -> anyhow::Result<bool> { | ||
let mut v = locked.lock().await; | ||
if *v == 5 { | ||
return Ok(result); | ||
} | ||
*v += 1; | ||
Err(anyhow::anyhow!("")) | ||
} | ||
|
||
// retry till the end and get a positive result | ||
let locked = Arc::new(Mutex::new(1)); | ||
assert!(super::retry(5, 1, || async { check(locked.clone(), true).await }).await); | ||
|
||
// retry till the end and get a negative result | ||
let locked = Arc::new(Mutex::new(1)); | ||
assert!( | ||
!super::retry(5, 1, || async { check(locked.clone(), false).await }).await | ||
); | ||
|
||
// not waiting long enough | ||
let locked = Arc::new(Mutex::new(1)); | ||
assert!( | ||
!super::retry(2, 1, || async { check(locked.clone(), true).await }).await | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn poll() { | ||
async fn check(locked: Arc<Mutex<i32>>, result: bool) -> Option<bool> { | ||
let mut v = locked.lock().await; | ||
if *v == 5 { | ||
return Some(result); | ||
} | ||
*v += 1; | ||
None | ||
} | ||
|
||
// poll till the end and get a positive result | ||
let locked = Arc::new(Mutex::new(1)); | ||
assert!( | ||
super::poll(5, 1, || async { check(locked.clone(), true).await }) | ||
.await | ||
.unwrap() | ||
); | ||
|
||
// poll till the end and get a negative result | ||
let locked = Arc::new(Mutex::new(1)); | ||
assert!( | ||
!super::poll(5, 1, || async { check(locked.clone(), false).await }) | ||
.await | ||
.unwrap() | ||
); | ||
|
||
// not waiting long enough | ||
let locked = Arc::new(Mutex::new(1)); | ||
assert!( | ||
super::poll(2, 1, || async { check(locked.clone(), true).await }) | ||
.await | ||
.is_none() | ||
); | ||
} | ||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters