Skip to content

Commit 7a0ab53

Browse files
committed
add test
1 parent a84da75 commit 7a0ab53

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/connection_manager.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,59 @@ impl Default for OperationRetryOptions {
6868
}
6969
}
7070

71+
impl OperationRetryOptions {
72+
pub fn allow_retry(&self, current: u32) -> bool {
73+
self.max_retries.is_none() || current < self.max_retries.unwrap()
74+
}
75+
}
76+
77+
#[cfg(test)]
78+
mod tests {
79+
use super::*;
80+
81+
#[test]
82+
fn test_allow_retry_no_max_retries() {
83+
let options = OperationRetryOptions {
84+
operation_timeout: Duration::from_secs(30),
85+
retry_delay: Duration::from_secs(5),
86+
max_retries: None,
87+
};
88+
89+
// If max_retries is None, it should always allow retries
90+
assert!(options.allow_retry(0));
91+
assert!(options.allow_retry(100));
92+
assert!(options.allow_retry(u32::MAX));
93+
}
94+
95+
#[test]
96+
fn test_allow_retry_with_max_retries() {
97+
let options = OperationRetryOptions {
98+
operation_timeout: Duration::from_secs(30),
99+
retry_delay: Duration::from_secs(5),
100+
max_retries: Some(3),
101+
};
102+
103+
// If max_retries is set to 3, we allow retries for current < 3
104+
assert!(options.allow_retry(0)); // current < 3
105+
assert!(options.allow_retry(2)); // current < 3
106+
assert!(!options.allow_retry(3)); // current == 3
107+
assert!(!options.allow_retry(4)); // current > 3
108+
}
109+
110+
#[test]
111+
fn test_allow_retry_max_retries_is_zero() {
112+
let options = OperationRetryOptions {
113+
operation_timeout: Duration::from_secs(30),
114+
retry_delay: Duration::from_secs(5),
115+
max_retries: Some(0),
116+
};
117+
118+
// If max_retries is 0, it should not allow any retries
119+
assert!(!options.allow_retry(0)); // current == 0
120+
assert!(!options.allow_retry(1)); // current > 0
121+
}
122+
}
123+
71124
/// configuration for TLS connections
72125
#[derive(Debug, Clone)]
73126
pub struct TlsOptions {

src/retry_op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub async fn handle_retry_error<Exe: Executor>(
5454
return Err(err.into());
5555
}
5656
};
57-
if operation_retry_options.max_retries.is_none() || current_retries < operation_retry_options.max_retries.unwrap() {
57+
if operation_retry_options.allow_retry(current_retries) {
5858
let max_retries= operation_retry_options.max_retries.unwrap_or(0);
5959
error!(
6060
"{operation_name}({topic}) answered {kind}{text}, retrying request after {:?} (max_retries = {max_retries})",

0 commit comments

Comments
 (0)