Files
graphql-monitoring-proxy/errors_test.go
lukaszraczylo 10e51daab3 feat(circuit): progressive backoff and configurable trip conditions
The circuit breaker ignored the trip-on-timeout, trip-on-4xx, and
trip-on-5xx flags and never cleared its counts, so the failure ratio
became lifetime-cumulative. The backoff-multiplier and max-backoff
settings did nothing. Circuit-open surfaced as HTTP 500.

Failure classification now honours the trip flags, the breaker uses a
rolling count window, and repeated trips extend the open state up to the
configured maximum. A helper maps circuit-open to HTTP 503. The request
body is copied once per request instead of once per retry. Defaults
reproduce the prior behaviour: the breaker is off by default and a
backoff multiplier of one keeps a constant open window.
2026-08-14 11:10:37 +01:00

323 lines
9.1 KiB
Go

package main
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/sony/gobreaker"
"github.com/stretchr/testify/assert"
)
func TestNewProxyError(t *testing.T) {
tests := []struct {
name string
code string
message string
statusCode int
retryable bool
expectStatus int
}{
{
name: "connection refused error",
code: ErrCodeConnectionRefused,
message: "backend unavailable",
statusCode: http.StatusServiceUnavailable,
retryable: true,
expectStatus: http.StatusServiceUnavailable,
},
{
name: "timeout error",
code: ErrCodeTimeout,
message: "request timeout",
statusCode: http.StatusGatewayTimeout,
retryable: true,
expectStatus: http.StatusGatewayTimeout,
},
{
name: "circuit breaker open",
code: ErrCodeCircuitOpen,
message: "circuit breaker open",
statusCode: http.StatusServiceUnavailable,
retryable: false,
expectStatus: http.StatusServiceUnavailable,
},
{
name: "rate limit exceeded",
code: ErrCodeRateLimited,
message: "too many requests",
statusCode: http.StatusTooManyRequests,
retryable: false,
expectStatus: http.StatusTooManyRequests,
},
{
name: "service unavailable",
code: ErrCodeServiceUnavailable,
message: "no retry tokens available",
statusCode: http.StatusServiceUnavailable,
retryable: false,
expectStatus: http.StatusServiceUnavailable,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := NewProxyError(tt.code, tt.message, tt.statusCode, tt.retryable)
assert.NotNil(t, err)
assert.Equal(t, tt.code, err.Code)
assert.Equal(t, tt.message, err.Message)
assert.Equal(t, tt.retryable, err.Retryable)
assert.Equal(t, tt.expectStatus, err.StatusCode)
assert.NotEmpty(t, err.Timestamp)
assert.NotNil(t, err.Metadata)
})
}
}
func TestProxyError_Error(t *testing.T) {
tests := []struct {
name string
err *ProxyError
expected string
}{
{
name: "error with details",
err: NewProxyError(ErrCodeConnectionRefused, "backend unavailable", http.StatusServiceUnavailable, true).
WithDetails("connection refused"),
expected: "CONNECTION_REFUSED: backend unavailable (connection refused)",
},
{
name: "error without details",
err: NewProxyError(ErrCodeCircuitOpen, "circuit breaker open", http.StatusServiceUnavailable, false),
expected: "CIRCUIT_OPEN: circuit breaker open",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.err.Error())
})
}
}
func TestProxyError_Unwrap(t *testing.T) {
cause := errors.New("original error")
err := NewProxyError(ErrCodeTimeout, "timeout occurred", http.StatusGatewayTimeout, true).WithCause(cause)
unwrapped := errors.Unwrap(err)
assert.Equal(t, cause, unwrapped)
}
func TestProxyError_WithMethods(t *testing.T) {
t.Run("with details", func(t *testing.T) {
err := NewProxyError(ErrCodeTimeout, "timeout", http.StatusGatewayTimeout, true).
WithDetails("operation timed out")
assert.Equal(t, "operation timed out", err.Details)
})
t.Run("with cause", func(t *testing.T) {
cause := errors.New("original error")
err := NewProxyError(ErrCodeTimeout, "timeout", http.StatusGatewayTimeout, true).
WithCause(cause)
assert.Equal(t, cause, err.Cause)
})
t.Run("with trace ID", func(t *testing.T) {
err := NewProxyError(ErrCodeTimeout, "timeout", http.StatusGatewayTimeout, true).
WithTraceID("trace-123")
assert.Equal(t, "trace-123", err.TraceID)
})
t.Run("with metadata", func(t *testing.T) {
err := NewProxyError(ErrCodeTimeout, "timeout", http.StatusGatewayTimeout, true).
WithMetadata("attempt", 3).
WithMetadata("endpoint", "/graphql")
assert.Equal(t, 3, err.Metadata["attempt"])
assert.Equal(t, "/graphql", err.Metadata["endpoint"])
})
}
func TestProxyError_MarshalJSON(t *testing.T) {
cause := errors.New("connection refused")
err := NewProxyError(ErrCodeConnectionRefused, "backend unavailable", http.StatusServiceUnavailable, true).
WithDetails("network error").
WithCause(cause).
WithTraceID("trace-456")
data, jsonErr := err.MarshalJSON()
assert.NoError(t, jsonErr)
assert.NotEmpty(t, data)
assert.Contains(t, string(data), "CONNECTION_REFUSED")
assert.Contains(t, string(data), "backend unavailable")
assert.Contains(t, string(data), "connection refused")
}
func TestErrorCodes(t *testing.T) {
// Verify all error codes are defined
codes := []string{
ErrCodeConnectionRefused,
ErrCodeConnectionReset,
ErrCodeTimeout,
ErrCodeCircuitOpen,
ErrCodeRateLimited,
ErrCodeInvalidRequest,
ErrCodeBackendError,
ErrCodeInternalError,
ErrCodeUnauthorized,
ErrCodeForbidden,
ErrCodeNotFound,
ErrCodeServiceUnavailable,
ErrCodeBadGateway,
ErrCodeInvalidResponse,
ErrCodeQueryTooComplex,
ErrCodeCacheFailed,
ErrCodeContextCanceled,
}
for _, code := range codes {
assert.NotEmpty(t, code, "Error code should not be empty")
}
// Verify codes are unique
codeMap := make(map[string]bool)
for _, code := range codes {
assert.False(t, codeMap[code], "Error code %s should be unique", code)
codeMap[code] = true
}
}
func TestProxyError_ChainableMethods(t *testing.T) {
// Test that methods can be chained
err := NewProxyError(ErrCodeTimeout, "timeout", http.StatusGatewayTimeout, true).
WithDetails("operation timeout").
WithCause(errors.New("deadline exceeded")).
WithTraceID("trace-789").
WithMetadata("attempt", 1).
WithMetadata("duration_ms", 5000)
assert.Equal(t, "operation timeout", err.Details)
assert.NotNil(t, err.Cause)
assert.Equal(t, "trace-789", err.TraceID)
assert.Equal(t, 1, err.Metadata["attempt"])
assert.Equal(t, 5000, err.Metadata["duration_ms"])
}
func TestProxyError_Retryable(t *testing.T) {
tests := []struct {
name string
code string
retryable bool
}{
{
name: "timeout is retryable",
code: ErrCodeTimeout,
retryable: true,
},
{
name: "connection refused is retryable",
code: ErrCodeConnectionRefused,
retryable: true,
},
{
name: "rate limited is not retryable",
code: ErrCodeRateLimited,
retryable: false,
},
{
name: "circuit open is not retryable",
code: ErrCodeCircuitOpen,
retryable: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := NewProxyError(tt.code, "test error", http.StatusInternalServerError, tt.retryable)
assert.Equal(t, tt.retryable, err.Retryable)
})
}
}
// Test_IsRetryable_GetStatusCode_Wrapped verifies that retryability and status
// are correctly recovered from a ProxyError that has been wrapped (e.g. via
// %w), using errors.As rather than a direct type assertion.
func Test_IsRetryable_GetStatusCode_Wrapped(t *testing.T) {
base := NewProxyError(ErrCodeBackendError, "boom", 503, true)
wrapped := fmt.Errorf("request failed: %w", base)
assert.True(t, IsRetryable(wrapped), "retryable flag must survive wrapping")
assert.Equal(t, 503, GetStatusCode(wrapped), "status code must survive wrapping")
nonRetryable := NewProxyError(ErrCodeInvalidRequest, "bad", 400, false)
wrapped2 := fmt.Errorf("wrapped: %w", nonRetryable)
assert.False(t, IsRetryable(wrapped2))
assert.Equal(t, 400, GetStatusCode(wrapped2))
assert.False(t, IsRetryable(nil))
assert.Equal(t, 200, GetStatusCode(nil))
}
// Test_StatusCodeForError covers the C4 fix: circuit-open errors must map to
// 503 (previously fell through to a generic 500 because nothing mapped
// them), a wrapped *ProxyError's own status code must survive, and any
// other error defaults to 500. nil maps to 200.
func Test_StatusCodeForError(t *testing.T) {
tests := []struct {
name string
err error
wantStatus int
}{
{
name: "nil error maps to 200",
err: nil,
wantStatus: 200,
},
{
name: "circuit breaker open (proxy sentinel) maps to 503",
err: ErrCircuitOpen,
wantStatus: 503,
},
{
name: "circuit breaker open (gobreaker sentinel) maps to 503",
err: gobreaker.ErrOpenState,
wantStatus: 503,
},
{
name: "wrapped circuit breaker open still maps to 503",
err: fmt.Errorf("request failed: %w", ErrCircuitOpen),
wantStatus: 503,
},
{
name: "wrapped gobreaker open state still maps to 503",
err: fmt.Errorf("request failed: %w", gobreaker.ErrOpenState),
wantStatus: 503,
},
{
name: "generic error defaults to 500",
err: errors.New("boom"),
wantStatus: 500,
},
{
name: "ProxyError with explicit status code is honoured",
err: NewProxyError(ErrCodeRateLimited, "too many requests", http.StatusTooManyRequests, false),
wantStatus: http.StatusTooManyRequests,
},
{
name: "wrapped ProxyError status code survives wrapping",
err: fmt.Errorf("wrapped: %w", NewProxyError(ErrCodeBackendError, "boom", http.StatusBadGateway, true)),
wantStatus: http.StatusBadGateway,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantStatus, StatusCodeForError(tt.err))
})
}
}