Skip to content

test(circuit-breaker): drive breaker transitions deterministically in the rapid-transition race test (LAB-3612) - #292

Open
27Bslash6 wants to merge 1 commit into
mainfrom
lab-3612-deterministic-breaker-transition-test
Open

27Bslash6 wants to merge 1 commit into
mainfrom
lab-3612-deterministic-breaker-transition-test

Conversation

@27Bslash6

@27Bslash6 27Bslash6 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR rewrites the test_rapid_concurrent_state_transitions test to make circuit breaker state transitions deterministic rather than relying on OS thread scheduling.

Problem

The previous test used time.sleep(0.01) between operations and a short timeout_seconds to trigger state transitions, then only asserted weakly that "at least 2 states were observed." This made the test non-deterministic — whether the breaker actually tripped and recovered depended on how the OS happened to schedule threads and time.sleep, so transition coverage was never guaranteed.

Changes

The test now drives the breaker through controlled, reproducible transitions:

  • Frozen clock: Uses time_machine.travel(0, tick=False) so time only advances deliberately, never during a breaker operation.
  • Lockstep rounds: 10 threads run 10 rounds in synchronized lockstep using two barriers:
    • An arrivals barrier ensures an admitted operation doesn't complete until every thread has been admitted or rejected — so a HALF_OPEN probe holds its permit while other threads contend for it.
    • A round_end barrier advances the clock past the recovery timeout between rounds (via a barrier action, guaranteeing the clock only moves when no thread is inside the breaker).
  • Explicit expectations: The test now tracks how many operations were admitted per round and asserts exact counts:
    • Failure rounds (every 3rd) admit all 10 threads and trip the breaker.
    • The round after a failure admits exactly half_open_requests probes and recovers — verifying the double-checked OPEN → HALF_OPEN transition and the permit cap under contention.
    • All other rounds run CLOSED and admit everyone.
  • Stronger assertions: Replaces the weak "≥2 states" check with an exact admitted_per_round == expected assertion and confirms both OPEN and CLOSED states are always observed (breaker both trips and recovers on every run).
  • Adds handling to distinguish breaker-rejected calls (BackendError) from admitted-but-failed calls (RuntimeError).
  • Barrier timeouts (30s) are used purely as hang guards to avoid a dead worker blocking the whole test suite until CI timeout.

Impact

The rapid-transition race test is now deterministic and reliable, verifying that the double-checked locking pattern correctly handles concurrent state checks and permit limits — instead of leaving coverage to chance.

… the rapid-transition race test (LAB-3612)

test_rapid_concurrent_state_transitions asserted that post-hoc, unlocked reads
of breaker._state saw at least two distinct states after 10 threads slept 10 ms
between calls against a breaker with a 50 ms recovery timeout. The fifth call
lands 51-54 ms after the last failure, so the assertion rested on a 1-4 ms
scheduling margin; under load it fails (1 in 50 locally, and the 3.13 leg of
the main-branch run at 284fa7e).

Freeze the clock with time_machine and run the threads in lockstep rounds: a
Barrier action advances the clock past the timeout between rounds, and each
admitted operation waits at a second barrier until every thread has been
admitted or rejected, so the HALF_OPEN probe holds its permit while the other
threads contend for it. Assert the exact admitted count per round (everyone in
failure and CLOSED rounds, exactly half_open_requests in the round after each
trip) and that both OPEN and CLOSED are observed. The test now fails against a
breaker with the half-open cap removed, one that never leaves OPEN, and one
that never closes; the old version passed the first of those.

Test-only change; no src/ change.
@kodus-27b

kodus-27b Bot commented Sep 14, 2026

Copy link
Copy Markdown

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Validate Business Logic: Ask Kody to validate your code against business rules by adding a comment with the @kody -v business-logic command.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug
Performance
Security
Business Logic

Access your configuration settings here.

@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Team

Run ID: d5ec7168-4221-4576-a889-184100835e8f

📥 Commits

Reviewing files that changed from the base of the PR and between 284fa7e and 721ec99.

📒 Files selected for processing (1)
  • tests/unit/test_circuit_breaker_race_conditions.py

Included review availability: 0 reviews are currently available. Your included PR review attempts over the past 7 days set your current allowance at 2 reviews per hour.


Walkthrough

Changes

Circuit breaker race-condition tests

Layer / File(s) Summary
Deterministic race validation
tests/unit/test_circuit_breaker_race_conditions.py
The test identifies circuit-breaker rejections with BackendError. It uses frozen time, barriers, lockstep rounds, controlled timeout advancement, injected failures, admission-count checks, and circuit-state validation.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Other

Merge Risk: ⚪ Minimal · up to 721ec

The deterministic test update has no identified merge-blocking risk.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the failure, the deterministic test changes, and the validation results. However, it omits the required template sections, including Type of Change, Security Checklist, Docume… Update the description to use the repository template. Add the required headings and complete the applicable checkboxes. State that this is a test-only change, confirm that no public API or documentation changes are required, record the rel…
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: making the circuit-breaker race test deterministic. It is specific, concise, and includes the relevant issue reference.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description explains the failure, the deterministic test changes, and the validation results. However, it omits the required template sections, including Type of Change, Security Checklist, Documentation Validation Checklist, Testing checklist, Backward Compatibility, and Additional Notes.

Resolution

Update the description to use the repository template. Add the required headings and complete the applicable checkboxes. State that this is a test-only change, confirm that no public API or documentation changes are required, record the relevant test commands and results, and complete the security and backward compatibility checks. Use the template headings instead of only Why, What, and Proof.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch lab-3612-deterministic-breaker-transition-test

Comment @coderabbitai help to get the list of available commands.

Comment thread tests/unit/test_circuit_breaker_race_conditions.py
@27Bslash6

Copy link
Copy Markdown
Contributor Author

@kody start-review

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.

1 participant