Files
Authorization/helper/circuit_breaker_test.go
T
2026-01-27 10:45:15 +08:00

747 lines
17 KiB
Go

package helper
import (
"errors"
"os"
"sync"
"testing"
"time"
)
func TestMain(m *testing.M) {
os.Setenv("GO_ENV", "development")
code := m.Run()
os.Unsetenv("GO_ENV")
os.Exit(code)
}
func TestNewCircuitBreaker(t *testing.T) {
tests := []struct {
name string
cbName string
maxFailures int
timeout time.Duration
wantState CircuitState
}{
{
name: "creates circuit breaker with correct defaults",
cbName: "test-service",
maxFailures: 5,
timeout: 2 * time.Second,
wantState: StateClosed,
},
{
name: "creates circuit breaker with different parameters",
cbName: "db-service",
maxFailures: 3,
timeout: 1 * time.Second,
wantState: StateClosed,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cb := NewCircuitBreaker(tt.cbName, tt.maxFailures, tt.timeout)
if cb.name != tt.cbName {
t.Errorf("name = %v, want %v", cb.name, tt.cbName)
}
if cb.maxFailures != tt.maxFailures {
t.Errorf("maxFailures = %v, want %v", cb.maxFailures, tt.maxFailures)
}
if cb.timeout != tt.timeout {
t.Errorf("timeout = %v, want %v", cb.timeout, tt.timeout)
}
if cb.state != tt.wantState {
t.Errorf(StateMismatchMessage, cb.state, tt.wantState)
}
if cb.resetTimeout != 30*time.Second {
t.Errorf("resetTimeout = %v, want %v", cb.resetTimeout, 30*time.Second)
}
if cb.failures != 0 {
t.Errorf("failures = %v, want 0", cb.failures)
}
})
}
}
func TestCircuitBreakerCallSuccess(t *testing.T) {
cb := NewCircuitBreaker("test", 3, 1*time.Second)
successFn := func() error {
return nil
}
err := Call(cb, successFn)
if err != nil {
t.Errorf("Call() error = %v, want nil", err)
}
if GetState(cb) != StateClosed {
t.Errorf(StateMismatchMessage, GetState(cb), StateClosed)
}
}
func TestCircuitBreakerCallFailuresOpenCircuit(t *testing.T) {
cb := NewCircuitBreaker("test", 3, 1*time.Second)
failFn := func() error {
return errors.New(ServiceError)
}
// First 2 failures - circuit should stay closed
for i := 0; i < 2; i++ {
err := Call(cb, failFn)
if err == nil {
t.Errorf("Call() iteration %d: expected error, got nil", i)
}
if GetState(cb) != StateClosed {
t.Errorf("iteration %d: state = %v, want %v", i, GetState(cb), StateClosed)
}
}
// 3rd failure - circuit should open
err := Call(cb, failFn)
if err == nil {
t.Error("Call() expected error, got nil")
}
if GetState(cb) != StateOpen {
t.Errorf(StateMismatchMessage, GetState(cb), StateOpen)
}
// Next call should immediately return circuit breaker error
err = Call(cb, failFn)
if !IsCircuitBreakerError(err) {
t.Errorf("expected CircuitBreakerError, got %v", err)
}
}
func TestCircuitBreakerCallOpenToHalfOpen(t *testing.T) {
cb := NewCircuitBreaker("test", 2, 1*time.Second)
cb.resetTimeout = 100 * time.Millisecond // Shorter reset for testing
failFn := func() error {
return errors.New(ServiceError)
}
// Open the circuit
Call(cb, failFn)
Call(cb, failFn)
if GetState(cb) != StateOpen {
t.Fatalf(StateMismatchMessage, GetState(cb), StateOpen)
}
// Wait for reset timeout
time.Sleep(150 * time.Millisecond)
// Next call should transition to HalfOpen
successFn := func() error {
return nil
}
err := Call(cb, successFn)
if err != nil {
t.Errorf("Call() error = %v, want nil", err)
}
// Should now be closed
if GetState(cb) != StateClosed {
t.Errorf(StateMismatchMessage, GetState(cb), StateClosed)
}
}
func TestCircuitBreakerCallHalfOpenFailReturnsToOpen(t *testing.T) {
cb := NewCircuitBreaker("test", 2, 1*time.Second)
cb.resetTimeout = 100 * time.Millisecond
failFn := func() error {
return errors.New(ServiceError)
}
// Open the circuit
Call(cb, failFn)
Call(cb, failFn)
if GetState(cb) != StateOpen {
t.Fatalf(StateMismatchMessage, GetState(cb), StateOpen)
}
// Wait for reset timeout to transition to HalfOpen
time.Sleep(150 * time.Millisecond)
// Fail in HalfOpen state - should return to Open
err := Call(cb, failFn)
if err == nil {
t.Error("Call() expected error, got nil")
}
if GetState(cb) != StateOpen {
t.Errorf(StateMismatchMessage, GetState(cb), StateOpen)
}
}
func TestCircuitBreakerCallGradualFailureReduction(t *testing.T) {
cb := NewCircuitBreaker("test", 5, 1*time.Second)
failFn := func() error {
return errors.New(ServiceError)
}
successFn := func() error {
return nil
}
// Add 3 failures
for i := 0; i < 3; i++ {
Call(cb, failFn)
}
cb.mutex.RLock()
failures := cb.failures
cb.mutex.RUnlock()
if failures != 3 {
t.Fatalf("failures = %v, want 3", failures)
}
// One success should reduce failure count
Call(cb, successFn)
cb.mutex.RLock()
failures = cb.failures
cb.mutex.RUnlock()
if failures != 2 {
t.Errorf("failures after success = %v, want 2", failures)
}
}
func TestCircuitBreakerGetState(t *testing.T) {
tests := []struct {
name string
setupFunc func(*CircuitBreaker)
wantState CircuitState
}{
{
name: "returns closed state",
setupFunc: func(cb *CircuitBreaker) {
cb.state = StateClosed
},
wantState: StateClosed,
},
{
name: "returns open state",
setupFunc: func(cb *CircuitBreaker) {
cb.state = StateOpen
},
wantState: StateOpen,
},
{
name: "returns half-open state",
setupFunc: func(cb *CircuitBreaker) {
cb.state = StateHalfOpen
},
wantState: StateHalfOpen,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cb := NewCircuitBreaker("test", 3, 1*time.Second)
tt.setupFunc(cb)
got := GetState(cb)
if got != tt.wantState {
t.Errorf("GetState() = %v, want %v", got, tt.wantState)
}
})
}
}
func TestCircuitBreakerReset(t *testing.T) {
cb := NewCircuitBreaker("test", 2, 1*time.Second)
// Open the circuit
failFn := func() error {
return errors.New("error")
}
Call(cb, failFn)
Call(cb, failFn)
if GetState(cb) != StateOpen {
t.Fatalf(StateMismatchMessage, GetState(cb), StateOpen)
}
// Reset the circuit breaker
Reset(cb)
if GetState(cb) != StateClosed {
t.Errorf("state after Reset() = %v, want %v", GetState(cb), StateClosed)
}
cb.mutex.RLock()
failures := cb.failures
cb.mutex.RUnlock()
if failures != 0 {
t.Errorf("failures after Reset() = %v, want 0", failures)
}
}
func TestCircuitBreakerErrorError(t *testing.T) {
tests := []struct {
name string
err *CircuitBreakerError
wantError string
}{
{
name: "formats error message correctly",
err: &CircuitBreakerError{
Name: "database",
State: "open",
},
wantError: "circuit breaker 'database' is open",
},
{
name: "formats error message with different state",
err: &CircuitBreakerError{
Name: "redis",
State: "half-open",
},
wantError: "circuit breaker 'redis' is half-open",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.err.Error()
if got != tt.wantError {
t.Errorf("Error() = %v, want %v", got, tt.wantError)
}
})
}
}
func TestIsCircuitBreakerError(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{
name: "returns true for CircuitBreakerError",
err: &CircuitBreakerError{
Name: "test",
State: "open",
},
want: true,
},
{
name: "returns false for regular error",
err: errors.New("regular error"),
want: false,
},
{
name: "returns false for nil error",
err: nil,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsCircuitBreakerError(tt.err)
if got != tt.want {
t.Errorf("IsCircuitBreakerError() = %v, want %v", got, tt.want)
}
})
}
}
func TestCircuitBreakerConcurrency(t *testing.T) {
cb := NewCircuitBreaker("test", 10, 1*time.Second)
var wg sync.WaitGroup
successCount := 0
errorCount := 0
var countMutex sync.Mutex
// Run 100 concurrent operations
for i := 0; i < 100; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
fn := func() error {
// Alternate between success and failure
if index%2 == 0 {
return nil
}
return errors.New("error")
}
err := Call(cb, fn)
countMutex.Lock()
if err == nil {
successCount++
} else {
errorCount++
}
countMutex.Unlock()
}(i)
}
wg.Wait()
// Verify that all operations completed
if successCount+errorCount != 100 {
t.Errorf("total operations = %v, want 100", successCount+errorCount)
}
// Verify circuit breaker is in a valid state
state := GetState(cb)
if state != StateClosed && state != StateOpen && state != StateHalfOpen {
t.Errorf("invalid state = %v", state)
}
}
func TestCircuitBreakerOpenCircuitRejectsImmediately(t *testing.T) {
cb := NewCircuitBreaker("test", 1, 1*time.Second)
// Open the circuit
failFn := func() error {
return errors.New("error")
}
Call(cb, failFn)
if GetState(cb) != StateOpen {
t.Fatalf(StateMismatchMessage, GetState(cb), StateOpen)
}
// Try calling with a function that should not execute
executed := false
testFn := func() error {
executed = true
return nil
}
err := Call(cb, testFn)
if !IsCircuitBreakerError(err) {
t.Errorf("expected CircuitBreakerError, got %v", err)
}
if executed {
t.Error("function should not have executed when circuit is open")
}
}
func BenchmarkCircuitBreakerCallSuccess(b *testing.B) {
cb := NewCircuitBreaker("test", 5, 1*time.Second)
fn := func() error {
return nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Call(cb, fn)
}
}
func BenchmarkCircuitBreakerCallOpen(b *testing.B) {
cb := NewCircuitBreaker("test", 1, 1*time.Second)
// Open the circuit
Call(cb, func() error {
return errors.New("error")
})
fn := func() error {
return nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Call(cb, fn)
}
}
func TestCircuitBreakerStateTransitions(t *testing.T) {
cb := NewCircuitBreaker("test", 2, 1*time.Second)
cb.resetTimeout = 100 * time.Millisecond
// Start: Closed
if GetState(cb) != StateClosed {
t.Errorf("Initial state should be Closed, got %v", GetState(cb))
}
// First failure - still closed
Call(cb, func() error { return errors.New("error") })
if GetState(cb) != StateClosed {
t.Error("Should remain Closed after first failure")
}
// Second failure - should open
Call(cb, func() error { return errors.New("error") })
if GetState(cb) != StateOpen {
t.Error("Should be Open after reaching max failures")
}
// Wait for potential half-open transition
time.Sleep(150 * time.Millisecond)
// After reset timeout, next call should attempt in half-open
// Successful call should close circuit
err := Call(cb, func() error { return nil })
if err != nil {
t.Logf("Call returned error: %v (timing may affect state transition)", err)
}
// Circuit should eventually close after successful call
finalState := GetState(cb)
if finalState != StateClosed && finalState != StateHalfOpen {
t.Logf("Final state is %v (expected Closed or HalfOpen)", finalState)
}
}
func TestCircuitBreakerZeroMaxFailures(t *testing.T) {
cb := NewCircuitBreaker("test", 0, 1*time.Second)
// Even one failure should open circuit when maxFailures is 0
err := Call(cb, func() error { return errors.New("error") })
if GetState(cb) != StateOpen {
t.Error("Circuit should open immediately with maxFailures=0")
}
if err == nil {
t.Error("Should return error when circuit is open")
}
}
func TestCircuitBreakerNegativeMaxFailures(t *testing.T) {
// Negative maxFailures should be treated as invalid, but won't panic
cb := NewCircuitBreaker("test", -1, 1*time.Second)
// Circuit should not open with negative maxFailures
Call(cb, func() error { return errors.New("error") })
Call(cb, func() error { return errors.New("error") })
// Should handle gracefully
if cb == nil {
t.Error("Circuit breaker should not be nil")
}
}
func TestCircuitBreakerVeryShortTimeout(t *testing.T) {
cb := NewCircuitBreaker("test", 1, 1*time.Nanosecond)
cb.resetTimeout = 1 * time.Nanosecond
// Open circuit
Call(cb, func() error { return errors.New("error") })
// Very short timeout means it should transition quickly
time.Sleep(10 * time.Millisecond)
state := GetState(cb)
if state != StateHalfOpen && state != StateClosed {
t.Logf("State is %v, which is acceptable with very short timeout", state)
}
}
func TestCircuitBreakerMultipleSuccessesAfterFailure(t *testing.T) {
cb := NewCircuitBreaker("test", 3, 1*time.Second)
// Add one failure
Call(cb, func() error { return errors.New("error") })
// Multiple successes should reset failure count
for i := 0; i < 10; i++ {
err := Call(cb, func() error { return nil })
if err != nil {
t.Errorf("Successful calls should not return error: %v", err)
}
}
// Circuit should still be closed
if GetState(cb) != StateClosed {
t.Error("Circuit should remain closed after successes")
}
// Should need 3 failures again to open
Call(cb, func() error { return errors.New("error") })
Call(cb, func() error { return errors.New("error") })
if GetState(cb) == StateOpen {
t.Error("Should not be open yet, need one more failure")
}
}
func TestCircuitBreakerHighConcurrency(t *testing.T) {
cb := NewCircuitBreaker("test", 10, 1*time.Second)
concurrency := 100
done := make(chan bool, concurrency)
errChan := make(chan error, concurrency)
for i := 0; i < concurrency; i++ {
go func(idx int) {
err := Call(cb, func() error {
if idx%3 == 0 {
return errors.New("error")
}
return nil
})
errChan <- err
done <- true
}(i)
}
for i := 0; i < concurrency; i++ {
<-done
}
close(errChan)
// Check that no panics occurred and circuit handled concurrency
errorCount := 0
for err := range errChan {
if err != nil {
errorCount++
}
}
if errorCount == 0 {
t.Error("Expected some errors from concurrent execution")
}
}
func TestCircuitBreakerHalfOpenSingleRequest(t *testing.T) {
cb := NewCircuitBreaker("test", 1, 1*time.Second)
cb.resetTimeout = 50 * time.Millisecond
// Open circuit
Call(cb, func() error { return errors.New("error") })
if GetState(cb) != StateOpen {
t.Error("Circuit should be open")
}
// Wait for half-open transition
time.Sleep(100 * time.Millisecond)
// Circuit should transition to half-open on next call
// The first call in half-open that fails should reopen
err := Call(cb, func() error { return errors.New("error") })
if err == nil {
t.Error("Expected error from failed call")
}
// After failed half-open attempt, should be open again
state := GetState(cb)
if state != StateOpen && state != StateHalfOpen {
t.Logf("Circuit state is %v (expected Open or HalfOpen due to timing)", state)
}
}
func TestCircuitBreakerSuccessResetsFailureCount(t *testing.T) {
cb := NewCircuitBreaker("test", 3, 1*time.Second)
// 2 failures
Call(cb, func() error { return errors.New("error 1") })
Call(cb, func() error { return errors.New("error 2") })
if GetState(cb) != StateClosed {
t.Error("Should still be closed with 2 failures")
}
// Success should reduce failure count
Call(cb, func() error { return nil })
// Check that failure count was reduced (should still be closed)
if GetState(cb) != StateClosed {
t.Error("Should still be closed after one success")
}
// Now add more failures - should take 3 to open since count was reduced
Call(cb, func() error { return errors.New("error 3") })
Call(cb, func() error { return errors.New("error 4") })
// May or may not be closed depending on exact implementation
state := GetState(cb)
if state != StateClosed && state != StateOpen {
t.Errorf("Unexpected state: %v", state)
}
// One more failure should definitely open it if not already
Call(cb, func() error { return errors.New("error 5") })
if GetState(cb) != StateOpen {
t.Error("Should be open after threshold failures")
}
}
func TestCircuitBreakerDifferentErrorTypes(t *testing.T) {
cb := NewCircuitBreaker("test", 2, 1*time.Second)
// Different error types should all count as failures
Call(cb, func() error { return errors.New("network error") })
Call(cb, func() error { return errors.New("timeout") })
if GetState(cb) != StateOpen {
t.Error("All error types should count toward failure threshold")
}
}
func TestCircuitBreakerNilFunction(t *testing.T) {
cb := NewCircuitBreaker("test", 3, 1*time.Second)
// Should handle nil function gracefully (though this is a programming error)
defer func() {
if r := recover(); r != nil {
t.Log("Recovered from panic with nil function, which is expected behavior")
}
}()
Call(cb, nil)
}
func TestCircuitBreakerLongRunningOperation(t *testing.T) {
cb := NewCircuitBreaker("test", 2, 100*time.Millisecond)
// Test that timeout works during operation
err := Call(cb, func() error {
time.Sleep(200 * time.Millisecond)
return nil
})
// Operation should complete despite being longer than circuit breaker timeout
// (timeout is for circuit reset, not operation timeout)
if err != nil {
t.Errorf("Long operation should not fail due to CB timeout: %v", err)
}
}
func TestCircuitBreakerRapidStateChanges(t *testing.T) {
cb := NewCircuitBreaker("test", 1, 1*time.Second)
cb.resetTimeout = 10 * time.Millisecond
for i := 0; i < 10; i++ {
// Open circuit
Call(cb, func() error { return errors.New("error") })
// Wait for half-open
time.Sleep(20 * time.Millisecond)
// Close circuit
Call(cb, func() error { return nil })
}
// After rapid changes, circuit should handle it gracefully
finalState := GetState(cb)
if finalState != StateClosed && finalState != StateHalfOpen && finalState != StateOpen {
t.Errorf("Invalid final state: %v", finalState)
}
}