retrier_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package retrier
  2. import (
  3. "errors"
  4. "testing"
  5. "time"
  6. )
  7. var i int
  8. func genWork(returns []error) func() error {
  9. i = 0
  10. return func() error {
  11. i++
  12. if i > len(returns) {
  13. return nil
  14. }
  15. return returns[i-1]
  16. }
  17. }
  18. func TestRetrier(t *testing.T) {
  19. r := New([]time.Duration{0, 10 * time.Millisecond}, WhitelistClassifier{errFoo})
  20. err := r.Run(genWork([]error{errFoo, errFoo}))
  21. if err != nil {
  22. t.Error(err)
  23. }
  24. if i != 3 {
  25. t.Error("run wrong number of times")
  26. }
  27. err = r.Run(genWork([]error{errFoo, errBar}))
  28. if err != errBar {
  29. t.Error(err)
  30. }
  31. if i != 2 {
  32. t.Error("run wrong number of times")
  33. }
  34. err = r.Run(genWork([]error{errBar, errBaz}))
  35. if err != errBar {
  36. t.Error(err)
  37. }
  38. if i != 1 {
  39. t.Error("run wrong number of times")
  40. }
  41. }
  42. func TestRetrierNone(t *testing.T) {
  43. r := New(nil, nil)
  44. i = 0
  45. err := r.Run(func() error {
  46. i++
  47. return errFoo
  48. })
  49. if err != errFoo {
  50. t.Error(err)
  51. }
  52. if i != 1 {
  53. t.Error("run wrong number of times")
  54. }
  55. i = 0
  56. err = r.Run(func() error {
  57. i++
  58. return nil
  59. })
  60. if err != nil {
  61. t.Error(err)
  62. }
  63. if i != 1 {
  64. t.Error("run wrong number of times")
  65. }
  66. }
  67. func TestRetrierJitter(t *testing.T) {
  68. r := New([]time.Duration{0, 10 * time.Millisecond, 4 * time.Hour}, nil)
  69. if r.calcSleep(0) != 0 {
  70. t.Error("Incorrect sleep calculated")
  71. }
  72. if r.calcSleep(1) != 10*time.Millisecond {
  73. t.Error("Incorrect sleep calculated")
  74. }
  75. if r.calcSleep(2) != 4*time.Hour {
  76. t.Error("Incorrect sleep calculated")
  77. }
  78. r.SetJitter(0.25)
  79. for i := 0; i < 20; i++ {
  80. if r.calcSleep(0) != 0 {
  81. t.Error("Incorrect sleep calculated")
  82. }
  83. slp := r.calcSleep(1)
  84. if slp < 7500*time.Microsecond || slp > 12500*time.Microsecond {
  85. t.Error("Incorrect sleep calculated")
  86. }
  87. slp = r.calcSleep(2)
  88. if slp < 3*time.Hour || slp > 5*time.Hour {
  89. t.Error("Incorrect sleep calculated")
  90. }
  91. }
  92. r.SetJitter(-1)
  93. if r.jitter != 0.25 {
  94. t.Error("Invalid jitter value accepted")
  95. }
  96. r.SetJitter(2)
  97. if r.jitter != 0.25 {
  98. t.Error("Invalid jitter value accepted")
  99. }
  100. }
  101. func TestRetrierThreadSafety(t *testing.T) {
  102. r := New([]time.Duration{0}, nil)
  103. for i := 0; i < 2; i++ {
  104. go func() {
  105. r.Run(func() error {
  106. return errors.New("error")
  107. })
  108. }()
  109. }
  110. }
  111. func ExampleRetrier() {
  112. r := New(ConstantBackoff(3, 100*time.Millisecond), nil)
  113. err := r.Run(func() error {
  114. // do some work
  115. return nil
  116. })
  117. if err != nil {
  118. // handle the case where the work failed three times
  119. }
  120. }