use retry_if::{ExponentialBackoffConfig, retry};
use std::time::Duration;
use tokio::time::{Instant, pause};
const BACKOFF_CONFIG: ExponentialBackoffConfig = ExponentialBackoffConfig {
max_retries: 5,
t_wait: Duration::from_secs(1),
backoff: 2.0,
t_wait_max: None,
backoff_max: None,
};
fn retry_if(_i: &i64) -> bool {
true
}
pub struct Counter {
pub count: i64,
}
impl Counter {
#[retry(BACKOFF_CONFIG, retry_if)]
async fn increase_count(&mut self) -> i64 {
self.count += 1;
self.count
}
}
#[tokio::test]
async fn main() {
let mut counter = Counter { count: 0 };
pause();
let start = Instant::now();
counter.increase_count().await;
let end = Instant::now();
let duration = end - start;
assert!(duration > Duration::from_secs(31));
assert!(duration < Duration::from_millis(31100));
assert_eq!(6, counter.count);
}