Skip to content

Commit 8d2cf59

Browse files
ShadowCurseroypat
authored andcommitted
chore: use custom refill interval in rate limiter tests
Specify custom interval in order to avoid race condition in tests which wait for the limiter to refill for the exact amount of time. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 56874c8 commit 8d2cf59

File tree

1 file changed

+13
-6
lines changed
  • src/vmm/src/rate_limiter

1 file changed

+13
-6
lines changed

src/vmm/src/rate_limiter/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)