Skip to content

Commit bc177a8

Browse files
ValentaTomase2b-bot[bot]
authored andcommitted
test(api): stop the rate-limit refill test racing its own drain
GitOrigin-RevId: 4297fc15c6bd05a81e8d87ee33179da16d8a0b5d
1 parent 5fd4a6c commit bc177a8

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

packages/api/internal/middleware/ratelimit/ratelimit_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@ func TestIntegration_Refill(t *testing.T) {
243243

244244
redisClient := redis_utils.SetupInstance(t)
245245
limiter := redis_rate.NewLimiter(redisClient)
246-
ff := newTestFF(t, routeConfig(10, 2))
246+
// One token every 2s: slow enough that a loaded runner cannot refill a
247+
// token mid-drain (a 100ms refill raced the three requests below and the
248+
// deny assertion saw 200), fast enough for the refill poll to finish.
249+
ff := newTestFF(t, map[string]map[string]int{
250+
testRoute: {"rate": 1, "burst": 2, "period_s": 2},
251+
})
247252

248253
r := newRouterWithTeam(t, limiter, Config{FailOpen: true}, ff, uuid.New())
249254

@@ -255,11 +260,11 @@ func TestIntegration_Refill(t *testing.T) {
255260
w := doRequest(t, r)
256261
assert.Equal(t, http.StatusTooManyRequests, w.Code)
257262

258-
// Wait for refill (rate=10/s → one token every 100ms).
259-
time.Sleep(200 * time.Millisecond)
260-
261-
w = doRequest(t, r)
262-
assert.Equal(t, http.StatusOK, w.Code)
263+
// A denied request consumes no token, so poll until one refills instead
264+
// of sleeping through a fixed window.
265+
require.Eventually(t, func() bool {
266+
return doRequest(t, r).Code == http.StatusOK
267+
}, 10*time.Second, 50*time.Millisecond, "no token was refilled")
263268
}
264269

265270
func TestIntegration_IndependentTeams(t *testing.T) {

0 commit comments

Comments
 (0)