retry_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package fx
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestRetry(t *testing.T) {
  10. assert.NotNil(t, DoWithRetry(func() error {
  11. return errors.New("any")
  12. }))
  13. times1 := 0
  14. assert.Nil(t, DoWithRetry(func() error {
  15. times1++
  16. if times1 == defaultRetryTimes {
  17. return nil
  18. }
  19. return errors.New("any")
  20. }))
  21. times2 := 0
  22. assert.NotNil(t, DoWithRetry(func() error {
  23. times2++
  24. if times2 == defaultRetryTimes+1 {
  25. return nil
  26. }
  27. return errors.New("any")
  28. }))
  29. total := 2 * defaultRetryTimes
  30. times3 := 0
  31. assert.Nil(t, DoWithRetry(func() error {
  32. times3++
  33. if times3 == total {
  34. return nil
  35. }
  36. return errors.New("any")
  37. }, WithRetry(total)))
  38. }
  39. func TestRetryWithTimeout(t *testing.T) {
  40. assert.Nil(t, DoWithRetry(func() error {
  41. return nil
  42. }, WithTimeout(time.Millisecond*500)))
  43. times1 := 0
  44. assert.Nil(t, DoWithRetry(func() error {
  45. times1++
  46. if times1 == 1 {
  47. return errors.New("any ")
  48. }
  49. time.Sleep(time.Millisecond * 150)
  50. return nil
  51. }, WithTimeout(time.Millisecond*250)))
  52. total := defaultRetryTimes
  53. times2 := 0
  54. assert.Nil(t, DoWithRetry(func() error {
  55. times2++
  56. if times2 == total {
  57. return nil
  58. }
  59. time.Sleep(time.Millisecond * 50)
  60. return errors.New("any")
  61. }, WithTimeout(time.Millisecond*50*(time.Duration(total)+2))))
  62. assert.NotNil(t, DoWithRetry(func() error {
  63. return errors.New("any")
  64. }, WithTimeout(time.Millisecond*250)))
  65. }
  66. func TestRetryWithInterval(t *testing.T) {
  67. times1 := 0
  68. assert.NotNil(t, DoWithRetry(func() error {
  69. times1++
  70. if times1 == 1 {
  71. return errors.New("any")
  72. }
  73. time.Sleep(time.Millisecond * 150)
  74. return nil
  75. }, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
  76. times2 := 0
  77. assert.NotNil(t, DoWithRetry(func() error {
  78. times2++
  79. if times2 == 2 {
  80. return nil
  81. }
  82. time.Sleep(time.Millisecond * 150)
  83. return errors.New("any ")
  84. }, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
  85. }
  86. func TestRetryCtx(t *testing.T) {
  87. assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error {
  88. if retryCount == 0 {
  89. return errors.New("any")
  90. }
  91. time.Sleep(time.Millisecond * 150)
  92. return nil
  93. }, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
  94. assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error {
  95. if retryCount == 1 {
  96. return nil
  97. }
  98. time.Sleep(time.Millisecond * 150)
  99. return errors.New("any ")
  100. }, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
  101. }