Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattisonchao committed Feb 18, 2025
1 parent a84da75 commit 7a0ab53
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,59 @@ impl Default for OperationRetryOptions {
}
}

impl OperationRetryOptions {
pub fn allow_retry(&self, current: u32) -> bool {
self.max_retries.is_none() || current < self.max_retries.unwrap()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_allow_retry_no_max_retries() {
let options = OperationRetryOptions {
operation_timeout: Duration::from_secs(30),
retry_delay: Duration::from_secs(5),
max_retries: None,
};

// If max_retries is None, it should always allow retries
assert!(options.allow_retry(0));
assert!(options.allow_retry(100));
assert!(options.allow_retry(u32::MAX));
}

#[test]
fn test_allow_retry_with_max_retries() {
let options = OperationRetryOptions {
operation_timeout: Duration::from_secs(30),
retry_delay: Duration::from_secs(5),
max_retries: Some(3),
};

// If max_retries is set to 3, we allow retries for current < 3
assert!(options.allow_retry(0)); // current < 3
assert!(options.allow_retry(2)); // current < 3
assert!(!options.allow_retry(3)); // current == 3
assert!(!options.allow_retry(4)); // current > 3
}

#[test]
fn test_allow_retry_max_retries_is_zero() {
let options = OperationRetryOptions {
operation_timeout: Duration::from_secs(30),
retry_delay: Duration::from_secs(5),
max_retries: Some(0),
};

// If max_retries is 0, it should not allow any retries
assert!(!options.allow_retry(0)); // current == 0
assert!(!options.allow_retry(1)); // current > 0
}
}

/// configuration for TLS connections
#[derive(Debug, Clone)]
pub struct TlsOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/retry_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub async fn handle_retry_error<Exe: Executor>(
return Err(err.into());
}
};
if operation_retry_options.max_retries.is_none() || current_retries < operation_retry_options.max_retries.unwrap() {
if operation_retry_options.allow_retry(current_retries) {
let max_retries= operation_retry_options.max_retries.unwrap_or(0);
error!(
"{operation_name}({topic}) answered {kind}{text}, retrying request after {:?} (max_retries = {max_retries})",
Expand Down

0 comments on commit 7a0ab53

Please sign in to comment.