clockwork.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. The initial time of the
  31. // FakeClock will be an arbitrary non-zero time.
  32. func NewFakeClock() FakeClock {
  33. // use a fixture that does not fulfill Time.IsZero()
  34. return NewFakeClockAt(time.Date(1984, time.April, 4, 0, 0, 0, 0, time.UTC))
  35. }
  36. // NewFakeClockAt returns a FakeClock initialised at the given time.Time.
  37. func NewFakeClockAt(t time.Time) FakeClock {
  38. return &fakeClock{
  39. time: t,
  40. }
  41. }
  42. type realClock struct{}
  43. func (rc *realClock) After(d time.Duration) <-chan time.Time {
  44. return time.After(d)
  45. }
  46. func (rc *realClock) Sleep(d time.Duration) {
  47. time.Sleep(d)
  48. }
  49. func (rc *realClock) Now() time.Time {
  50. return time.Now()
  51. }
  52. type fakeClock struct {
  53. sleepers []*sleeper
  54. blockers []*blocker
  55. time time.Time
  56. l sync.RWMutex
  57. }
  58. // sleeper represents a caller of After or Sleep
  59. type sleeper struct {
  60. until time.Time
  61. done chan time.Time
  62. }
  63. // blocker represents a caller of BlockUntil
  64. type blocker struct {
  65. count int
  66. ch chan struct{}
  67. }
  68. // After mimics time.After; it waits for the given duration to elapse on the
  69. // fakeClock, then sends the current time on the returned channel.
  70. func (fc *fakeClock) After(d time.Duration) <-chan time.Time {
  71. fc.l.Lock()
  72. defer fc.l.Unlock()
  73. now := fc.time
  74. done := make(chan time.Time, 1)
  75. if d.Nanoseconds() == 0 {
  76. // special case - trigger immediately
  77. done <- now
  78. } else {
  79. // otherwise, add to the set of sleepers
  80. s := &sleeper{
  81. until: now.Add(d),
  82. done: done,
  83. }
  84. fc.sleepers = append(fc.sleepers, s)
  85. // and notify any blockers
  86. fc.blockers = notifyBlockers(fc.blockers, len(fc.sleepers))
  87. }
  88. return done
  89. }
  90. // notifyBlockers notifies all the blockers waiting until the
  91. // given number of sleepers are waiting on the fakeClock. It
  92. // returns an updated slice of blockers (i.e. those still waiting)
  93. func notifyBlockers(blockers []*blocker, count int) (newBlockers []*blocker) {
  94. for _, b := range blockers {
  95. if b.count == count {
  96. close(b.ch)
  97. } else {
  98. newBlockers = append(newBlockers, b)
  99. }
  100. }
  101. return
  102. }
  103. // Sleep blocks until the given duration has passed on the fakeClock
  104. func (fc *fakeClock) Sleep(d time.Duration) {
  105. <-fc.After(d)
  106. }
  107. // Time returns the current time of the fakeClock
  108. func (fc *fakeClock) Now() time.Time {
  109. fc.l.RLock()
  110. t := fc.time
  111. fc.l.RUnlock()
  112. return t
  113. }
  114. // Advance advances fakeClock to a new point in time, ensuring channels from any
  115. // previous invocations of After are notified appropriately before returning
  116. func (fc *fakeClock) Advance(d time.Duration) {
  117. fc.l.Lock()
  118. defer fc.l.Unlock()
  119. end := fc.time.Add(d)
  120. var newSleepers []*sleeper
  121. for _, s := range fc.sleepers {
  122. if end.Sub(s.until) >= 0 {
  123. s.done <- end
  124. } else {
  125. newSleepers = append(newSleepers, s)
  126. }
  127. }
  128. fc.sleepers = newSleepers
  129. fc.blockers = notifyBlockers(fc.blockers, len(fc.sleepers))
  130. fc.time = end
  131. }
  132. // BlockUntil will block until the fakeClock has the given number of sleepers
  133. // (callers of Sleep or After)
  134. func (fc *fakeClock) BlockUntil(n int) {
  135. fc.l.Lock()
  136. // Fast path: current number of sleepers is what we're looking for
  137. if len(fc.sleepers) == n {
  138. fc.l.Unlock()
  139. return
  140. }
  141. // Otherwise, set up a new blocker
  142. b := &blocker{
  143. count: n,
  144. ch: make(chan struct{}),
  145. }
  146. fc.blockers = append(fc.blockers, b)
  147. fc.l.Unlock()
  148. <-b.ch
  149. }