frostfs-sdk-go/pool/tree/circuitbreaker_test.go
Alex Vanin c5991fc66d
All checks were successful
DCO / DCO (pull_request) Successful in 28s
Code generation / Generate proto (pull_request) Successful in 32s
Tests and linters / Tests (pull_request) Successful in 44s
Tests and linters / Lint (pull_request) Successful in 1m27s
[#339] pool/tree: Fix linter issues
Signed-off-by: Alex Vanin <a.vanin@yadro.com>
2025-03-04 12:22:38 +03:00

41 lines
1.1 KiB
Go

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)
}