@@ -149,105 +149,3 @@ fn apply_jitter(duration: Duration, jitter: f64) -> Duration {
149
149
let millis = ( secs * 1000f64 ) + ( nanos / 1000000f64 ) ;
150
150
Duration :: from_millis ( millis as u64 )
151
151
}
152
-
153
- #[ cfg( test) ]
154
- mod test {
155
- use super :: * ;
156
- use proptest:: prelude:: * ;
157
-
158
- #[ test]
159
- fn test_constant_interval ( ) {
160
- let mut ivl = ConstantInterval :: new ( Duration :: from_secs ( 2 ) , Some ( 3 ) ) ;
161
-
162
- assert_eq ! ( ivl. next( ) , Some ( Duration :: from_secs( 2 ) ) ) ;
163
- assert_eq ! ( ivl. next( ) , Some ( Duration :: from_secs( 2 ) ) ) ;
164
- assert_eq ! ( ivl. next( ) , Some ( Duration :: from_secs( 2 ) ) ) ;
165
- assert_eq ! ( ivl. next( ) , None ) ;
166
- }
167
-
168
- #[ test]
169
- fn test_constant_interval_no_max ( ) {
170
- let mut ivl = ConstantInterval :: new ( Duration :: from_secs ( 2 ) , None ) ;
171
- assert_eq ! ( ivl. next( ) , Some ( Duration :: from_secs( 2 ) ) ) ;
172
- }
173
-
174
- #[ test]
175
- fn test_exponential_backoff ( ) {
176
- let mut backoff = ExponentialBackoff :: new ( Duration :: from_secs ( 2 ) , 3 ) ;
177
-
178
- assert_eq ! ( backoff. next( ) , Some ( Duration :: from_secs( 2 ) ) ) ;
179
- assert_eq ! ( backoff. next( ) , Some ( Duration :: from_secs( 6 ) ) ) ;
180
- assert_eq ! ( backoff. next( ) , Some ( Duration :: from_secs( 18 ) ) ) ;
181
- }
182
-
183
- #[ test]
184
- fn test_at_maximum_value ( ) {
185
- let max = Duration :: MAX ;
186
- let mu = Duration :: from_micros ( 1 ) ;
187
- let mut backoff = ExponentialBackoff :: new ( max - mu, 2 ) ;
188
-
189
- assert_eq ! ( backoff. next( ) , Some ( max - mu) ) ;
190
- assert_eq ! ( backoff. next( ) , Some ( max) ) ;
191
- assert_eq ! ( backoff. next( ) , Some ( max) ) ;
192
- }
193
-
194
- #[ test]
195
- fn test_maximum_bound ( ) {
196
- let mut backoff = ExponentialBackoff :: new ( Duration :: from_millis ( 2 ) , 3 )
197
- . max_delay ( Some ( Duration :: from_millis ( 7 ) ) ) ;
198
-
199
- assert_eq ! ( backoff. next( ) , Some ( Duration :: from_millis( 2 ) ) ) ;
200
- assert_eq ! ( backoff. next( ) , Some ( Duration :: from_millis( 6 ) ) ) ;
201
- assert_eq ! ( backoff. next( ) , Some ( Duration :: from_millis( 7 ) ) ) ;
202
- }
203
-
204
- #[ test]
205
- fn test_minimum_value ( ) {
206
- let zero = Duration :: from_millis ( 0 ) ;
207
- let mut backoff = ExponentialBackoff :: new ( zero, 10 ) ;
208
-
209
- assert_eq ! ( backoff. next( ) , Some ( zero) ) ;
210
- assert_eq ! ( backoff. next( ) , Some ( zero) ) ;
211
-
212
- let mut backoff = ExponentialBackoff :: new ( Duration :: from_millis ( 1 ) , 0 ) ;
213
-
214
- assert_eq ! ( backoff. next( ) , Some ( Duration :: from_millis( 1 ) ) ) ;
215
- assert_eq ! ( backoff. next( ) , Some ( zero) ) ;
216
- }
217
-
218
- #[ test]
219
- fn test_rounding ( ) {
220
- let second = Duration :: from_secs ( 1 ) ;
221
- assert_eq ! ( apply_jitter( second, 1.0 ) , second) ;
222
- }
223
-
224
- proptest ! {
225
- #[ test]
226
- fn test_jitter( millis: u64 , jitter: u64 ) {
227
- let max_num = 2u64 . checked_pow( f64 :: MANTISSA_DIGITS ) . unwrap( ) ;
228
- let jitter = ( jitter % max_num) as f64 / ( max_num as f64 ) ;
229
- let unjittered_duration = Duration :: from_millis( millis) ;
230
- let jittered_duration = apply_jitter( unjittered_duration, jitter) ;
231
- prop_assert!( jittered_duration <= unjittered_duration) ;
232
- }
233
- }
234
-
235
- // NOTE: The test is disabled because the clock does not advance.
236
- #[ ignore]
237
- #[ tokio:: test]
238
- async fn test_exponential_backoff_delay ( ) {
239
- let retry_interval_initial = Duration :: from_secs ( 4 ) ;
240
- let retry_interval_factor = 5 ;
241
- let retry_interval_max = Duration :: from_secs ( 24 * 60 * 60 ) ;
242
- tokio:: time:: pause ( ) ;
243
-
244
- let _ = retry_future (
245
- || async { 0 } ,
246
- |_| true ,
247
- ExponentialBackoff :: new ( retry_interval_initial, retry_interval_factor)
248
- . max_delay ( Some ( retry_interval_max) )
249
- . take ( 5 ) ,
250
- )
251
- . await ;
252
- }
253
- }
0 commit comments