backoff_test.go 736 B

1234567891011121314151617181920212223242526272829303132
  1. package gensupport
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestBackoff(t *testing.T) {
  7. eb := &ExponentialBackoff{Base: time.Millisecond, Max: time.Second}
  8. var total time.Duration
  9. for n, max := 0, 2*time.Millisecond; ; n, max = n+1, max*2 {
  10. if n > 100 {
  11. // There's less than 1 in 10^28 of taking longer than 100 iterations,
  12. // so this is just to check we don't have an infinite loop.
  13. t.Fatalf("Failed to timeout after 100 iterations.")
  14. }
  15. pause, retry := eb.Pause()
  16. if !retry {
  17. break
  18. }
  19. if 0 > pause || pause >= max {
  20. t.Errorf("Iteration %d: pause = %v; want in range [0, %v)", n, pause, max)
  21. }
  22. total += pause
  23. }
  24. if total < time.Second {
  25. t.Errorf("Total time = %v; want > %v", total, time.Second)
  26. }
  27. }