tests: fix flaky tests

This commit is contained in:
Stevan Freeborn
2026-03-25 20:14:24 -05:00
parent 77dd25f8d8
commit f30ad7dd97
4 changed files with 92 additions and 28 deletions
+24
View File
@@ -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()
);
}