You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

backoff_test.go 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package backoff
  18. import (
  19. "context"
  20. "testing"
  21. "time"
  22. )
  23. func TestBackoff_NextDelay(t *testing.T) {
  24. t.Parallel()
  25. tests := map[string]struct {
  26. minBackoff time.Duration
  27. maxBackoff time.Duration
  28. expectedRanges [][]time.Duration
  29. }{
  30. "exponential backoff with jitter honoring min and max": {
  31. minBackoff: 100 * time.Millisecond,
  32. maxBackoff: 10 * time.Second,
  33. expectedRanges: [][]time.Duration{
  34. {100 * time.Millisecond, 200 * time.Millisecond},
  35. {200 * time.Millisecond, 400 * time.Millisecond},
  36. {400 * time.Millisecond, 800 * time.Millisecond},
  37. {800 * time.Millisecond, 1600 * time.Millisecond},
  38. {1600 * time.Millisecond, 3200 * time.Millisecond},
  39. {3200 * time.Millisecond, 6400 * time.Millisecond},
  40. {6400 * time.Millisecond, 10000 * time.Millisecond},
  41. {6400 * time.Millisecond, 10000 * time.Millisecond},
  42. },
  43. },
  44. "exponential backoff with max equal to the end of a range": {
  45. minBackoff: 100 * time.Millisecond,
  46. maxBackoff: 800 * time.Millisecond,
  47. expectedRanges: [][]time.Duration{
  48. {100 * time.Millisecond, 200 * time.Millisecond},
  49. {200 * time.Millisecond, 400 * time.Millisecond},
  50. {400 * time.Millisecond, 800 * time.Millisecond},
  51. {400 * time.Millisecond, 800 * time.Millisecond},
  52. },
  53. },
  54. "exponential backoff with max equal to the end of a range + 1": {
  55. minBackoff: 100 * time.Millisecond,
  56. maxBackoff: 801 * time.Millisecond,
  57. expectedRanges: [][]time.Duration{
  58. {100 * time.Millisecond, 200 * time.Millisecond},
  59. {200 * time.Millisecond, 400 * time.Millisecond},
  60. {400 * time.Millisecond, 800 * time.Millisecond},
  61. {800 * time.Millisecond, 801 * time.Millisecond},
  62. {800 * time.Millisecond, 801 * time.Millisecond},
  63. },
  64. },
  65. "exponential backoff with max equal to the end of a range - 1": {
  66. minBackoff: 100 * time.Millisecond,
  67. maxBackoff: 799 * time.Millisecond,
  68. expectedRanges: [][]time.Duration{
  69. {100 * time.Millisecond, 200 * time.Millisecond},
  70. {200 * time.Millisecond, 400 * time.Millisecond},
  71. {400 * time.Millisecond, 799 * time.Millisecond},
  72. {400 * time.Millisecond, 799 * time.Millisecond},
  73. },
  74. },
  75. "min backoff is equal to max": {
  76. minBackoff: 100 * time.Millisecond,
  77. maxBackoff: 100 * time.Millisecond,
  78. expectedRanges: [][]time.Duration{
  79. {100 * time.Millisecond, 100 * time.Millisecond},
  80. {100 * time.Millisecond, 100 * time.Millisecond},
  81. {100 * time.Millisecond, 100 * time.Millisecond},
  82. },
  83. },
  84. "min backoff is greater then max": {
  85. minBackoff: 200 * time.Millisecond,
  86. maxBackoff: 100 * time.Millisecond,
  87. expectedRanges: [][]time.Duration{
  88. {200 * time.Millisecond, 200 * time.Millisecond},
  89. {200 * time.Millisecond, 200 * time.Millisecond},
  90. {200 * time.Millisecond, 200 * time.Millisecond},
  91. },
  92. },
  93. }
  94. for testName, testData := range tests {
  95. testData := testData
  96. t.Run(testName, func(t *testing.T) {
  97. t.Parallel()
  98. b := New(context.Background(), Config{
  99. MinBackoff: testData.minBackoff,
  100. MaxBackoff: testData.maxBackoff,
  101. MaxRetries: len(testData.expectedRanges),
  102. })
  103. for _, expectedRange := range testData.expectedRanges {
  104. delay := b.NextDelay()
  105. if delay < expectedRange[0] || delay > expectedRange[1] {
  106. t.Errorf("%d expected to be within %d and %d", delay, expectedRange[0], expectedRange[1])
  107. }
  108. }
  109. })
  110. }
  111. }