Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

62 linhas
1.5 KiB

  1. /*
  2. Copyright 2017 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package backoff
  14. import (
  15. "math"
  16. "testing"
  17. "time"
  18. )
  19. // Test if exponential backoff helper can produce correct series of
  20. // retry delays.
  21. func TestBackoff(t *testing.T) {
  22. b := ExponentialBackoff{minBackoff, maxBackoff}
  23. tests := []struct {
  24. retries int
  25. min time.Duration
  26. max time.Duration
  27. }{
  28. {
  29. retries: 0,
  30. min: minBackoff,
  31. max: minBackoff,
  32. },
  33. {
  34. retries: 1,
  35. min: minBackoff,
  36. max: time.Duration(rate * float64(minBackoff)),
  37. },
  38. {
  39. retries: 3,
  40. min: time.Duration(math.Pow(rate, 3) * (1 - jitter) * float64(minBackoff)),
  41. max: time.Duration(math.Pow(rate, 3) * float64(minBackoff)),
  42. },
  43. {
  44. retries: 1000,
  45. min: time.Duration((1 - jitter) * float64(maxBackoff)),
  46. max: maxBackoff,
  47. },
  48. }
  49. for _, test := range tests {
  50. got := b.Delay(test.retries)
  51. if float64(got) < float64(test.min) || float64(got) > float64(test.max) {
  52. t.Errorf("delay(%v) = %v, want in range [%v, %v]", test.retries, got, test.min, test.max)
  53. }
  54. }
  55. }