@@ -774,6 +774,13 @@ pub(crate) mod tests {
774
774
775
775
use super :: * ;
776
776
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
+
777
784
impl TokenBucket {
778
785
// Resets the token bucket: budget set to max capacity and last-updated set to now.
779
786
fn reset ( & mut self ) {
@@ -1009,11 +1016,11 @@ pub(crate) mod tests {
1009
1016
// since consume failed, limiter should be blocked now
1010
1017
assert ! ( l. is_blocked( ) ) ;
1011
1018
// 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 ) ) ;
1013
1020
// limiter should still be blocked
1014
1021
assert ! ( l. is_blocked( ) ) ;
1015
1022
// 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 ) ) ;
1017
1024
// the timer_fd should have an event on it by now
1018
1025
l. event_handler ( ) . unwrap ( ) ;
1019
1026
// limiter should now be unblocked
@@ -1042,11 +1049,11 @@ pub(crate) mod tests {
1042
1049
// since consume failed, limiter should be blocked now
1043
1050
assert ! ( l. is_blocked( ) ) ;
1044
1051
// 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 ) ) ;
1046
1053
// limiter should still be blocked
1047
1054
assert ! ( l. is_blocked( ) ) ;
1048
1055
// 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 ) ) ;
1050
1057
// the timer_fd should have an event on it by now
1051
1058
l. event_handler ( ) . unwrap ( ) ;
1052
1059
// limiter should now be unblocked
@@ -1076,11 +1083,11 @@ pub(crate) mod tests {
1076
1083
// since consume failed, limiter should be blocked now
1077
1084
assert ! ( l. is_blocked( ) ) ;
1078
1085
// 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 ) ) ;
1080
1087
// limiter should still be blocked
1081
1088
assert ! ( l. is_blocked( ) ) ;
1082
1089
// 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 ) ) ;
1084
1091
// the timer_fd should have an event on it by now
1085
1092
l. event_handler ( ) . unwrap ( ) ;
1086
1093
// limiter should now be unblocked
0 commit comments