Conversation
These two functions in internal/server/unified.go had no direct test coverage despite containing meaningful branching logic: - buildCircuitBreakers: nil config fast-path, empty servers, single server with custom threshold/cooldown, multiple servers, zero values falling back to defaults, independence between server CBs. - getCircuitBreaker: returns pre-configured CB, lazy creation for unknown server, nil map initialisation, caching (same pointer on repeated calls), different servers get different instances, newly created CB starts in CLOSED state. Tests follow the table-driven and t.Parallel() patterns already used in internal/server/circuit_breaker_test.go. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds direct unit tests in internal/server to cover the previously untested buildCircuitBreakers and (*UnifiedServer).getCircuitBreaker logic (defaults, per-server config mapping, and lazy initialization/caching).
Changes:
- Introduces new tests for
buildCircuitBreakersacross nil/empty configs, multi-server configs, and default fallback behavior. - Adds tests for
getCircuitBreakercovering existing entries, lazy map initialization, default creation, and caching semantics.
Show a summary per file
| File | Description |
|---|---|
| internal/server/circuit_breaker_builder_test.go | Adds unit tests for circuit breaker construction from config and lazy retrieval/creation/caching on UnifiedServer. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comments suppressed due to low confidence (1)
internal/server/circuit_breaker_builder_test.go:129
- This subtest indexes
cbs["github"]/cbs["slack"]and calls methods on the result without asserting the keys exist. IfbuildCircuitBreakersregresses and omits one entry, the test will panic instead of producing an actionable assertion failure. Addrequire.Contains/require.NotNilchecks (and ideallyrequire.Len(t, cbs, 2)) before dereferencing.
cbs := buildCircuitBreakers(cfg)
// Open the github circuit breaker by hitting the threshold.
cbs["github"].RecordRateLimit(time.Time{})
assert.Equal(t, circuitOpen, cbs["github"].State(), "github CB should be open after 1 error (threshold=1)")
// slack's circuit breaker should still be closed (threshold=3, no errors recorded).
assert.Equal(t, circuitClosed, cbs["slack"].State(), "slack CB should remain CLOSED")
assert.NoError(t, cbs["slack"].Allow(), "slack CB should still allow requests")
- Files reviewed: 1/1 changed files
- Comments generated: 2
| cbs := buildCircuitBreakers(cfg) | ||
| for serverID, cb := range cbs { | ||
| assert.Equal(t, circuitClosed, cb.State(), "circuit breaker for %s should start CLOSED", serverID) | ||
| assert.NoError(t, cb.Allow(), "CLOSED circuit breaker for %s should allow requests", serverID) | ||
| } |
There was a problem hiding this comment.
The loop only asserts properties for entries returned by ranging over cbs. If buildCircuitBreakers mistakenly returns an empty map, this subtest would pass without running any assertions. Add a require.Len(t, cbs, 2) (or at least require.NotEmpty) before the loop to ensure the test actually exercises the expected circuit breakers.
| cb := cbs["github"] | ||
| // newCircuitBreaker replaces zero values with defaults. | ||
| assert.Equal(t, DefaultRateLimitThreshold, cb.threshold, "zero threshold should use default") | ||
| assert.Equal(t, DefaultRateLimitCooldown, cb.cooldown, "zero cooldown should use default") |
There was a problem hiding this comment.
cb := cbs["github"] can be nil if the map doesn't contain the expected key, which would cause a panic when accessing cb.threshold/cb.cooldown. Prefer require.Contains(t, cbs, "github") (or require.NotNil(t, cb)) before dereferencing so failures are reported cleanly.
This issue also appears on line 121 of the same file.
|
@copilot address this review feedback #4466 (review) |
Agent-Logs-Url: https://github.com/github/gh-aw-mcpg/sessions/f693fb51-ae62-4beb-b1b6-a61ef74b3502 Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Addressed in ae6a98e:
|
Test Coverage Improvement:
buildCircuitBreakersandgetCircuitBreakerFunction Analyzed
internal/serverbuildCircuitBreakers,getCircuitBreakerinternal/server/unified.goWhy These Functions?
buildCircuitBreakersandgetCircuitBreakercontained meaningful branching logic — nil-config fast-path, zero-value fallbacks to defaults, map lazy initialisation, caching semantics — yet had zero direct test coverage. They were identified by scanning all source files against existing*_test.gofiles and confirmed by grepping test files for their names.Tests Added
New file:
internal/server/circuit_breaker_builder_test.goTestBuildCircuitBreakers(6 sub-tests)nil config returns empty map— fast-path guardempty servers returns empty map— zero-length iterationsingle server uses configured threshold and cooldown— happy path with custom valuesmultiple servers each get their own circuit breaker— N-server map, each keyed correctlyzero threshold falls back to default— covers thenewCircuitBreakerdefault-fill brancheach circuit breaker starts in CLOSED state— functional state verificationcircuit breakers are independent per server— tripping one CB does not affect anotherTestGetCircuitBreaker(6 sub-tests)returns existing circuit breaker when present— pre-configured CB returned unchangedcreates default circuit breaker for unknown server— lazy creation pathnil circuitBreakers map is lazily initialised— coversus.circuitBreakers == nilbranchcached: second call returns same instance— pointer identity checkdifferent servers return different instances— map isolationnewly created default CB starts CLOSED— functional state verificationTest Execution
Tests are structured following existing project patterns (
t.Parallel(),assert/requirefrom testify, table-driven sub-tests). CI will run full verification.Generated by Test Coverage Improver
Next run will target the next most complex under-tested function
Warning
The following domain was blocked by the firewall during workflow execution:
proxy.golang.orgTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.