MCPcopy
hub / github.com/redis/go-redis / TestCircuitBreakerManager

Function TestCircuitBreakerManager

maintnotifications/circuit_breaker_test.go:228–348  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

226}
227
228func TestCircuitBreakerManager(t *testing.T) {
229 config := &Config{
230 CircuitBreakerFailureThreshold: 5,
231 CircuitBreakerResetTimeout: 60 * time.Second,
232 CircuitBreakerMaxRequests: 3,
233 }
234
235 t.Run("GetCircuitBreaker", func(t *testing.T) {
236 manager := newCircuitBreakerManager(config)
237
238 cb1 := manager.GetCircuitBreaker("endpoint1:6379")
239 cb2 := manager.GetCircuitBreaker("endpoint2:6379")
240 cb3 := manager.GetCircuitBreaker("endpoint1:6379") // Same as cb1
241
242 if cb1 == cb2 {
243 t.Error("Different endpoints should have different circuit breakers")
244 }
245
246 if cb1 != cb3 {
247 t.Error("Same endpoint should return the same circuit breaker")
248 }
249 })
250
251 t.Run("GetAllStats", func(t *testing.T) {
252 manager := newCircuitBreakerManager(config)
253
254 // Create circuit breakers for different endpoints
255 cb1 := manager.GetCircuitBreaker("endpoint1:6379")
256 cb2 := manager.GetCircuitBreaker("endpoint2:6379")
257
258 // Execute some operations
259 cb1.Execute(func() error { return nil })
260 cb2.Execute(func() error { return errors.New("test error") })
261
262 stats := manager.GetAllStats()
263
264 if len(stats) != 2 {
265 t.Errorf("Expected 2 circuit breaker stats, got %d", len(stats))
266 }
267
268 // Check that we have stats for both endpoints
269 endpoints := make(map[string]bool)
270 for _, stat := range stats {
271 endpoints[stat.Endpoint] = true
272 }
273
274 if !endpoints["endpoint1:6379"] || !endpoints["endpoint2:6379"] {
275 t.Error("Missing stats for expected endpoints")
276 }
277 })
278
279 t.Run("Reset", func(t *testing.T) {
280 manager := newCircuitBreakerManager(config)
281 testError := errors.New("test error")
282
283 cb := manager.GetCircuitBreaker("test-endpoint:6379")
284
285 // Force circuit to open

Callers

nothing calls this directly

Calls 10

newCircuitBreakerManagerFunction · 0.85
newCircuitBreakerFunction · 0.85
GetCircuitBreakerMethod · 0.80
ExecuteMethod · 0.80
GetAllStatsMethod · 0.80
RunMethod · 0.45
ErrorMethod · 0.45
GetStateMethod · 0.45
ResetMethod · 0.45
LoadMethod · 0.45

Tested by

no test coverage detected