Skip to content

test(circuit-breaker): verify open circuit does not call the service and fix overflowed timestamp - #3619

Open
renanmpimentel wants to merge 2 commits into
iluwatar:masterfrom
renanmpimentel:test/circuit-breaker-open-state
Open

renanmpimentel wants to merge 2 commits into
iluwatar:masterfrom
renanmpimentel:test/circuit-breaker-open-state

Conversation

@renanmpimentel

@renanmpimentel renanmpimentel commented Oct 6, 2026 •

Copy link
Copy Markdown

What

Test-only changes to DefaultCircuitBreakerTest.

1. The open-circuit contract was not observed

Making attemptRequest() ignore the OPEN state (if (state == State.OPEN) → if (false)) keeps all 12 module tests green. AppTest expects the fallback "Delayed service is down", but that is also the message of the exception thrown by the real call, so a fallback and a real call cannot be told apart.

New test testOpenCircuitReturnsCachedResponseWithoutCallingService uses a service that counts calls and returns a distinct message per call (Failure #1, #2, ...). It asserts that, after the circuit opens, attemptRequest() returns the cached Failure #1 and the service was called once. It also catches removing evaluateState() from attemptRequest().

2. testEvaluateState relied on an int overflow

circuitBreaker.lastFailureTime = System.nanoTime() - 1000 * 1000 * 1000 * 1000;

1000 * 1000 * 1000 * 1000 is evaluated as int and equals -727379968, so the "failure much earlier than now" actually lands ~0.73 s in the future, and the test asserts OPEN for it (the comment says the opposite of what the circuit should do). As a result, the test passes even if evaluateState ignores retryTimePeriod (> retryTimePeriod → > 0).

The test now uses long literals and a 60 s retry period: a recent failure must be OPEN, a failure older than the retry period must be HALF_OPEN.

Verification

  • mvn -pl circuit-breaker verify: 13 tests pass, spotless check passes.
  • Each regression above fails the updated tests and passes again once reverted.

Found with supertest: mutation testing plus statement-removal analysis, with every finding confirmed by running the regression against the unmodified test.

- Add a test with a call-counting service: once the circuit is open,
  attemptRequest() must return the cached failure without calling the
  service again. Previously a fallback and a real failing call returned
  the same string, so ignoring the OPEN state went undetected.
- testEvaluateState computed "a failure long ago" with int arithmetic
  (1000 * 1000 * 1000 * 1000 overflows to -727379968), which placed the
  failure in the future. Use long literals and assert OPEN within the
  retry period and HALF_OPEN after it.
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

PR Summary

Updated circuit-breaker tests to correctly observe open-circuit behavior and fix timestamp overflow handling. Reworked testEvaluateState to use long literals and a 60-second retry, and added testOpenCircuitReturnsCachedResponseWithoutCallingService to ensure an open circuit returns a cached failure without calling the service. Imports adjusted and assertion order made explicit (expected, actual).

Changes

File Summary
circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java - Update testEvaluateState to use long timing with a 60s retry; asserts OPEN/HALF_OPEN/CLOSED in proper order (expected, actual).
  • Add testOpenCircuitReturnsCachedResponseWithoutCallingService to verify cached failure is returned when circuit is OPEN, using a counting RemoteService.
  • Adjust imports (AtomicInteger, assertThrows) and update assertion style. |

autogenerated by presubmit.ai

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
  • 5085a97: test(circuit-breaker): verify open circuit does not call the service

  • Add a test with a call-counting service: once the circuit is open,
    attemptRequest() must return the cached failure without calling the
    service again. Previously a fallback and a real failing call returned
    the same string, so ignoring the OPEN state went undetected.

  • testEvaluateState computed "a failure long ago" with int arithmetic
    (1000 * 1000 * 1000 * 1000 overflows to -727379968), which placed the
    failure in the future. Use long literals and assert OPEN within the
    retry period and HALF_OPEN after it.

Files Processed (1)
  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java (3 hunks)
Actionable Comments (3)
  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [41-41]

    best_practice: "AssertEquals argument order should be (expected, actual)."

  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [47-47]

    best_practice: "AssertEquals argument order misused"

  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [52-52]

    best_practice: "AssertEquals argument order misused"

Skipped Comments (1)
  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [85-105]

    enhancement: "New test: verify cached response after OPEN does not call service again"

@codecov

codecov Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.99%. Comparing base (4cabb20) to head (6f47a62).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3619      +/-   ##
============================================
- Coverage     84.06%   83.99%   -0.08%     
+ Complexity     4353     4347       -6     
============================================
  Files          1133     1133              
  Lines         15400    15400              
  Branches        739      739              
============================================
- Hits          12946    12935      -11     
- Misses         2158     2164       +6     
- Partials        296      301       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM!

Review Summary

Commits Considered (1)
  • 6f47a62: test(circuit-breaker): use (expected, actual) order in testEvaluateState
Files Processed (1)
  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java (3 hunks)
Actionable Comments (0)
Skipped Comments (9)
  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [39-39]

    readability: "Explicit initialization intent for long retry period."

  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [41-41]

    readability: "Assert initial CLOSED state after construction."

  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [47-47]

    readability: "Open state before retry period expiration."

  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [51-51]

    readability: "HALF_OPEN after retry period elapsed."

  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [55-55]

    maintainability: "Idempotency of evaluateState to CLOSED."

  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [86-86]

    enhancement: "Add test for cached response during OPEN state."

  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [98-98]

    behavior: "First failure triggers exception and opens circuit."

  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [101-101]

    behavior: "Cached failure response on OPEN state."

  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker/DefaultCircuitBreakerTest.java [103-103]

    readability: "OPEN state preserved on cache-hit."

This branch has not been deployed

No deployments
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