clockwork.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package clockwork
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // Clock provides an interface that packages can use instead of directly
  7. // using the time module, so that chronology-related behavior can be tested
  8. type Clock interface {
  9. After(d time.Duration) <-chan time.Time
  10. Sleep(d time.Duration)
  11. Now() time.Time
  12. }
  13. // FakeClock provides an interface for a clock which can be
  14. // manually advanced through time
  15. type FakeClock interface {
  16. Clock
  17. // Advance advances the FakeClock to a new point in time, ensuring any existing
  18. // sleepers are notified appropriately before returning
  19. Advance(d time.Duration)
  20. // BlockUntil will block until the FakeClock has the given number of
  21. // sleepers (callers of Sleep or After)
  22. BlockUntil(n int)
  23. }
  24. // NewRealClock returns a Clock which simply delegates calls to the actual time
  25. // package; it should be used by packages in production.
  26. func NewRealClock() Clock {
  27. return &realClock{}
  28. }
  29. // NewFakeClock returns a FakeClock implementation which can be
  30. // manually advanced through time for testing.
  31. func NewFakeClock() FakeClock {
  32. return &fakeClock{
  33. l: sync.RWMutex{},
  34. }
  35. }
  36. type realClock struct{}
  37. func (rc *realClock) After(d time.Duration) <-chan time.Time {
  38. return time.After(d)
  39. }
  40. func (rc *realClock) Sleep(d time.Duration) {
  41. time.Sleep(d)
  42. }
  43. func (rc *realClock) Now() time.Time {
  44. return time.Now()
  45. }
  46. type fakeClock struct {
  47. sleepers []*sleeper
  48. blockers []*blocker
  49. time time.Time
  50. l sync.RWMutex
  51. }
  52. // sleeper represents a caller of After or Sleep
  53. type sleeper struct {
  54. until time.Time
  55. done chan time.Time
  56. }
  57. // blocker represents a caller of BlockUntil
  58. type blocker struct {
  59. count int
  60. ch chan struct{}
  61. }
  62. // After mimics time.After; it waits for the given duration to elapse on the
  63. // fakeClock, then sends the current time on the returned channel.
  64. func (fc *fakeClock) After(d time.Duration) <-chan time.Time {
  65. fc.l.Lock()
  66. defer fc.l.Unlock()
  67. now := fc.time
  68. done := make(chan time.Time, 1)
  69. if d.Nanoseconds() == 0 {
  70. // special case - trigger immediately
  71. done <- now
  72. } else {
  73. // otherwise, add to the set of sleepers
  74. s := &sleeper{
  75. until: now.Add(d),
  76. done: done,
  77. }
  78. fc.sleepers = append(fc.sleepers, s)
  79. // and notify any blockers
  80. fc.blockers = notifyBlockers(fc.blockers, len(fc.sleepers))
  81. }
  82. return done
  83. }
  84. // notifyBlockers notifies all the blockers waiting until the
  85. // given number of sleepers are waiting on the fakeClock. It
  86. // returns an updated slice of blockers (i.e. those still waiting)
  87. func notifyBlockers(blockers []*blocker, count int) (newBlockers []*blocker) {
  88. for _, b := range blockers {
  89. if b.count == count {
  90. close(b.ch)
  91. } else {
  92. newBlockers = append(newBlockers, b)
  93. }
  94. }
  95. return
  96. }
  97. // Sleep blocks until the given duration has passed on the fakeClock
  98. func (fc *fakeClock) Sleep(d time.Duration) {
  99. <-fc.After(d)
  100. }
  101. // Time returns the current time of the fakeClock
  102. func (fc *fakeClock) Now() time.Time {
  103. fc.l.Lock()
  104. defer fc.l.Unlock()
  105. return fc.time
  106. }
  107. // Advance advances fakeClock to a new point in time, ensuring channels from any
  108. // previous invocations of After are notified appropriately before returning
  109. func (fc *fakeClock) Advance(d time.Duration) {
  110. fc.l.Lock()
  111. defer fc.l.Unlock()
  112. end := fc.time.Add(d)
  113. var newSleepers []*sleeper
  114. for _, s := range fc.sleepers {
  115. if end.Sub(s.until) >= 0 {
  116. s.done <- end
  117. } else {
  118. newSleepers = append(newSleepers, s)
  119. }
  120. }
  121. fc.sleepers = newSleepers
  122. fc.blockers = notifyBlockers(fc.blockers, len(fc.sleepers))
  123. fc.time = end
  124. }
  125. // BlockUntil will block until the fakeClock has the given number of sleepers
  126. // (callers of Sleep or After)
  127. func (fc *fakeClock) BlockUntil(n int) {
  128. fc.l.Lock()
  129. // Fast path: current number of sleepers is what we're looking for
  130. if len(fc.sleepers) == n {
  131. fc.l.Unlock()
  132. return
  133. }
  134. // Otherwise, set up a new blocker
  135. b := &blocker{
  136. count: n,
  137. ch: make(chan struct{}),
  138. }
  139. fc.blockers = append(fc.blockers, b)
  140. fc.l.Unlock()
  141. <-b.ch
  142. }