Skip to content

Isolate #[throttle] registry tests with a shared TEST_LOCK#1849

Merged
madmax983 merged 1 commit into
trunk-devfrom
fix/1725-throttle-test-isolation
Jul 13, 2026
Merged

Isolate #[throttle] registry tests with a shared TEST_LOCK#1849
madmax983 merged 1 commit into
trunk-devfrom
fix/1725-throttle-test-isolation

Conversation

@madmax983

Copy link
Copy Markdown
Owner

Before

Two rate-limit integration tests intermittently failed, returning 429 instead of 200:

  • throttle_route::principal_key_derives_from_session_without_global_principal_middleware
  • rate_limit_principal::principal_strategy_keys_on_session_user_id

Under the ~1200 parallel integration tests, per-principal token buckets could leak between concurrently-running tests through the process-global #[throttle] limiter registry (THROTTLE_REGISTRY). Calling __throttle_registry_reset() alone is insufficient: it clears the entire process-wide registry, so a sibling test running concurrently can either repopulate a bucket mid-assertion or have its own limiter dropped out from under it.

After

Each test that touches the throttle registry now takes a process-global TEST_LOCK first, then resets the registry, and holds the lock guard for the whole test body. This serializes all registry-touching tests against each other, so no sibling can mutate the registry during another's assertions — the same isolation precedent already used by the circuit-breaker tests (circuit_breaker::TEST_LOCK, PR #1817) and config_runtime_drift.

How

  • Added a pub static TEST_LOCK: std::sync::Mutex<()> to security/rate_limit.rs, mirroring circuit_breaker::TEST_LOCK, and re-exported it (#[doc(hidden)]) from security/mod.rs next to __throttle_registry_reset.
  • At the top of every #[throttle]-registry test in throttle_route.rs (and the two named flaky tests), take the lock first, then call __throttle_registry_reset(), holding the guard for the whole test. Because the reset clears the whole registry, every registry-touching test must hold the lock — otherwise a locked test's reset could drop an unlocked sibling's limiter — so the guard is applied consistently across all of them. Each also carries #[allow(clippy::await_holding_lock)], matching the circuit-breaker precedent.

Notes

Gates

  • cargo fmt --all -- --check — clean
  • cargo clippy -p autumn-web --all-targets -- -D warnings — clean
  • Both named tests pass, repeated 3x; all 37 throttle_route + rate_limit_principal tests pass.

Generated by Claude Code

Two rate-limit tests intermittently returned 429 instead of 200 because
per-principal buckets could leak from concurrent tests through the
process-global #[throttle] limiter registry. Add a process-global
TEST_LOCK (mirroring circuit_breaker::TEST_LOCK) and have every
registry-touching integration test take the lock first, then reset the
registry, holding the guard for the whole test so no sibling mutates the
registry mid-assertion.

Part of #1725.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@madmax983
madmax983 marked this pull request as ready for review July 13, 2026 06:49
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@madmax983
madmax983 merged commit 1bcc7e3 into trunk-dev Jul 13, 2026
33 of 34 checks passed
@madmax983
madmax983 deleted the fix/1725-throttle-test-isolation branch July 13, 2026 14:01
@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.02%. Comparing base (14e6518) to head (04ed87d).
⚠️ Report is 37 commits behind head on trunk-dev.

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##           trunk-dev    #1849      +/-   ##
=============================================
+ Coverage      86.82%   87.02%   +0.19%     
=============================================
  Files            273      276       +3     
  Lines         202424   207214    +4790     
=============================================
+ Hits          175761   180330    +4569     
- Misses         26663    26884     +221     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants