@@ -239,7 +239,7 @@ impl ConnectionPermit {
239239 }
240240
241241 pub ( crate ) async fn report_retryable_failure ( & self ) {
242- self . controller . clone ( ) . report_and_update ( & self , None , false ) . await ;
242+ self . controller . clone ( ) . report_and_update ( self , None , false ) . await ;
243243 }
244244}
245245
@@ -248,8 +248,8 @@ impl ConnectionPermit {
248248mod test_constants {
249249
250250 pub const TR_HALF_LIFE_MS : u64 = 10 ;
251- pub const INCR_SPACING_MS : u64 = 4 ;
252- pub const DECR_SPACING_MS : u64 = 2 ;
251+ pub const INCR_SPACING_MS : u64 = 200 ;
252+ pub const DECR_SPACING_MS : u64 = 100 ;
253253
254254 pub const TARGET_TIME_MS_S : u64 = 5 ;
255255 pub const TARGET_TIME_MS_L : u64 = 20 ;
@@ -303,10 +303,12 @@ mod tests {
303303
304304 let controller = AdaptiveConcurrencyController :: new_testing ( 1 , ( 1 , 4 ) ) ;
305305
306- for _ in 0 ..10 {
306+ for i in 0 ..10 {
307307 let permit = controller. acquire_connection_permit ( ) . await . unwrap ( ) ;
308- advance ( Duration :: from_millis ( 1 ) ) . await ;
308+ // Increase the duration, so we're always going faster than predicted
309+ advance ( Duration :: from_millis ( 12 - i) ) . await ;
309310 permit. report_completion ( B , true ) . await ;
311+
310312 advance ( Duration :: from_millis ( INCR_SPACING_MS + 1 ) ) . await ;
311313 }
312314
@@ -324,19 +326,38 @@ mod tests {
324326 // Advance on so that the first success will trigger an adjustment.
325327 advance ( Duration :: from_millis ( INCR_SPACING_MS + 1 ) ) . await ;
326328
327- let t = Instant :: now ( ) ;
329+ for i in 0 ..5 {
330+ let permit = controller. acquire_connection_permit ( ) . await . unwrap ( ) ;
331+ // Increase the duration, so we're always going faster than predicted
332+ advance ( Duration :: from_millis ( 12 - i) ) . await ;
333+ permit. report_completion ( B , true ) . await ;
334+
335+ // Don't advance, so it should have only incremented by one as not enough time
336+ // will have passed for more.
337+ }
338+
339+ assert_eq ! ( controller. available_permits( ) , 2 ) ;
340+ assert_eq ! ( controller. total_permits( ) , 2 ) ;
341+
342+ // Now, advance the clock by enough time to allow another change.
343+ advance ( Duration :: from_millis ( INCR_SPACING_MS + 1 ) ) . await ;
328344
329- while t . elapsed ( ) < Duration :: from_millis ( INCR_SPACING_MS + 2 ) {
345+ for i in 5 .. 10 {
330346 let permit = controller. acquire_connection_permit ( ) . await . unwrap ( ) ;
331- advance ( Duration :: from_millis ( 1 ) ) . await ;
347+ // Increase the duration, so we're always going faster than predicted
348+ advance ( Duration :: from_millis ( 12 - i) ) . await ;
332349 permit. report_completion ( B , true ) . await ;
350+
351+ // Don't advance, so it should have only incremented by one as not enough time
352+ // will have passed for more.
333353 }
334354
335355 // The window above should have had exactly two increases; one at the first success and one within the next
336356 // interval.
337357 assert_eq ! ( controller. available_permits( ) , 3 ) ;
338358 assert_eq ! ( controller. total_permits( ) , 3 ) ;
339359 }
360+
340361 #[ tokio:: test]
341362 async fn test_permit_increase_on_slow_but_good_enough ( ) {
342363 time:: pause ( ) ;
0 commit comments