tests: fix flaky tests
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
|
||||
use onspring::OnspringClient;
|
||||
use std::collections::HashMap;
|
||||
use std::future::Future;
|
||||
use std::sync::Once;
|
||||
use std::time::Duration;
|
||||
|
||||
static INIT: Once = Once::new();
|
||||
|
||||
@@ -64,3 +66,25 @@ pub async fn add_record() -> i32 {
|
||||
let response = client.save_record(request).await.unwrap();
|
||||
response.id
|
||||
}
|
||||
|
||||
pub async fn retry<F, Fut, T>(max_attempts: u32, delay: Duration, f: F) -> T
|
||||
where
|
||||
F: Fn() -> Fut,
|
||||
Fut: Future<Output = Result<T, Box<dyn std::error::Error>>>,
|
||||
{
|
||||
let mut last_err = None;
|
||||
for attempt in 0..max_attempts {
|
||||
if attempt > 0 {
|
||||
tokio::time::sleep(delay).await;
|
||||
}
|
||||
match f().await {
|
||||
Ok(val) => return val,
|
||||
Err(e) => last_err = Some(e),
|
||||
}
|
||||
}
|
||||
panic!(
|
||||
"retry failed after {} attempts: {}",
|
||||
max_attempts,
|
||||
last_err.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user