Skip to content

[test-improver] Improve tests for server circuit breaker#4181

Merged
lpcox merged 2 commits intomainfrom
test-improver/circuit-breaker-coverage-77baa43fa5f839e9
Apr 20, 2026
Merged

[test-improver] Improve tests for server circuit breaker#4181
lpcox merged 2 commits intomainfrom
test-improver/circuit-breaker-coverage-77baa43fa5f839e9

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

File Analyzed

  • Test File: internal/server/circuit_breaker_test.go
  • Package: internal/server
  • Lines Added: +75

Improvements Made

1. Increased Coverage

Five previously uncovered branches in circuit_breaker.go are now exercised:

  • RecordSuccess from OPEN state — covers the else if prev != circuitClosed branch, which fires when an in-flight request completes successfully after the circuit was already opened by a concurrent request
  • Allow in HALF-OPEN with probeInFlight=false — covers the defensive fallback that permits a request through when the probe flag was reset without a state transition (the comment in code says "This shouldn't normally happen")
  • extractRateLimitErrorText with non-map content items — covers the continue branch when a content array element is not a map[string]interface{} (e.g. a raw string)
  • isRateLimitToolResult with non-map content items — same branch, parallel function
  • parseRateLimitResetFromText without a terminator — covers the end < 0 branch when the seconds value has no trailing s, ], or ) delimiter (e.g. "rate reset in 42")

2. Coverage Improvements (circuit_breaker.go)

Function Before After
Allow 90.0% 95.0%
RecordSuccess 91.7% 100.0%
extractRateLimitErrorText 90.9% 100.0%
isRateLimitToolResult 93.8% 100.0%
parseRateLimitResetFromText 91.7% 100.0%

Overall internal/server package: 88.8% → 89.2%

3. Cleaner & More Stable Tests

  • ✅ All new tests use t.Parallel() consistent with the existing style
  • ✅ New tests follow the same table-driven and subtest patterns already established in the file
  • TestCircuitBreaker_HalfOpenAllowsWhenNoProbeInFlight directly manipulates internal state via the exported mutex to set up the edge-case scenario without timing dependencies

Test Execution

All tests pass:

ok  github.com/github/gh-aw-mcpg/internal/server  4.460s

Note: TestFetchAndFixSchema_NetworkError in internal/config is a pre-existing failure (confirmed by checking main before this change) and is unrelated to these modifications.

Why These Changes?

The circuit_breaker.go functions RecordSuccess, extractRateLimitErrorText, isRateLimitToolResult, and parseRateLimitResetFromText each had minor coverage gaps caused by defensive/error branches that are difficult to trigger through normal code flow. These branches protect against edge cases (in-flight requests completing after circuit open, malformed content arrays, truncated error strings) and are important to verify correct behavior. The new tests exercise each gap directly and confirm the expected fallback behavior.


Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests

Warning

⚠️ Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • invalidhostthatdoesnotexist12345.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "invalidhostthatdoesnotexist12345.com"

See Network Configuration for more information.

Generated by Test Improver · ● 7.9M ·

Add missing test cases that cover previously uncovered branches in
circuit_breaker.go:

- RecordSuccess from OPEN state: exercises the 'else if prev !=
  circuitClosed' branch, covering the case where an in-flight request
  completes successfully after the circuit was opened
- Allow in HALF-OPEN with no probe in flight: exercises the defensive
  fallback path that permits a request through when probeInFlight is false
- extractRateLimitErrorText with non-map content items: covers the
  continue branch when a content array element is not a map
- isRateLimitToolResult with non-map content items: same as above for
  the rate limit tool result detector
- parseRateLimitResetFromText without a terminator character: covers the
  'end < 0' branch when the seconds value has no trailing s/]/) delimiter

Coverage improvements in circuit_breaker.go:
  Allow:                  90.0% -> 95.0%
  RecordSuccess:          91.7% -> 100.0%
  extractRateLimitErrorText: 90.9% -> 100.0%
  isRateLimitToolResult:  93.8% -> 100.0%
  parseRateLimitResetFromText: 91.7% -> 100.0%

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review April 20, 2026 13:32
Copilot AI review requested due to automatic review settings April 20, 2026 13:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves branch coverage for the server rate-limit circuit breaker by adding targeted tests for defensive/edge-case paths in the circuit breaker and rate-limit parsing helpers.

Changes:

  • Add test coverage for skipping non-map content entries in rate-limit tool result parsing/extraction.
  • Add test coverage for rate-limit reset parsing when the pattern has no terminator.
  • Add tests for circuit breaker edge states: RecordSuccess() when OPEN and Allow() when HALF-OPEN with probeInFlight=false.
Show a summary per file
File Description
internal/server/circuit_breaker_test.go Adds new table/subtests to exercise previously uncovered defensive branches in circuit breaker logic and rate-limit text parsing.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread internal/server/circuit_breaker_test.go Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@lpcox lpcox merged commit 51311d3 into main Apr 20, 2026
4 checks passed
@lpcox lpcox deleted the test-improver/circuit-breaker-coverage-77baa43fa5f839e9 branch April 20, 2026 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants