[fix][test] Fix flaky RateLimiterTest.testDispatchRate#25846
Merged
Conversation
The test waited a fixed 3 seconds and then asserted that permits had been renewed, but the renew task is scheduled at fixed rate and can fire late under CI load, leaving acquiredPermits == permits when the assertion runs. Replace the fixed sleep with an Awaitility poll so the test waits until the renew task has actually freed permits.
nodece
approved these changes
May 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
RateLimiterTest.testDispatchRateis flaky in CI. Recent example: PR #25844 run 26175306430 job 77005756965.The test invokes
tryAcquire(100)three times on a dispatch-rateRateLimiter(which has no upper bound onacquiredPermits, only back-pressure), leavingacquiredPermits = 300. It then sleeps3 * rateTimeMSec(3 s) and assertsgetAvailablePermits() > 0, expecting the scheduled renew task to have run three times and subtracted3 * permitsfromacquiredPermits.The renew is scheduled via
ScheduledExecutorService.scheduleAtFixedRate(...)at a 1 s rate. Under CI load, the third tick can fire slightly after the assertion runs, soacquiredPermitsis still≥ 100andgetAvailablePermits()is still0, failing the assertion.Modifications
Replace the fixed
Thread.sleep(rateTimeMSec)+ assertion withAwaitility.await().atMost(10 * rateTimeMSec, MILLISECONDS).until(() -> rate.getAvailablePermits() > 0). This polls for the expected post-condition instead of relying on the scheduler firing on a fixed wall-clock budget. Also drops the intermediateassertEquals(getAvailablePermits(), 0)check after 2 s, since it is the same kind of timing-sensitive snapshot.Verifying this change
This change is already covered by existing tests, such as the modified
testDispatchRateitself, plus the other timing tests inRateLimiterTest.