You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package common
  2. import (
  3. "sync"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestDefaultTicker(t *testing.T) {
  9. ticker := defaultTickerMaker(time.Millisecond * 10)
  10. <-ticker.Chan()
  11. ticker.Stop()
  12. }
  13. func TestRepeatTimer(t *testing.T) {
  14. ch := make(chan time.Time, 100)
  15. mtx := new(sync.Mutex)
  16. // tick() fires from start to end
  17. // (exclusive) in milliseconds with incr.
  18. // It locks on mtx, so subsequent calls
  19. // run in series.
  20. tick := func(startMs, endMs, incrMs time.Duration) {
  21. mtx.Lock()
  22. go func() {
  23. for tMs := startMs; tMs < endMs; tMs += incrMs {
  24. lt := time.Time{}
  25. lt = lt.Add(tMs * time.Millisecond)
  26. ch <- lt
  27. }
  28. mtx.Unlock()
  29. }()
  30. }
  31. // tock consumes Ticker.Chan() events and checks them against the ms in "timesMs".
  32. tock := func(t *testing.T, rt *RepeatTimer, timesMs []int64) {
  33. // Check against timesMs.
  34. for _, timeMs := range timesMs {
  35. tyme := <-rt.Chan()
  36. sinceMs := tyme.Sub(time.Time{}) / time.Millisecond
  37. assert.Equal(t, timeMs, int64(sinceMs))
  38. }
  39. // TODO detect number of running
  40. // goroutines to ensure that
  41. // no other times will fire.
  42. // See https://github.com/tendermint/tmlibs/issues/120.
  43. time.Sleep(time.Millisecond * 100)
  44. done := true
  45. select {
  46. case <-rt.Chan():
  47. done = false
  48. default:
  49. }
  50. assert.True(t, done)
  51. }
  52. tm := NewLogicalTickerMaker(ch)
  53. rt := NewRepeatTimerWithTickerMaker("bar", time.Second, tm)
  54. /* NOTE: Useful for debugging deadlocks...
  55. go func() {
  56. time.Sleep(time.Second * 3)
  57. trace := make([]byte, 102400)
  58. count := runtime.Stack(trace, true)
  59. fmt.Printf("Stack of %d bytes: %s\n", count, trace)
  60. }()
  61. */
  62. tick(0, 1000, 10)
  63. tock(t, rt, []int64{})
  64. tick(1000, 2000, 10)
  65. tock(t, rt, []int64{1000})
  66. tick(2005, 5000, 10)
  67. tock(t, rt, []int64{2005, 3005, 4005})
  68. tick(5001, 5999, 1)
  69. // Read 5005 instead of 5001 because
  70. // it's 1 second greater than 4005.
  71. tock(t, rt, []int64{5005})
  72. tick(6000, 7005, 1)
  73. tock(t, rt, []int64{6005})
  74. tick(7033, 8032, 1)
  75. tock(t, rt, []int64{7033})
  76. // After a reset, nothing happens
  77. // until two ticks are received.
  78. rt.Reset()
  79. tock(t, rt, []int64{})
  80. tick(8040, 8041, 1)
  81. tock(t, rt, []int64{})
  82. tick(9555, 9556, 1)
  83. tock(t, rt, []int64{9555})
  84. // After a stop, nothing more is sent.
  85. rt.Stop()
  86. tock(t, rt, []int64{})
  87. // Another stop panics.
  88. assert.Panics(t, func() { rt.Stop() })
  89. }