tries_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package backoff
  2. import (
  3. "math/rand"
  4. "testing"
  5. "time"
  6. )
  7. func TestMaxTriesHappy(t *testing.T) {
  8. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  9. max := 17 + r.Intn(13)
  10. bo := WithMaxRetries(&ZeroBackOff{}, uint64(max))
  11. // Load up the tries count, but reset should clear the record
  12. for ix := 0; ix < max/2; ix++ {
  13. bo.NextBackOff()
  14. }
  15. bo.Reset()
  16. // Now fill the tries count all the way up
  17. for ix := 0; ix < max; ix++ {
  18. d := bo.NextBackOff()
  19. if d == Stop {
  20. t.Errorf("returned Stop on try %d", ix)
  21. }
  22. }
  23. // We have now called the BackOff max number of times, we expect
  24. // the next result to be Stop, even if we try it multiple times
  25. for ix := 0; ix < 7; ix++ {
  26. d := bo.NextBackOff()
  27. if d != Stop {
  28. t.Error("invalid next back off")
  29. }
  30. }
  31. // Reset makes it all work again
  32. bo.Reset()
  33. d := bo.NextBackOff()
  34. if d == Stop {
  35. t.Error("returned Stop after reset")
  36. }
  37. }
  38. func TestMaxTriesZero(t *testing.T) {
  39. // It might not make sense, but its okay to send a zero
  40. bo := WithMaxRetries(&ZeroBackOff{}, uint64(0))
  41. for ix := 0; ix < 11; ix++ {
  42. d := bo.NextBackOff()
  43. if d == Stop {
  44. t.Errorf("returned Stop on try %d", ix)
  45. }
  46. }
  47. }