exponential_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package backoff
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. )
  7. func TestBackOff(t *testing.T) {
  8. var (
  9. testInitialInterval = 500 * time.Millisecond
  10. testRandomizationFactor = 0.1
  11. testMultiplier = 2.0
  12. testMaxInterval = 5 * time.Second
  13. )
  14. exp := NewExponential()
  15. exp.InitialInterval = testInitialInterval
  16. exp.RandomizationFactor = testRandomizationFactor
  17. exp.Multiplier = testMultiplier
  18. exp.MaxInterval = testMaxInterval
  19. exp.Reset()
  20. var expectedResults = []time.Duration{500, 1000, 2000, 4000, 5000, 5000, 5000, 5000, 5000, 5000}
  21. for i, d := range expectedResults {
  22. expectedResults[i] = d * time.Millisecond
  23. }
  24. for _, expected := range expectedResults {
  25. assertEquals(t, expected, exp.currentInterval)
  26. // Assert that the next back off falls in the expected range.
  27. var minInterval = expected - time.Duration(testRandomizationFactor*float64(expected))
  28. var maxInterval = expected + time.Duration(testRandomizationFactor*float64(expected))
  29. var actualInterval = exp.GetSleepTime()
  30. if !(minInterval <= actualInterval && actualInterval <= maxInterval) {
  31. t.Error("error")
  32. }
  33. exp.IncrementCurrentInterval()
  34. }
  35. }
  36. func TestGetRandomizedInterval(t *testing.T) {
  37. // 33% chance of being 1.
  38. assertEquals(t, 1, getRandomValueFromInterval(0.5, 0, 2))
  39. assertEquals(t, 1, getRandomValueFromInterval(0.5, 0.33, 2))
  40. // 33% chance of being 2.
  41. assertEquals(t, 2, getRandomValueFromInterval(0.5, 0.34, 2))
  42. assertEquals(t, 2, getRandomValueFromInterval(0.5, 0.66, 2))
  43. // 33% chance of being 3.
  44. assertEquals(t, 3, getRandomValueFromInterval(0.5, 0.67, 2))
  45. assertEquals(t, 3, getRandomValueFromInterval(0.5, 0.99, 2))
  46. }
  47. type TestClock struct {
  48. i time.Duration
  49. start time.Time
  50. }
  51. func (c *TestClock) Now() time.Time {
  52. t := c.start.Add(c.i)
  53. c.i += time.Second
  54. return t
  55. }
  56. func TestBackOffOverflow(t *testing.T) {
  57. var (
  58. testInitialInterval time.Duration = math.MaxInt64 / 2
  59. testMaxInterval time.Duration = math.MaxInt64
  60. testMultiplier float64 = 2.1
  61. )
  62. exp := NewExponential()
  63. exp.InitialInterval = testInitialInterval
  64. exp.Multiplier = testMultiplier
  65. exp.MaxInterval = testMaxInterval
  66. exp.Reset()
  67. exp.IncrementCurrentInterval()
  68. // Assert that when an overflow is possible the current varerval time.Duration is set to the max varerval time.Duration .
  69. assertEquals(t, testMaxInterval, exp.currentInterval)
  70. }
  71. func assertEquals(t *testing.T, expected, value time.Duration) {
  72. if expected != value {
  73. t.Errorf("got: %d, expected: %d", value, expected)
  74. }
  75. }