@@ -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 ) ]
73126pub struct TlsOptions {
0 commit comments