@@ -16,17 +16,19 @@ import (
1616 "github.com/coder/aibridge"
1717 "github.com/coder/aibridge/circuitbreaker"
1818 "github.com/coder/aibridge/config"
19+ "github.com/coder/aibridge/mcp"
1920 "github.com/coder/aibridge/metrics"
2021 "github.com/coder/aibridge/provider"
21- "github.com/coder/aibridge/mcp"
2222 "github.com/prometheus/client_golang/prometheus"
2323 promtest "github.com/prometheus/client_golang/prometheus/testutil"
2424 "github.com/stretchr/testify/assert"
2525 "github.com/stretchr/testify/require"
2626 "go.opentelemetry.io/otel"
2727)
2828
29- func TestCircuitBreaker_WithNewRequestBridge (t * testing.T ) {
29+ // TestCircuitBreaker_FullRecoveryCycle tests the complete circuit breaker lifecycle:
30+ // closed → open (after consecutive failures) → half-open (after timeout) → closed (after successful request)
31+ func TestCircuitBreaker_FullRecoveryCycle (t * testing.T ) {
3032 t .Parallel ()
3133
3234 type testCase struct {
@@ -136,12 +138,14 @@ func TestCircuitBreaker_WithNewRequestBridge(t *testing.T) {
136138 mockSrv .Start ()
137139
138140 makeRequest := func () * http.Response {
139- req , _ := http .NewRequest ("POST" , mockSrv .URL + "/" + tc .providerName + tc .endpoint , strings .NewReader (tc .requestBody ))
141+ req , err := http .NewRequest ("POST" , mockSrv .URL + "/" + tc .providerName + tc .endpoint , strings .NewReader (tc .requestBody ))
142+ require .NoError (t , err )
140143 req .Header .Set ("Content-Type" , "application/json" )
141144 tc .setupHeaders (req )
142145 resp , err := http .DefaultClient .Do (req )
143146 require .NoError (t , err )
144- _ , _ = io .ReadAll (resp .Body )
147+ _ , err = io .ReadAll (resp .Body )
148+ require .NoError (t , err )
145149 resp .Body .Close ()
146150 return resp
147151 }
@@ -203,6 +207,8 @@ func TestCircuitBreaker_WithNewRequestBridge(t *testing.T) {
203207 }
204208}
205209
210+ // TestCircuitBreaker_HalfOpenFailure tests that a failed request in half-open state
211+ // returns the circuit to open: closed → open → half-open → open
206212func TestCircuitBreaker_HalfOpenFailure (t * testing.T ) {
207213 t .Parallel ()
208214
@@ -253,13 +259,15 @@ func TestCircuitBreaker_HalfOpenFailure(t *testing.T) {
253259 mockSrv .Start ()
254260
255261 makeRequest := func () * http.Response {
256- req , _ := http .NewRequest ("POST" , mockSrv .URL + "/openai/v1/chat/completions" ,
262+ req , err := http .NewRequest ("POST" , mockSrv .URL + "/openai/v1/chat/completions" ,
257263 strings .NewReader (`{"model":"gpt-4o","messages":[{"role":"user","content":"hi"}]}` ))
264+ require .NoError (t , err )
258265 req .Header .Set ("Content-Type" , "application/json" )
259266 req .Header .Set ("Authorization" , "Bearer test-key" )
260267 resp , err := http .DefaultClient .Do (req )
261268 require .NoError (t , err )
262- _ , _ = io .ReadAll (resp .Body )
269+ _ , err = io .ReadAll (resp .Body )
270+ require .NoError (t , err )
263271 resp .Body .Close ()
264272 return resp
265273 }
0 commit comments