backoff_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package elastic
  2. import (
  3. "math/rand"
  4. "testing"
  5. "time"
  6. )
  7. func TestZeroBackoff(t *testing.T) {
  8. b := ZeroBackoff{}
  9. _, ok := b.Next(0)
  10. if !ok {
  11. t.Fatalf("expected %v, got %v", true, ok)
  12. }
  13. }
  14. func TestStopBackoff(t *testing.T) {
  15. b := StopBackoff{}
  16. _, ok := b.Next(0)
  17. if ok {
  18. t.Fatalf("expected %v, got %v", false, ok)
  19. }
  20. }
  21. func TestConstantBackoff(t *testing.T) {
  22. b := NewConstantBackoff(time.Second)
  23. d, ok := b.Next(0)
  24. if !ok {
  25. t.Fatalf("expected %v, got %v", true, ok)
  26. }
  27. if d != time.Second {
  28. t.Fatalf("expected %v, got %v", time.Second, d)
  29. }
  30. }
  31. func TestSimpleBackoff(t *testing.T) {
  32. var tests = []struct {
  33. Duration time.Duration
  34. Continue bool
  35. }{
  36. // #0
  37. {
  38. Duration: 1 * time.Millisecond,
  39. Continue: true,
  40. },
  41. // #1
  42. {
  43. Duration: 2 * time.Millisecond,
  44. Continue: true,
  45. },
  46. // #2
  47. {
  48. Duration: 7 * time.Millisecond,
  49. Continue: true,
  50. },
  51. // #3
  52. {
  53. Duration: 0,
  54. Continue: false,
  55. },
  56. // #4
  57. {
  58. Duration: 0,
  59. Continue: false,
  60. },
  61. }
  62. b := NewSimpleBackoff(1, 2, 7)
  63. for i, tt := range tests {
  64. d, ok := b.Next(i)
  65. if got, want := ok, tt.Continue; got != want {
  66. t.Fatalf("#%d: expected %v, got %v", i, want, got)
  67. }
  68. if got, want := d, tt.Duration; got != want {
  69. t.Fatalf("#%d: expected %v, got %v", i, want, got)
  70. }
  71. }
  72. }
  73. func TestExponentialBackoff(t *testing.T) {
  74. rand.Seed(time.Now().UnixNano())
  75. min := time.Duration(8) * time.Millisecond
  76. max := time.Duration(256) * time.Millisecond
  77. b := NewExponentialBackoff(min, max)
  78. between := func(value time.Duration, a, b int) bool {
  79. x := int(value / time.Millisecond)
  80. return a <= x && x <= b
  81. }
  82. got, ok := b.Next(0)
  83. if !ok {
  84. t.Fatalf("expected %v, got %v", true, ok)
  85. }
  86. if !between(got, 8, 256) {
  87. t.Errorf("expected [%v..%v], got %v", 8, 256, got)
  88. }
  89. got, ok = b.Next(1)
  90. if !ok {
  91. t.Fatalf("expected %v, got %v", true, ok)
  92. }
  93. if !between(got, 8, 256) {
  94. t.Errorf("expected [%v..%v], got %v", 8, 256, got)
  95. }
  96. got, ok = b.Next(2)
  97. if !ok {
  98. t.Fatalf("expected %v, got %v", true, ok)
  99. }
  100. if !between(got, 8, 256) {
  101. t.Errorf("expected [%v..%v], got %v", 8, 256, got)
  102. }
  103. got, ok = b.Next(3)
  104. if !ok {
  105. t.Fatalf("expected %v, got %v", true, ok)
  106. }
  107. if !between(got, 8, 256) {
  108. t.Errorf("expected [%v..%v], got %v", 8, 256, got)
  109. }
  110. got, ok = b.Next(4)
  111. if !ok {
  112. t.Fatalf("expected %v, got %v", true, ok)
  113. }
  114. if !between(got, 8, 256) {
  115. t.Errorf("expected [%v..%v], got %v", 8, 256, got)
  116. }
  117. if _, ok := b.Next(5); ok {
  118. t.Fatalf("expected %v, got %v", false, ok)
  119. }
  120. if _, ok = b.Next(6); ok {
  121. t.Fatalf("expected %v, got %v", false, ok)
  122. }
  123. }