You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
750 B
Go
45 lines
750 B
Go
package breaker
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"zero/core/stat"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func init() {
|
|
stat.SetReporter(nil)
|
|
}
|
|
|
|
func TestCircuitBreaker_Allow(t *testing.T) {
|
|
b := NewBreaker()
|
|
assert.True(t, len(b.Name()) > 0)
|
|
_, err := b.Allow()
|
|
assert.Nil(t, err)
|
|
}
|
|
|
|
func TestLogReason(t *testing.T) {
|
|
b := NewBreaker()
|
|
assert.True(t, len(b.Name()) > 0)
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
_ = b.Do(func() error {
|
|
return errors.New(strconv.Itoa(i))
|
|
})
|
|
}
|
|
errs := b.(*circuitBreaker).throttle.(loggedThrottle).errWin
|
|
assert.Equal(t, numHistoryReasons, errs.count)
|
|
}
|
|
|
|
func BenchmarkGoogleBreaker(b *testing.B) {
|
|
br := NewBreaker()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = br.Do(func() error {
|
|
return nil
|
|
})
|
|
}
|
|
}
|