@@ -10,7 +10,10 @@ import (
1010
1111func TestCircuitBreaker (t * testing.T ) {
1212 config := & Config {
13- LogLevel : logging .LogLevelError , // Reduce noise in tests
13+ LogLevel : logging .LogLevelError , // Reduce noise in tests
14+ CircuitBreakerFailureThreshold : 5 ,
15+ CircuitBreakerResetTimeout : 60 * time .Second ,
16+ CircuitBreakerMaxRequests : 3 ,
1417 }
1518
1619 t .Run ("InitialState" , func (t * testing.T ) {
@@ -44,7 +47,7 @@ func TestCircuitBreaker(t *testing.T) {
4447 t .Run ("FailureThreshold" , func (t * testing.T ) {
4548 cb := newCircuitBreaker ("test-endpoint:6379" , config )
4649 testError := errors .New ("test error" )
47-
50+
4851 // Fail 4 times (below threshold of 5)
4952 for i := 0 ; i < 4 ; i ++ {
5053 err := cb .Execute (func () error {
@@ -57,15 +60,15 @@ func TestCircuitBreaker(t *testing.T) {
5760 t .Errorf ("Circuit should still be closed after %d failures" , i + 1 )
5861 }
5962 }
60-
63+
6164 // 5th failure should open the circuit
6265 err := cb .Execute (func () error {
6366 return testError
6467 })
6568 if err != testError {
6669 t .Errorf ("Expected test error, got %v" , err )
6770 }
68-
71+
6972 if cb .GetState () != CircuitBreakerOpen {
7073 t .Errorf ("Expected state %v, got %v" , CircuitBreakerOpen , cb .GetState ())
7174 }
@@ -92,8 +95,13 @@ func TestCircuitBreaker(t *testing.T) {
9295 })
9396
9497 t .Run ("HalfOpenTransition" , func (t * testing.T ) {
95- cb := newCircuitBreaker ("test-endpoint:6379" , config )
96- cb .resetTimeout = 100 * time .Millisecond // Short timeout for testing
98+ testConfig := & Config {
99+ LogLevel : logging .LogLevelError ,
100+ CircuitBreakerFailureThreshold : 5 ,
101+ CircuitBreakerResetTimeout : 100 * time .Millisecond , // Short timeout for testing
102+ CircuitBreakerMaxRequests : 3 ,
103+ }
104+ cb := newCircuitBreaker ("test-endpoint:6379" , testConfig )
97105 testError := errors .New ("test error" )
98106
99107 // Force circuit to open
@@ -125,9 +133,13 @@ func TestCircuitBreaker(t *testing.T) {
125133 })
126134
127135 t .Run ("HalfOpenToClosedTransition" , func (t * testing.T ) {
128- cb := newCircuitBreaker ("test-endpoint:6379" , config )
129- cb .resetTimeout = 50 * time .Millisecond
130- cb .maxRequests = 3
136+ testConfig := & Config {
137+ LogLevel : logging .LogLevelError ,
138+ CircuitBreakerFailureThreshold : 5 ,
139+ CircuitBreakerResetTimeout : 50 * time .Millisecond ,
140+ CircuitBreakerMaxRequests : 3 ,
141+ }
142+ cb := newCircuitBreaker ("test-endpoint:6379" , testConfig )
131143 testError := errors .New ("test error" )
132144
133145 // Force circuit to open
@@ -155,8 +167,13 @@ func TestCircuitBreaker(t *testing.T) {
155167 })
156168
157169 t .Run ("HalfOpenToOpenOnFailure" , func (t * testing.T ) {
158- cb := newCircuitBreaker ("test-endpoint:6379" , config )
159- cb .resetTimeout = 50 * time .Millisecond
170+ testConfig := & Config {
171+ LogLevel : logging .LogLevelError ,
172+ CircuitBreakerFailureThreshold : 5 ,
173+ CircuitBreakerResetTimeout : 50 * time .Millisecond ,
174+ CircuitBreakerMaxRequests : 3 ,
175+ }
176+ cb := newCircuitBreaker ("test-endpoint:6379" , testConfig )
160177 testError := errors .New ("test error" )
161178
162179 // Force circuit to open
@@ -216,7 +233,10 @@ func TestCircuitBreaker(t *testing.T) {
216233
217234func TestCircuitBreakerManager (t * testing.T ) {
218235 config := & Config {
219- LogLevel : logging .LogLevelError ,
236+ LogLevel : logging .LogLevelError ,
237+ CircuitBreakerFailureThreshold : 5 ,
238+ CircuitBreakerResetTimeout : 60 * time .Second ,
239+ CircuitBreakerMaxRequests : 3 ,
220240 }
221241
222242 t .Run ("GetCircuitBreaker" , func (t * testing.T ) {
@@ -289,4 +309,48 @@ func TestCircuitBreakerManager(t *testing.T) {
289309 t .Error ("Failure count should be reset to 0" )
290310 }
291311 })
312+
313+ t .Run ("ConfigurableParameters" , func (t * testing.T ) {
314+ config := & Config {
315+ LogLevel : logging .LogLevelError ,
316+ CircuitBreakerFailureThreshold : 10 ,
317+ CircuitBreakerResetTimeout : 30 * time .Second ,
318+ CircuitBreakerMaxRequests : 5 ,
319+ }
320+
321+ cb := newCircuitBreaker ("test-endpoint:6379" , config )
322+
323+ // Test that configuration values are used
324+ if cb .failureThreshold != 10 {
325+ t .Errorf ("Expected failureThreshold=10, got %d" , cb .failureThreshold )
326+ }
327+ if cb .resetTimeout != 30 * time .Second {
328+ t .Errorf ("Expected resetTimeout=30s, got %v" , cb .resetTimeout )
329+ }
330+ if cb .maxRequests != 5 {
331+ t .Errorf ("Expected maxRequests=5, got %d" , cb .maxRequests )
332+ }
333+
334+ // Test that circuit opens after configured threshold
335+ testError := errors .New ("test error" )
336+ for i := 0 ; i < 9 ; i ++ {
337+ err := cb .Execute (func () error { return testError })
338+ if err != testError {
339+ t .Errorf ("Expected test error, got %v" , err )
340+ }
341+ if cb .GetState () != CircuitBreakerClosed {
342+ t .Errorf ("Circuit should still be closed after %d failures" , i + 1 )
343+ }
344+ }
345+
346+ // 10th failure should open the circuit
347+ err := cb .Execute (func () error { return testError })
348+ if err != testError {
349+ t .Errorf ("Expected test error, got %v" , err )
350+ }
351+
352+ if cb .GetState () != CircuitBreakerOpen {
353+ t .Errorf ("Expected state %v, got %v" , CircuitBreakerOpen , cb .GetState ())
354+ }
355+ })
292356}
0 commit comments