ticker_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package alerting
  2. //import (
  3. // "testing"
  4. // "time"
  5. //
  6. // "github.com/benbjohnson/clock"
  7. //)
  8. //
  9. //func inspectTick(tick time.Time, last time.Time, offset time.Duration, t *testing.T) {
  10. // if !tick.Equal(last.Add(time.Duration(1) * time.Second)) {
  11. // t.Fatalf("expected a tick 1 second more than prev, %s. got: %s", last, tick)
  12. // }
  13. //}
  14. //
  15. //// returns the new last tick seen
  16. //func assertAdvanceUntil(ticker *Ticker, last, desiredLast time.Time, offset, wait time.Duration, t *testing.T) time.Time {
  17. // for {
  18. // select {
  19. // case tick := <-ticker.C:
  20. // inspectTick(tick, last, offset, t)
  21. // last = tick
  22. // case <-time.NewTimer(wait).C:
  23. // if last.Before(desiredLast) {
  24. // t.Fatalf("waited %s for ticker to advance to %s, but only went up to %s", wait, desiredLast, last)
  25. // }
  26. // if last.After(desiredLast) {
  27. // t.Fatalf("timer advanced too far. should only have gone up to %s, but it went up to %s", desiredLast, last)
  28. // }
  29. // return last
  30. // }
  31. // }
  32. //}
  33. //
  34. //func assertNoAdvance(ticker *Ticker, desiredLast time.Time, wait time.Duration, t *testing.T) {
  35. // for {
  36. // select {
  37. // case tick := <-ticker.C:
  38. // t.Fatalf("timer should have stayed at %s, instead it advanced to %s", desiredLast, tick)
  39. // case <-time.NewTimer(wait).C:
  40. // return
  41. // }
  42. // }
  43. //}
  44. //
  45. //func TestTickerRetro1Hour(t *testing.T) {
  46. // offset := time.Duration(10) * time.Second
  47. // last := time.Unix(0, 0)
  48. // mock := clock.NewMock()
  49. // mock.Add(time.Duration(1) * time.Hour)
  50. // desiredLast := mock.Now().Add(-offset)
  51. // ticker := NewTicker(last, offset, mock)
  52. //
  53. // last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(10)*time.Millisecond, t)
  54. // assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
  55. //
  56. //}
  57. //
  58. //func TestAdvanceWithUpdateOffset(t *testing.T) {
  59. // offset := time.Duration(10) * time.Second
  60. // last := time.Unix(0, 0)
  61. // mock := clock.NewMock()
  62. // mock.Add(time.Duration(1) * time.Hour)
  63. // desiredLast := mock.Now().Add(-offset)
  64. // ticker := NewTicker(last, offset, mock)
  65. //
  66. // last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(10)*time.Millisecond, t)
  67. // assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
  68. //
  69. // // lowering offset should see a few more ticks
  70. // offset = time.Duration(5) * time.Second
  71. // ticker.updateOffset(offset)
  72. // desiredLast = mock.Now().Add(-offset)
  73. // last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(9)*time.Millisecond, t)
  74. // assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
  75. //
  76. // // advancing clock should see even more ticks
  77. // mock.Add(time.Duration(1) * time.Hour)
  78. // desiredLast = mock.Now().Add(-offset)
  79. // last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(8)*time.Millisecond, t)
  80. // assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
  81. //
  82. //}
  83. //
  84. //func getCase(lastSeconds, offsetSeconds int) (time.Time, time.Duration) {
  85. // last := time.Unix(int64(lastSeconds), 0)
  86. // offset := time.Duration(offsetSeconds) * time.Second
  87. // return last, offset
  88. //}
  89. //
  90. //func TestTickerNoAdvance(t *testing.T) {
  91. //
  92. // // it's 00:01:00 now. what are some cases where we don't want the ticker to advance?
  93. // mock := clock.NewMock()
  94. // mock.Add(time.Duration(60) * time.Second)
  95. //
  96. // type Case struct {
  97. // last int
  98. // offset int
  99. // }
  100. //
  101. // // note that some cases add up to now, others go into the future
  102. // cases := []Case{
  103. // {50, 10},
  104. // {50, 30},
  105. // {59, 1},
  106. // {59, 10},
  107. // {59, 30},
  108. // {60, 1},
  109. // {60, 10},
  110. // {60, 30},
  111. // {90, 1},
  112. // {90, 10},
  113. // {90, 30},
  114. // }
  115. // for _, c := range cases {
  116. // last, offset := getCase(c.last, c.offset)
  117. // ticker := NewTicker(last, offset, mock)
  118. // assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
  119. // }
  120. //}