retry_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. t.Run("with timeout", func(t *testing.T) {
  88. assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error {
  89. if retryCount == 0 {
  90. return errors.New("any")
  91. }
  92. time.Sleep(time.Millisecond * 150)
  93. return nil
  94. }, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
  95. assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error {
  96. if retryCount == 1 {
  97. return nil
  98. }
  99. time.Sleep(time.Millisecond * 150)
  100. return errors.New("any ")
  101. }, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
  102. })
  103. t.Run("with deadline exceeded", func(t *testing.T) {
  104. ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*250))
  105. defer cancel()
  106. var times int
  107. assert.Error(t, DoWithRetryCtx(ctx, func(ctx context.Context, retryCount int) error {
  108. times++
  109. time.Sleep(time.Millisecond * 150)
  110. return errors.New("any")
  111. }, WithInterval(time.Millisecond*150)))
  112. assert.Equal(t, 1, times)
  113. })
  114. t.Run("with deadline not exceeded", func(t *testing.T) {
  115. ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*250))
  116. defer cancel()
  117. var times int
  118. assert.NoError(t, DoWithRetryCtx(ctx, func(ctx context.Context, retryCount int) error {
  119. times++
  120. if times == defaultRetryTimes {
  121. return nil
  122. }
  123. time.Sleep(time.Millisecond * 50)
  124. return errors.New("any")
  125. }))
  126. assert.Equal(t, defaultRetryTimes, times)
  127. })
  128. }