retrier_test.go 2.2 KB

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