fixed sonarqube issues
This commit is contained in:
@@ -45,7 +45,7 @@ func TestNewCircuitBreaker(t *testing.T) {
|
||||
t.Errorf("timeout = %v, want %v", cb.timeout, tt.timeout)
|
||||
}
|
||||
if cb.state != tt.wantState {
|
||||
t.Errorf("state = %v, want %v", 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)
|
||||
@@ -57,7 +57,7 @@ func TestNewCircuitBreaker(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_Call_Success(t *testing.T) {
|
||||
func TestCircuitBreakerCallSuccess(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 3, 1*time.Second)
|
||||
|
||||
successFn := func() error {
|
||||
@@ -70,15 +70,15 @@ func TestCircuitBreaker_Call_Success(t *testing.T) {
|
||||
}
|
||||
|
||||
if GetState(cb) != StateClosed {
|
||||
t.Errorf("state = %v, want %v", GetState(cb), StateClosed)
|
||||
t.Errorf(StateMismatchMessage, GetState(cb), StateClosed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_Call_FailuresOpenCircuit(t *testing.T) {
|
||||
func TestCircuitBreakerCallFailuresOpenCircuit(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 3, 1*time.Second)
|
||||
|
||||
failFn := func() error {
|
||||
return errors.New("service error")
|
||||
return errors.New(ServiceError)
|
||||
}
|
||||
|
||||
// First 2 failures - circuit should stay closed
|
||||
@@ -98,7 +98,7 @@ func TestCircuitBreaker_Call_FailuresOpenCircuit(t *testing.T) {
|
||||
t.Error("Call() expected error, got nil")
|
||||
}
|
||||
if GetState(cb) != StateOpen {
|
||||
t.Errorf("state = %v, want %v", GetState(cb), StateOpen)
|
||||
t.Errorf(StateMismatchMessage, GetState(cb), StateOpen)
|
||||
}
|
||||
|
||||
// Next call should immediately return circuit breaker error
|
||||
@@ -108,12 +108,12 @@ func TestCircuitBreaker_Call_FailuresOpenCircuit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_Call_OpenToHalfOpen(t *testing.T) {
|
||||
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("service error")
|
||||
return errors.New(ServiceError)
|
||||
}
|
||||
|
||||
// Open the circuit
|
||||
@@ -121,7 +121,7 @@ func TestCircuitBreaker_Call_OpenToHalfOpen(t *testing.T) {
|
||||
Call(cb, failFn)
|
||||
|
||||
if GetState(cb) != StateOpen {
|
||||
t.Fatalf("state = %v, want %v", GetState(cb), StateOpen)
|
||||
t.Fatalf(StateMismatchMessage, GetState(cb), StateOpen)
|
||||
}
|
||||
|
||||
// Wait for reset timeout
|
||||
@@ -139,16 +139,16 @@ func TestCircuitBreaker_Call_OpenToHalfOpen(t *testing.T) {
|
||||
|
||||
// Should now be closed
|
||||
if GetState(cb) != StateClosed {
|
||||
t.Errorf("state = %v, want %v", GetState(cb), StateClosed)
|
||||
t.Errorf(StateMismatchMessage, GetState(cb), StateClosed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_Call_HalfOpenFailReturnsToOpen(t *testing.T) {
|
||||
func TestCircuitBreakerCallHalfOpenFailReturnsToOpen(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 2, 1*time.Second)
|
||||
cb.resetTimeout = 100 * time.Millisecond
|
||||
|
||||
failFn := func() error {
|
||||
return errors.New("service error")
|
||||
return errors.New(ServiceError)
|
||||
}
|
||||
|
||||
// Open the circuit
|
||||
@@ -156,7 +156,7 @@ func TestCircuitBreaker_Call_HalfOpenFailReturnsToOpen(t *testing.T) {
|
||||
Call(cb, failFn)
|
||||
|
||||
if GetState(cb) != StateOpen {
|
||||
t.Fatalf("state = %v, want %v", GetState(cb), StateOpen)
|
||||
t.Fatalf(StateMismatchMessage, GetState(cb), StateOpen)
|
||||
}
|
||||
|
||||
// Wait for reset timeout to transition to HalfOpen
|
||||
@@ -169,15 +169,15 @@ func TestCircuitBreaker_Call_HalfOpenFailReturnsToOpen(t *testing.T) {
|
||||
}
|
||||
|
||||
if GetState(cb) != StateOpen {
|
||||
t.Errorf("state = %v, want %v", GetState(cb), StateOpen)
|
||||
t.Errorf(StateMismatchMessage, GetState(cb), StateOpen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_Call_GradualFailureReduction(t *testing.T) {
|
||||
func TestCircuitBreakerCallGradualFailureReduction(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 5, 1*time.Second)
|
||||
|
||||
failFn := func() error {
|
||||
return errors.New("service error")
|
||||
return errors.New(ServiceError)
|
||||
}
|
||||
successFn := func() error {
|
||||
return nil
|
||||
@@ -208,7 +208,7 @@ func TestCircuitBreaker_Call_GradualFailureReduction(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_GetState(t *testing.T) {
|
||||
func TestCircuitBreakerGetState(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setupFunc func(*CircuitBreaker)
|
||||
@@ -250,7 +250,7 @@ func TestCircuitBreaker_GetState(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_Reset(t *testing.T) {
|
||||
func TestCircuitBreakerReset(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 2, 1*time.Second)
|
||||
|
||||
// Open the circuit
|
||||
@@ -261,7 +261,7 @@ func TestCircuitBreaker_Reset(t *testing.T) {
|
||||
Call(cb, failFn)
|
||||
|
||||
if GetState(cb) != StateOpen {
|
||||
t.Fatalf("state = %v, want %v", GetState(cb), StateOpen)
|
||||
t.Fatalf(StateMismatchMessage, GetState(cb), StateOpen)
|
||||
}
|
||||
|
||||
// Reset the circuit breaker
|
||||
@@ -280,7 +280,7 @@ func TestCircuitBreaker_Reset(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreakerError_Error(t *testing.T) {
|
||||
func TestCircuitBreakerErrorError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err *CircuitBreakerError
|
||||
@@ -350,7 +350,7 @@ func TestIsCircuitBreakerError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_Concurrency(t *testing.T) {
|
||||
func TestCircuitBreakerConcurrency(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 10, 1*time.Second)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
@@ -397,7 +397,7 @@ func TestCircuitBreaker_Concurrency(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_OpenCircuitRejectsImmediately(t *testing.T) {
|
||||
func TestCircuitBreakerOpenCircuitRejectsImmediately(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 1, 1*time.Second)
|
||||
|
||||
// Open the circuit
|
||||
@@ -407,7 +407,7 @@ func TestCircuitBreaker_OpenCircuitRejectsImmediately(t *testing.T) {
|
||||
Call(cb, failFn)
|
||||
|
||||
if GetState(cb) != StateOpen {
|
||||
t.Fatalf("state = %v, want %v", GetState(cb), StateOpen)
|
||||
t.Fatalf(StateMismatchMessage, GetState(cb), StateOpen)
|
||||
}
|
||||
|
||||
// Try calling with a function that should not execute
|
||||
@@ -428,7 +428,7 @@ func TestCircuitBreaker_OpenCircuitRejectsImmediately(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCircuitBreaker_Call_Success(b *testing.B) {
|
||||
func BenchmarkCircuitBreakerCallSuccess(b *testing.B) {
|
||||
cb := NewCircuitBreaker("test", 5, 1*time.Second)
|
||||
fn := func() error {
|
||||
return nil
|
||||
@@ -440,7 +440,7 @@ func BenchmarkCircuitBreaker_Call_Success(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCircuitBreaker_Call_Open(b *testing.B) {
|
||||
func BenchmarkCircuitBreakerCallOpen(b *testing.B) {
|
||||
cb := NewCircuitBreaker("test", 1, 1*time.Second)
|
||||
|
||||
// Open the circuit
|
||||
@@ -458,7 +458,7 @@ func BenchmarkCircuitBreaker_Call_Open(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_StateTransitions(t *testing.T) {
|
||||
func TestCircuitBreakerStateTransitions(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 2, 1*time.Second)
|
||||
cb.resetTimeout = 100 * time.Millisecond
|
||||
|
||||
@@ -497,7 +497,7 @@ func TestCircuitBreaker_StateTransitions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_ZeroMaxFailures(t *testing.T) {
|
||||
func TestCircuitBreakerZeroMaxFailures(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 0, 1*time.Second)
|
||||
|
||||
// Even one failure should open circuit when maxFailures is 0
|
||||
@@ -512,7 +512,7 @@ func TestCircuitBreaker_ZeroMaxFailures(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_NegativeMaxFailures(t *testing.T) {
|
||||
func TestCircuitBreakerNegativeMaxFailures(t *testing.T) {
|
||||
// Negative maxFailures should be treated as invalid, but won't panic
|
||||
cb := NewCircuitBreaker("test", -1, 1*time.Second)
|
||||
|
||||
@@ -526,7 +526,7 @@ func TestCircuitBreaker_NegativeMaxFailures(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_VeryShortTimeout(t *testing.T) {
|
||||
func TestCircuitBreakerVeryShortTimeout(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 1, 1*time.Nanosecond)
|
||||
cb.resetTimeout = 1 * time.Nanosecond
|
||||
|
||||
@@ -542,7 +542,7 @@ func TestCircuitBreaker_VeryShortTimeout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_MultipleSuccessesAfterFailure(t *testing.T) {
|
||||
func TestCircuitBreakerMultipleSuccessesAfterFailure(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 3, 1*time.Second)
|
||||
|
||||
// Add one failure
|
||||
@@ -570,7 +570,7 @@ func TestCircuitBreaker_MultipleSuccessesAfterFailure(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_HighConcurrency(t *testing.T) {
|
||||
func TestCircuitBreakerHighConcurrency(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 10, 1*time.Second)
|
||||
|
||||
concurrency := 100
|
||||
@@ -608,7 +608,7 @@ func TestCircuitBreaker_HighConcurrency(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_HalfOpenSingleRequest(t *testing.T) {
|
||||
func TestCircuitBreakerHalfOpenSingleRequest(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 1, 1*time.Second)
|
||||
cb.resetTimeout = 50 * time.Millisecond
|
||||
|
||||
@@ -637,7 +637,7 @@ func TestCircuitBreaker_HalfOpenSingleRequest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_SuccessResetsFailureCount(t *testing.T) {
|
||||
func TestCircuitBreakerSuccessResetsFailureCount(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 3, 1*time.Second)
|
||||
|
||||
// 2 failures
|
||||
@@ -674,7 +674,7 @@ func TestCircuitBreaker_SuccessResetsFailureCount(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_DifferentErrorTypes(t *testing.T) {
|
||||
func TestCircuitBreakerDifferentErrorTypes(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 2, 1*time.Second)
|
||||
|
||||
// Different error types should all count as failures
|
||||
@@ -686,7 +686,7 @@ func TestCircuitBreaker_DifferentErrorTypes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_NilFunction(t *testing.T) {
|
||||
func TestCircuitBreakerNilFunction(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 3, 1*time.Second)
|
||||
|
||||
// Should handle nil function gracefully (though this is a programming error)
|
||||
@@ -699,7 +699,7 @@ func TestCircuitBreaker_NilFunction(t *testing.T) {
|
||||
Call(cb, nil)
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_LongRunningOperation(t *testing.T) {
|
||||
func TestCircuitBreakerLongRunningOperation(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 2, 100*time.Millisecond)
|
||||
|
||||
// Test that timeout works during operation
|
||||
@@ -715,7 +715,7 @@ func TestCircuitBreaker_LongRunningOperation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitBreaker_RapidStateChanges(t *testing.T) {
|
||||
func TestCircuitBreakerRapidStateChanges(t *testing.T) {
|
||||
cb := NewCircuitBreaker("test", 1, 1*time.Second)
|
||||
cb.resetTimeout = 10 * time.Millisecond
|
||||
|
||||
|
||||
@@ -8,4 +8,6 @@ const (
|
||||
ErrorEncodingResponse = "Error encoding response"
|
||||
ErrorFailedtoLogLoginEvent = "Failed to log login event"
|
||||
WarningLabel = "WARNING:"
|
||||
StateMismatchMessage = "state = %v, want %v"
|
||||
ServiceError = "service error"
|
||||
)
|
||||
|
||||
@@ -68,7 +68,7 @@ func TestLogInfo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogInfo_NoEnvironment(t *testing.T) {
|
||||
func TestLogInfoNoEnvironment(t *testing.T) {
|
||||
// Setup
|
||||
os.Unsetenv("GO_ENV")
|
||||
|
||||
@@ -188,7 +188,7 @@ func TestLogError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogError_WithNilError(t *testing.T) {
|
||||
func TestLogErrorWithNilError(t *testing.T) {
|
||||
// Setup
|
||||
os.Setenv("GO_ENV", "development")
|
||||
defer os.Unsetenv("GO_ENV")
|
||||
@@ -223,7 +223,7 @@ func TestLogFatal(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestLogging_EnvironmentCheck(t *testing.T) {
|
||||
func TestLoggingEnvironmentCheck(t *testing.T) {
|
||||
// Test that all logging functions check for GO_ENV
|
||||
originalEnv := os.Getenv("GO_ENV")
|
||||
defer func() {
|
||||
|
||||
Reference in New Issue
Block a user