Skip to content

Commit 43f0c9c

Browse files
authored
Freeze time in rate limit general-limit specs (#5526)
The per-user reset window is wall-clock aligned via next_expires_in. When the 60-minute boundary falls between the burst of requests, the counter resets mid-test and the final request is not rate limited, returning 200 instead of 429. Freeze time around each burst so the window cannot roll over.
1 parent f29a864 commit 43f0c9c

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

‎spec/request/rate_limit_spec.rb‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@
2323
end
2424

2525
it 'uses the general limit' do
26-
10.times do |n|
26+
# Freeze time so the reset window cannot roll over mid-test.
27+
Timecop.freeze do
28+
10.times do |n|
29+
get '/v3/spaces', nil, user_headers
30+
expect(last_response.status).to eq(200), "rate limited after #{n} requests"
31+
end
32+
2733
get '/v3/spaces', nil, user_headers
28-
expect(last_response.status).to eq(200), "rate limited after #{n} requests"
34+
expect(last_response.status).to eq(429)
35+
parsed_response = Oj.load(last_response.body)
36+
expect(parsed_response['errors'].first['detail']).to eq('Rate Limit Exceeded')
2937
end
30-
31-
get '/v3/spaces', nil, user_headers
32-
expect(last_response.status).to eq(429)
33-
parsed_response = Oj.load(last_response.body)
34-
expect(parsed_response['errors'].first['detail']).to eq('Rate Limit Exceeded')
3538
end
3639
end
3740

@@ -48,15 +51,17 @@
4851
end
4952

5053
it 'uses the general limit' do
51-
10.times do |n|
54+
Timecop.freeze do
55+
10.times do |n|
56+
get '/v3/spaces', nil, client_headers
57+
expect(last_response.status).to eq(200), "rate limited after #{n} requests"
58+
end
59+
5260
get '/v3/spaces', nil, client_headers
53-
expect(last_response.status).to eq(200), "rate limited after #{n} requests"
61+
expect(last_response.status).to eq(429)
62+
parsed_response = Oj.load(last_response.body)
63+
expect(parsed_response['errors'].first['detail']).to eq('Rate Limit Exceeded')
5464
end
55-
56-
get '/v3/spaces', nil, client_headers
57-
expect(last_response.status).to eq(429)
58-
parsed_response = Oj.load(last_response.body)
59-
expect(parsed_response['errors'].first['detail']).to eq('Rate Limit Exceeded')
6065
end
6166
end
6267

0 commit comments

Comments
 (0)