Skip to content

Commit

Permalink
added generic retry helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Gadorek authored and Hahihula committed Jan 22, 2025
1 parent f40f44a commit 829822b
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{command_executor::execute_command, idf_tools::read_and_parse_tools_file};
use log::debug;
use rust_search::SearchBuilder;
#[cfg(not(windows))]
use std::os::unix::fs::MetadataExt;
Expand Down Expand Up @@ -227,11 +228,35 @@ pub fn remove_directory_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
Ok(())
}

/// Retry wrapper function that takes a closure and retries it according to the configuration
pub fn with_retry<F, T, E>(f: F, max_retries: usize) -> Result<T, E>
where
F: Fn() -> Result<T, E>,
E: std::fmt::Debug,
{
let mut attempt = 0;

loop {
match f() {
Ok(value) => return Ok(value),
Err(e) => {
attempt += 1;
if attempt >= max_retries {
return Err(e);
}

debug!("Attempt {} failed with error: {:?}", attempt, e);
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs::{self, File};
use std::io::Write;
use std::sync::atomic::{AtomicU32, Ordering};
use tempfile::TempDir;

#[test]
Expand Down Expand Up @@ -358,4 +383,38 @@ mod tests {
assert!(remove_directory_all(&test_dir).is_ok());
assert!(!test_dir.exists());
}
#[test]
fn test_retry_success_after_failure() {
let counter = AtomicU32::new(0);

let result = with_retry(
|| {
let current = counter.fetch_add(1, Ordering::SeqCst);
if current < 2 {
Err("Not ready yet")
} else {
Ok("Success!")
}
},
3,
);

assert!(result.is_ok());
assert_eq!(counter.load(Ordering::SeqCst), 3);
}
#[test]
fn test_retry_all_attempts_failed() {
let counter = AtomicU32::new(0);

let result: Result<&str, &str> = with_retry(
|| {
counter.fetch_add(1, Ordering::SeqCst);
Err("Always fails")
},
3,
);

assert!(result.is_err());
assert_eq!(counter.load(Ordering::SeqCst), 3);
}
}

0 comments on commit 829822b

Please sign in to comment.