Freeze time in rate limit specs, to fix a flaky test #40

Merged
matchu merged 1 commit from armin-bot/impress:claude/freeze-time-rate-limit-specs into main 2026-11-17 12:58:52 -08:00
Contributor

Fixes a flaky test in spec/requests/rate_limits_spec.rb. It failed in run 59, on a PR that had nothing to do with rate limits.

Cause

Rack::Attack counts requests in fixed time windows, not sliding ones: each counter's cache key includes Time.now.to_i / period. So the 5-minute logins/ip counter resets every time the clock reaches a multiple of 5 minutes.

In that run, "limits attempts from one IP, even against different accounts" made its 30 failed logins between 20:44:59.6 and 20:45:00.17. That's about 28 before the 20:45:00 boundary and 2 after. So when the final login with the correct password arrived, the new window's count was only 2. The login succeeded and redirected to /, instead of being throttled.

Every throttle test in this file can hit this. The pets/ip tests are the most exposed, because that window is only 30 seconds.

Fix

The fix freezes time around each example (with ActiveSupport::Testing::TimeHelpers, as authenticated_at_spec.rb already does). That way all of a test's requests land in the same window. The retry-after test still works, because it computes its value from the same frozen clock.

Testing

bundle exec rspec spec/requests/rate_limits_spec.rb: 17 examples, 0 failures.

I didn't add a test that recreates the boundary crossing, because the run 59 log already shows what happened.

🤖 Generated with Claude Code

https://claude.ai/code/session_014PWWd15hN2tDYWAGEkLWwr

Fixes a flaky test in `spec/requests/rate_limits_spec.rb`. It failed in [run 59](https://code.openneo.net/OpenNeo/impress/actions/runs/59/jobs/0/attempt/1), on a PR that had nothing to do with rate limits. ## Cause Rack::Attack counts requests in fixed time windows, not sliding ones: each counter's cache key includes `Time.now.to_i / period`. So the 5-minute `logins/ip` counter resets every time the clock reaches a multiple of 5 minutes. In that run, "limits attempts from one IP, even against different accounts" made its 30 failed logins between `20:44:59.6` and `20:45:00.17`. That's about 28 before the 20:45:00 boundary and 2 after. So when the final login with the correct password arrived, the new window's count was only 2. The login succeeded and redirected to `/`, instead of being throttled. Every throttle test in this file can hit this. The `pets/ip` tests are the most exposed, because that window is only 30 seconds. ## Fix The fix freezes time around each example (with `ActiveSupport::Testing::TimeHelpers`, as `authenticated_at_spec.rb` already does). That way all of a test's requests land in the same window. The `retry-after` test still works, because it computes its value from the same frozen clock. ## Testing `bundle exec rspec spec/requests/rate_limits_spec.rb`: 17 examples, 0 failures. I didn't add a test that recreates the boundary crossing, because the run 59 log already shows what happened. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_014PWWd15hN2tDYWAGEkLWwr
Freeze time in rate limit specs, to fix a flaky test
All checks were successful
CI / rspec (pull_request) Successful in 1m56s
CI / deploy (pull_request) Has been skipped
20c6a64508
Rack::Attack counts requests in fixed time windows: each counter's key
includes `Time.now.to_i / period`. So if a test's requests straddle a
window boundary, the count resets partway through, and the final request
isn't throttled.

This happened in CI: "limits attempts from one IP" made its 30 failed
logins from 20:44:59.6 to 20:45:00.17, so the 5-minute `logins/ip`
counter reset at 20:45:00, and the real login went through.

Freezing time puts all of a test's requests in the same window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014PWWd15hN2tDYWAGEkLWwr
matchu scheduled this pull request to auto merge when all checks succeed 2026-11-17 12:57:27 -08:00
matchu merged commit 6e047f1b33 into main 2026-11-17 12:58:52 -08:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
OpenNeo/impress!40
No description provided.