1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*!
   Utilities for retrying test operations.
*/

use core::time::Duration;
use std::thread::sleep;
use tracing::{info, trace};

use crate::error::Error;

/**
   A simplified version of retry logic used for testing.
   We do not need complicated retry logic as we need this
   only to test eventual consistency which should reach
   within a few seconds.
*/
pub fn assert_eventually_succeed<R>(
    task_name: &str,
    attempts: u16,
    interval: Duration,
    task: impl Fn() -> Result<R, Error>,
) -> Result<R, Error> {
    sleep(interval);
    for i in 0..attempts {
        match task() {
            Ok(res) => {
                info!("task {} succeed after {} tries", task_name, i);
                return Ok(res);
            }
            Err(e) => {
                trace!("retrying task {} that failed with error: {}", task_name, e);
                sleep(interval)
            }
        }
    }

    Err(Error::retry(task_name.to_string(), attempts))
}