Skip to content

Commit

Permalink
fix(limits): separately test min-max vs header+jitter cases (#520)
Browse files Browse the repository at this point in the history
I conflated the logic while hastily adding a test for jitter in #519.

This fixes that flakey test by separating the two paths.
  • Loading branch information
jharley authored Aug 1, 2024
1 parent cf2ca67 commit f3ddd9c
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions client/v2/limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,27 @@ func TestClient_rateLimitBackoff(t *testing.T) {

min = 100 * time.Millisecond
max = 500 * time.Millisecond
r := rateLimitBackoff(min, max, w.Result())
assert.Greater(t,
rateLimitBackoff(min, max, w.Result()),
60*time.Second,
"expected backoff to be 60sec+jitter",
)
})

if assert.Greater(t, r, 60*time.Second, "expected backoff to be 60sec+") {
assert.WithinRange(t,
time.Now().Add(r-60*time.Second),
time.Now().Add(min),
time.Now().Add(max),
"jitter not applied correctly",
)
}
t.Run("without supported rate limit header jitter is between min and max", func(t *testing.T) {
w := httptest.NewRecorder()
w.WriteHeader(http.StatusTooManyRequests)

min = 200 * time.Millisecond
max = 900 * time.Millisecond

now := time.Now().UTC()
assert.WithinRange(t,
now.Add(rateLimitBackoff(min, max, w.Result())),
now.Add(min),
now.Add(max),
"expected backoff to be between min and max",
)
})
}

Expand Down

0 comments on commit f3ddd9c

Please sign in to comment.