@@ -46,45 +46,45 @@ func TestCircuitBreakerCountBased(t *testing.T) {
4646 resp , err := c .R ().Get (ts .URL + "/500" )
4747 assertErrorIs (t , ErrCircuitBreakerOpen , err )
4848 assertNil (t , resp )
49- assertEqual (t , CircuitBreakerStateOpen , c .circuitBreaker .getState (), "expected open state after reaching failure threshold" )
49+ assertEqual (t , CircuitBreakerStateOpen , c .circuitBreaker .getState ())
5050
5151 time .Sleep (resetTimeout + 50 * time .Millisecond )
52- assertEqual (t , CircuitBreakerStateHalfOpen , c .circuitBreaker .getState (), "expected half-open state" )
52+ assertEqual (t , CircuitBreakerStateHalfOpen , c .circuitBreaker .getState ())
5353
5454 _ , err = c .R ().Get (ts .URL + "/500" )
5555 assertError (t , err )
56- assertEqual (t , CircuitBreakerStateOpen , c .circuitBreaker .getState (), "expected open state after failure in half-open" )
56+ assertEqual (t , CircuitBreakerStateOpen , c .circuitBreaker .getState ())
5757
5858 time .Sleep (resetTimeout + 50 * time .Millisecond )
59- assertEqual (t , CircuitBreakerStateHalfOpen , c .circuitBreaker .getState (), "expected half-open state" )
59+ assertEqual (t , CircuitBreakerStateHalfOpen , c .circuitBreaker .getState ())
6060
6161 for i := uint64 (0 ); i < successThreshold ; i ++ {
6262 _ , err := c .R ().Get (ts .URL + "/200" )
6363 assertNil (t , err )
6464 }
65- assertEqual (t , CircuitBreakerStateClosed , c .circuitBreaker .getState (), "expected closed state after success threshold" )
65+ assertEqual (t , CircuitBreakerStateClosed , c .circuitBreaker .getState ())
6666
6767 resp , err = c .R ().Get (ts .URL + "/200" )
6868 assertNil (t , err )
6969 assertEqual (t , http .StatusOK , resp .StatusCode ())
7070
7171 _ , err = c .R ().Get (ts .URL + "/500" )
7272 assertError (t , err )
73- assertEqual (t , 1 , c .circuitBreaker .sw .Get ().failures , "expected failure count to be 1 after single failure in closed state" )
73+ assertEqual (t , 1 , c .circuitBreaker .sw .Get ().failures )
7474
7575 time .Sleep (resetTimeout )
7676
7777 _ , err = c .R ().Get (ts .URL + "/500" )
7878 assertError (t , err )
79- assertEqual (t , 1 , c .circuitBreaker .sw .Get ().failures , "expected failure count to be 1 after single failure in closed state" )
79+ assertEqual (t , 1 , c .circuitBreaker .sw .Get ().failures )
8080}
8181
8282func TestCircuitBreaker5xxPolicy (t * testing.T ) {
8383 res1 := CircuitBreaker5xxPolicy (& http.Response {StatusCode : 500 })
84- assertTrue (t , res1 , "expected true for 5xx status code" )
84+ assertEqual (t , true , res1 )
8585
8686 res2 := CircuitBreaker5xxPolicy (& http.Response {StatusCode : 200 })
87- assertFalse (t , res2 , "expected false for non-5xx status code" )
87+ assertEqual (t , false , res2 )
8888}
8989
9090func TestCircuitBreakerCountBasedOpensAndAllow (t * testing.T ) {
@@ -94,30 +94,30 @@ func TestCircuitBreakerCountBasedOpensAndAllow(t *testing.T) {
9494 // expected allow when state is closed
9595 err1 := cb .allow ()
9696 assertNil (t , err1 )
97- assertEqual (t , 0 , cb .sw .Get ().failures , "expected allow when no failures initially" )
97+ assertEqual (t , 0 , cb .sw .Get ().failures )
9898
9999 // expected still closed after 1 failure
100100 cb .applyPolicies (fail )
101101 err2 := cb .allow ()
102102 assertNil (t , err2 )
103- assertEqual (t , 1 , cb .sw .Get ().failures , "expected still closed after 1 failure" )
103+ assertEqual (t , 1 , cb .sw .Get ().failures )
104104
105105 // expected open after reaching failure threshold
106106 cb .applyPolicies (fail )
107107 err3 := cb .allow ()
108- assertErrorIs (t , ErrCircuitBreakerOpen , err3 , "expected open after reaching failure threshold" )
108+ assertErrorIs (t , ErrCircuitBreakerOpen , err3 )
109109
110110 // time.Sleep to half-open state
111111 time .Sleep (25 * time .Millisecond )
112- assertEqual (t , CircuitBreakerStateHalfOpen , cb .getState (), "expected half-open state after reset timeout" )
112+ assertEqual (t , CircuitBreakerStateHalfOpen , cb .getState ())
113113
114114 // expected still half-open after a failure
115115 cb .applyPolicies (fail )
116- assertEqual (t , CircuitBreakerStateOpen , cb .getState (), "expected open state after failure in half-open" )
116+ assertEqual (t , CircuitBreakerStateOpen , cb .getState ())
117117
118118 // expected open state on allow
119119 err4 := cb .allow ()
120- assertErrorIs (t , ErrCircuitBreakerOpen , err4 , "expected open state on allow after failure in half-open" )
120+ assertErrorIs (t , ErrCircuitBreakerOpen , err4 )
121121}
122122
123123func TestCircuitBreakerCountBasedHalfOpenToClosedOnSuccess (t * testing.T ) {
@@ -128,7 +128,7 @@ func TestCircuitBreakerCountBasedHalfOpenToClosedOnSuccess(t *testing.T) {
128128 // expected open after failing threshold
129129 cb .applyPolicies (fail )
130130 err1 := cb .allow ()
131- assertErrorIs (t , ErrCircuitBreakerOpen , err1 , "expected open after failing threshold" )
131+ assertErrorIs (t , ErrCircuitBreakerOpen , err1 )
132132
133133 // wait for resetTimeout to transition to half-open
134134 deadline := time .Now ().Add (200 * time .Millisecond )
@@ -139,11 +139,11 @@ func TestCircuitBreakerCountBasedHalfOpenToClosedOnSuccess(t *testing.T) {
139139 time .Sleep (5 * time .Millisecond )
140140 }
141141 // expected half-open state after reset timeout
142- assertEqual (t , CircuitBreakerStateHalfOpen , cb .getState (), "expected half-open state after reset timeout" )
142+ assertEqual (t , CircuitBreakerStateHalfOpen , cb .getState ())
143143
144144 // on success in half-open, should move to closed
145145 cb .applyPolicies (ok )
146- assertEqual (t , CircuitBreakerStateClosed , cb .getState (), "expected closed state after success in half-open" )
146+ assertEqual (t , CircuitBreakerStateClosed , cb .getState ())
147147
148148 // expected allow when closed
149149 err := cb .allow ()
@@ -166,16 +166,18 @@ func TestCircuitBreakerRatioBasedOpenToClosed(t *testing.T) {
166166 // expected open after failures exceed ratio threshold
167167 cb .applyPolicies (fail )
168168 err2 := cb .allow ()
169- assertErrorIs (t , ErrCircuitBreakerOpen , err2 , "expected open after failures exceed ratio threshold" )
170-
169+ assertErrorIs (t , ErrCircuitBreakerOpen , err2 )
170+ // if err := cb.allow(); err != ErrCircuitBreakerOpen {
171+ // t.Fatalf("expected open after failures exceed ratio threshold, got %v", err)
172+ // }
171173 time .Sleep (25 * time .Millisecond )
172174
173175 // expected half-open state after reset timeout
174- assertEqual (t , CircuitBreakerStateHalfOpen , cb .getState (), "expected half-open state after reset timeout" )
176+ assertEqual (t , CircuitBreakerStateHalfOpen , cb .getState ())
175177
176178 // on success in half-open, should move to closed
177179 cb .applyPolicies (ok )
178- assertEqual (t , CircuitBreakerStateClosed , cb .getState (), "expected closed state after success in half-open" )
180+ assertEqual (t , CircuitBreakerStateClosed , cb .getState ())
179181}
180182
181183func TestCircuitBreakerNewStateAndPolicies (t * testing.T ) {
@@ -205,10 +207,10 @@ func TestCircuitBreakerAllowDuringHalfOpen(t *testing.T) {
205207 fail := & http.Response {StatusCode : 500 }
206208
207209 cb .applyPolicies (fail ) // opens
208- assertErrorIs (t , ErrCircuitBreakerOpen , cb .allow (), "expected open state" )
210+ assertErrorIs (t , ErrCircuitBreakerOpen , cb .allow ())
209211
210212 time .Sleep (25 * time .Millisecond ) // wait to transition to half-open
211- assertEqual (t , CircuitBreakerStateHalfOpen , cb .getState (), "expected half-open state" )
213+ assertEqual (t , CircuitBreakerStateHalfOpen , cb .getState ())
212214 assertNil (t , cb .allow ())
213215}
214216
@@ -224,8 +226,8 @@ func TestCircuitBreakerOnTriggerHooks(t *testing.T) {
224226
225227 cb .onTriggerHooks (nil , ErrCircuitBreakerOpen )
226228
227- assertTrue (t , called , "expected onTrigger hook to be called" )
228- assertEqual (t , ErrCircuitBreakerOpen , gotErr , "expected error to be passed to onTrigger hook" )
229+ assertEqual (t , true , called )
230+ assertEqual (t , ErrCircuitBreakerOpen , gotErr )
229231}
230232
231233func TestCircuitBreakerOnStateChangeHooks (t * testing.T ) {
@@ -241,9 +243,9 @@ func TestCircuitBreakerOnStateChangeHooks(t *testing.T) {
241243
242244 cb .onStateChangeHooks (CircuitBreakerStateClosed , CircuitBreakerStateOpen )
243245
244- assertTrue ( t , called )
245- assertEqual (t , CircuitBreakerStateClosed , oldState , "expected old state to be passed to onStateChange hook" )
246- assertEqual (t , CircuitBreakerStateOpen , newState , "expected new state to be passed to onStateChange hook" )
246+ assertEqual ( t , true , called )
247+ assertEqual (t , CircuitBreakerStateClosed , oldState )
248+ assertEqual (t , CircuitBreakerStateOpen , newState )
247249}
248250
249251func TestCircuitBreakerMultipleHooksAreCalled (t * testing.T ) {
@@ -254,14 +256,14 @@ func TestCircuitBreakerMultipleHooksAreCalled(t *testing.T) {
254256 cb .OnTrigger (func (_ * Request , _ error ) { triggerCount ++ })
255257
256258 cb .onTriggerHooks (nil , ErrCircuitBreakerOpen )
257- assertEqual (t , 2 , triggerCount , "expected both trigger hooks to be called" )
259+ assertEqual (t , 2 , triggerCount )
258260
259261 stateCount := 0
260262 cb .OnStateChange (func (_ , _ CircuitBreakerState ) { stateCount ++ })
261263 cb .OnStateChange (func (_ , _ CircuitBreakerState ) { stateCount ++ })
262264
263265 cb .onStateChangeHooks (CircuitBreakerStateClosed , CircuitBreakerStateHalfOpen )
264- assertEqual (t , 2 , stateCount , "expected both state change hooks to be called" )
266+ assertEqual (t , 2 , stateCount )
265267}
266268
267269func TestCircuitBreakerConcurrentOnTriggerRegistration (t * testing.T ) {
@@ -283,7 +285,7 @@ func TestCircuitBreakerConcurrentOnTriggerRegistration(t *testing.T) {
283285
284286 cb .onTriggerHooks (nil , ErrCircuitBreakerOpen )
285287 got := atomic .LoadInt32 (& cnt )
286- assertEqual (t , int32 (n ), got , " expected N hooks executed" )
288+ assertEqual (t , int32 (n ), got ) // expected N hooks executed
287289}
288290
289291func TestCircuitBreakerConcurrentOnStateChangeRegistration (t * testing.T ) {
@@ -305,82 +307,18 @@ func TestCircuitBreakerConcurrentOnStateChangeRegistration(t *testing.T) {
305307
306308 cb .onStateChangeHooks (CircuitBreakerStateClosed , CircuitBreakerStateOpen )
307309 got := atomic .LoadInt32 (& cnt )
308- assertEqual (t , int32 (n ), got , " expected N state change hooks executed" )
310+ assertEqual (t , int32 (n ), got ) // expected N state change hooks executed
309311}
310312
311- func TestCircuitBreakerSlidingWindow1SetInterval (t * testing.T ) {
313+ func TestCircuitBreakerSlidingWindowSetInterval (t * testing.T ) {
312314 cb := NewCircuitBreakerWithCount (2 , 1 , 100 * time .Millisecond )
313315
314316 // Verify initial interval
315- assertEqual (t , 100 * time .Millisecond , cb .sw .interval , "initial interval mismatch" )
317+ assertEqual (t , 100 * time .Millisecond , cb .sw .interval )
316318
317319 // Change interval to a longer duration
318320 cb .sw .SetInterval (200 * time .Millisecond )
319321
320322 // Verify interval was changed
321- assertEqual (t , 200 * time .Millisecond , cb .sw .interval , "interval not updated correctly" )
322- }
323-
324- func TestCircuitBreakerSlidingWindow2SetInterval (t * testing.T ) {
325- sw := newSlidingWindow (func () totalAndFailures { return totalAndFailures {} }, 100 * time .Millisecond , 5 )
326- assertEqual (t , 100 * time .Millisecond , sw .interval , "initial interval mismatch" )
327-
328- sw .SetInterval (250 * time .Millisecond )
329- assertEqual (t , 250 * time .Millisecond , sw .interval , "interval not updated correctly" )
330- }
331-
332- func TestCircuitBreakerSlidingWindowConcurrentAddGet (t * testing.T ) {
333- sw := newSlidingWindow (func () totalAndFailures { return totalAndFailures {} }, 200 * time .Millisecond , 10 )
334-
335- var wg sync.WaitGroup
336- n := 200
337- wg .Add (n )
338- for i := 0 ; i < n ; i ++ {
339- go func () {
340- sw .Add (totalAndFailures {total : 1 , failures : 0 })
341- wg .Done ()
342- }()
343- }
344- wg .Wait ()
345-
346- got := sw .Get ()
347- assertEqual (t , n , got .total , "concurrent adds: expected total count mismatch" )
348- }
349-
350- func TestCircuitBreakerTotalAndFailuresOperations (t * testing.T ) {
351- a := totalAndFailures {total : 2 , failures : 1 }
352- b := totalAndFailures {total : 3 , failures : 2 }
353-
354- c := a .op (b )
355- assertEqual (t , 5 , c .total , "op result incorrect, want total 5" )
356- assertEqual (t , 3 , c .failures , "op result incorrect, want failures 3" )
357-
358- inv := c .inverse ()
359- assertEqual (t , - 5 , inv .total , "inverse result incorrect, want total -5" )
360- assertEqual (t , - 3 , inv .failures , "inverse result incorrect, want failures -3" )
361-
362- empty := c .empty ()
363- assertEqual (t , 0 , empty .total , "empty result incorrect, want total 0" )
364- assertEqual (t , 0 , empty .failures , "empty result incorrect, want failures 0" )
365- }
366-
367- func TestCircuitBreakerSlidingWindowResetWhenElapsedExceedsBuckets (t * testing.T ) {
368- interval := 100 * time .Millisecond
369- sw := newSlidingWindow (func () totalAndFailures { return totalAndFailures {} }, interval , 4 )
370-
371- // Pre-populate total and buckets to non-zero values
372- sw .values [0 ] = totalAndFailures {total : 5 , failures : 2 }
373- sw .values [1 ] = totalAndFailures {total : 3 , failures : 1 }
374- sw .total = sw .values [0 ].op (sw .values [1 ]).op (sw .total )
375-
376- // Force lastStart far in the past so bucketsToAdvance >= len(values) path is taken
377- sw .lastStart = sw .lastStart .Add (- time .Duration (10 ) * interval )
378-
379- // Add a new value; should reset buckets and only this value remains
380- sw .Add (totalAndFailures {total : 1 , failures : 1 })
381-
382- got := sw .Get ()
383- assertEqual (t , 1 , got .total , "after reset expected total=1" )
384- assertEqual (t , 1 , got .failures , "after reset expected failures=1" )
385- assertEqual (t , 0 , sw .idx , "expected idx reset to 0" )
323+ assertEqual (t , 200 * time .Millisecond , cb .sw .interval )
386324}
0 commit comments