breakers_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package breaker
  2. import (
  3. "errors"
  4. "fmt"
  5. "testing"
  6. "git.i2edu.net/i2/go-zero/core/stat"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func init() {
  10. stat.SetReporter(nil)
  11. }
  12. func TestBreakersDo(t *testing.T) {
  13. assert.Nil(t, Do("any", func() error {
  14. return nil
  15. }))
  16. errDummy := errors.New("any")
  17. assert.Equal(t, errDummy, Do("any", func() error {
  18. return errDummy
  19. }))
  20. }
  21. func TestBreakersDoWithAcceptable(t *testing.T) {
  22. errDummy := errors.New("anyone")
  23. for i := 0; i < 10000; i++ {
  24. assert.Equal(t, errDummy, GetBreaker("anyone").DoWithAcceptable(func() error {
  25. return errDummy
  26. }, func(err error) bool {
  27. return err == nil || err == errDummy
  28. }))
  29. }
  30. verify(t, func() bool {
  31. return Do("anyone", func() error {
  32. return nil
  33. }) == nil
  34. })
  35. for i := 0; i < 10000; i++ {
  36. err := DoWithAcceptable("another", func() error {
  37. return errDummy
  38. }, func(err error) bool {
  39. return err == nil
  40. })
  41. assert.True(t, err == errDummy || err == ErrServiceUnavailable)
  42. }
  43. verify(t, func() bool {
  44. return ErrServiceUnavailable == Do("another", func() error {
  45. return nil
  46. })
  47. })
  48. }
  49. func TestBreakersNoBreakerFor(t *testing.T) {
  50. NoBreakerFor("any")
  51. errDummy := errors.New("any")
  52. for i := 0; i < 10000; i++ {
  53. assert.Equal(t, errDummy, GetBreaker("any").Do(func() error {
  54. return errDummy
  55. }))
  56. }
  57. assert.Equal(t, nil, Do("any", func() error {
  58. return nil
  59. }))
  60. }
  61. func TestBreakersFallback(t *testing.T) {
  62. errDummy := errors.New("any")
  63. for i := 0; i < 10000; i++ {
  64. err := DoWithFallback("fallback", func() error {
  65. return errDummy
  66. }, func(err error) error {
  67. return nil
  68. })
  69. assert.True(t, err == nil || err == errDummy)
  70. }
  71. verify(t, func() bool {
  72. return ErrServiceUnavailable == Do("fallback", func() error {
  73. return nil
  74. })
  75. })
  76. }
  77. func TestBreakersAcceptableFallback(t *testing.T) {
  78. errDummy := errors.New("any")
  79. for i := 0; i < 10000; i++ {
  80. err := DoWithFallbackAcceptable("acceptablefallback", func() error {
  81. return errDummy
  82. }, func(err error) error {
  83. return nil
  84. }, func(err error) bool {
  85. return err == nil
  86. })
  87. assert.True(t, err == nil || err == errDummy)
  88. }
  89. verify(t, func() bool {
  90. return ErrServiceUnavailable == Do("acceptablefallback", func() error {
  91. return nil
  92. })
  93. })
  94. }
  95. func verify(t *testing.T, fn func() bool) {
  96. var count int
  97. for i := 0; i < 100; i++ {
  98. if fn() {
  99. count++
  100. }
  101. }
  102. assert.True(t, count >= 80, fmt.Sprintf("should be greater than 80, actual %d", count))
  103. }