feat: Add ResetCircuitBreaker helper

This commit is contained in:
2026-08-16 03:41:26 +03:00
parent 26c4be2d3a
commit 286cb8653a
4 changed files with 70 additions and 6 deletions

View File

@@ -319,6 +319,19 @@ func newCircuitBreaker() *circuitBreaker {
}
}
// ResetCircuitBreaker resets the circuit breaker to its initial closed state.
// This is useful for testing or recovery scenarios where the circuit needs to be
// manually reset without waiting for the timeout.
func (cb *circuitBreaker) ResetCircuitBreaker() {
cb.mu.Lock()
defer cb.mu.Unlock()
cb.state = closed
cb.failures = 0
cb.successes = 0
cb.openSince = time.Time{}
}
func (cb *circuitBreaker) allow() bool {
cb.mu.Lock()
defer cb.mu.Unlock()

View File

@@ -307,6 +307,43 @@ func TestIsRetryableError(t *testing.T) {
}
}
func TestResetCircuitBreaker(t *testing.T) {
cb := newCircuitBreaker()
// Record failures to open the circuit
cb.recordFailure()
cb.recordFailure()
cb.recordFailure()
if cb.state != open {
t.Errorf("expected state open after 3 failures, got %v", cb.state)
}
// Reset the circuit breaker
cb.ResetCircuitBreaker()
// Should be back to closed state
if cb.state != closed {
t.Errorf("expected state closed after reset, got %v", cb.state)
}
if cb.failures != 0 {
t.Errorf("expected failures to be 0 after reset, got %d", cb.failures)
}
if cb.successes != 0 {
t.Errorf("expected successes to be 0 after reset, got %d", cb.successes)
}
if cb.openSince != (time.Time{}) {
t.Errorf("expected openSince to be zero after reset, got %v", cb.openSince)
}
// After reset, allow() should return true (circuit closed)
for i := 0; i < 10; i++ {
if !cb.allow() {
t.Fatalf("expected allow() to return true after reset, attempt %d", i)
}
}
}
// Test Response parsing
func TestResponseParsing(t *testing.T) {
// Test with a valid JSON response