breaker_test.go 750 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package breaker
  2. import (
  3. "errors"
  4. "strconv"
  5. "testing"
  6. "zero/core/stat"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func init() {
  10. stat.SetReporter(nil)
  11. }
  12. func TestCircuitBreaker_Allow(t *testing.T) {
  13. b := NewBreaker()
  14. assert.True(t, len(b.Name()) > 0)
  15. _, err := b.Allow()
  16. assert.Nil(t, err)
  17. }
  18. func TestLogReason(t *testing.T) {
  19. b := NewBreaker()
  20. assert.True(t, len(b.Name()) > 0)
  21. for i := 0; i < 1000; i++ {
  22. _ = b.Do(func() error {
  23. return errors.New(strconv.Itoa(i))
  24. })
  25. }
  26. errs := b.(*circuitBreaker).throttle.(loggedThrottle).errWin
  27. assert.Equal(t, numHistoryReasons, errs.count)
  28. }
  29. func BenchmarkGoogleBreaker(b *testing.B) {
  30. br := NewBreaker()
  31. for i := 0; i < b.N; i++ {
  32. _ = br.Do(func() error {
  33. return nil
  34. })
  35. }
  36. }