@@ -774,6 +774,13 @@ pub(crate) mod tests {
774774
775775 use super :: * ;
776776
777+ // Define custom refill interval to be a bit bigger. This will help
778+ // in tests which wait for a limiter refill in 2 stages. This will make it so
779+ // second wait will always result in the limiter being refilled. Otherwise
780+ // there is a chance for a race condition between limiter refilling and limiter
781+ // checking.
782+ const TEST_REFILL_TIMER_INTERVAL_MS : u64 = REFILL_TIMER_INTERVAL_MS + 10 ;
783+
777784 impl TokenBucket {
778785 // Resets the token bucket: budget set to max capacity and last-updated set to now.
779786 fn reset ( & mut self ) {
@@ -1009,11 +1016,11 @@ pub(crate) mod tests {
10091016 // since consume failed, limiter should be blocked now
10101017 assert ! ( l. is_blocked( ) ) ;
10111018 // wait half the timer period
1012- thread:: sleep ( Duration :: from_millis ( REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
1019+ thread:: sleep ( Duration :: from_millis ( TEST_REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
10131020 // limiter should still be blocked
10141021 assert ! ( l. is_blocked( ) ) ;
10151022 // wait the other half of the timer period
1016- thread:: sleep ( Duration :: from_millis ( REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
1023+ thread:: sleep ( Duration :: from_millis ( TEST_REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
10171024 // the timer_fd should have an event on it by now
10181025 l. event_handler ( ) . unwrap ( ) ;
10191026 // limiter should now be unblocked
@@ -1042,11 +1049,11 @@ pub(crate) mod tests {
10421049 // since consume failed, limiter should be blocked now
10431050 assert ! ( l. is_blocked( ) ) ;
10441051 // wait half the timer period
1045- thread:: sleep ( Duration :: from_millis ( REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
1052+ thread:: sleep ( Duration :: from_millis ( TEST_REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
10461053 // limiter should still be blocked
10471054 assert ! ( l. is_blocked( ) ) ;
10481055 // wait the other half of the timer period
1049- thread:: sleep ( Duration :: from_millis ( REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
1056+ thread:: sleep ( Duration :: from_millis ( TEST_REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
10501057 // the timer_fd should have an event on it by now
10511058 l. event_handler ( ) . unwrap ( ) ;
10521059 // limiter should now be unblocked
@@ -1076,11 +1083,11 @@ pub(crate) mod tests {
10761083 // since consume failed, limiter should be blocked now
10771084 assert ! ( l. is_blocked( ) ) ;
10781085 // wait half the timer period
1079- thread:: sleep ( Duration :: from_millis ( REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
1086+ thread:: sleep ( Duration :: from_millis ( TEST_REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
10801087 // limiter should still be blocked
10811088 assert ! ( l. is_blocked( ) ) ;
10821089 // wait the other half of the timer period
1083- thread:: sleep ( Duration :: from_millis ( REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
1090+ thread:: sleep ( Duration :: from_millis ( TEST_REFILL_TIMER_INTERVAL_MS / 2 ) ) ;
10841091 // the timer_fd should have an event on it by now
10851092 l. event_handler ( ) . unwrap ( ) ;
10861093 // limiter should now be unblocked
0 commit comments