package tree import ( "errors" "testing" "time" "github.com/stretchr/testify/require" ) func TestCircuitBreaker(t *testing.T) { remoteErr := errors.New("service is being synchronized") breakDuration := 1 * time.Second threshold := 10 cb := newCircuitBreaker(breakDuration, threshold) // Hit threshold for i := 0; i < threshold; i++ { err := cb.Do(1, func() error { return remoteErr }) require.ErrorIs(t, err, remoteErr) } // Different client should not be affected by threshold require.NoError(t, cb.Do(2, func() error { return nil })) // Immediate request should return circuit breaker error require.ErrorIs(t, cb.Do(1, func() error { return nil }), ErrCBClosed) // Request after breakDuration should be ok time.Sleep(breakDuration) require.NoError(t, cb.Do(1, func() error { return nil })) // Try hitting threshold one more time after break duration for i := 0; i < threshold; i++ { err := cb.Do(1, func() error { return remoteErr }) require.ErrorIs(t, err, remoteErr) } // Immediate request should return circuit breaker error require.ErrorIs(t, cb.Do(1, func() error { return nil }), ErrCBClosed) }