clock.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package clock
  14. import "time"
  15. // PassiveClock allows for injecting fake or real clocks into code
  16. // that needs to read the current time but does not support scheduling
  17. // activity in the future.
  18. type PassiveClock interface {
  19. Now() time.Time
  20. Since(time.Time) time.Duration
  21. }
  22. // Clock allows for injecting fake or real clocks into code that
  23. // needs to do arbitrary things based on time.
  24. type Clock interface {
  25. PassiveClock
  26. // After returns the channel of a new Timer.
  27. // This method does not allow to free/GC the backing timer before it fires. Use
  28. // NewTimer instead.
  29. After(d time.Duration) <-chan time.Time
  30. // NewTimer returns a new Timer.
  31. NewTimer(d time.Duration) Timer
  32. // Sleep sleeps for the provided duration d.
  33. // Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.
  34. Sleep(d time.Duration)
  35. // Tick returns the channel of a new Ticker.
  36. // This method does not allow to free/GC the backing ticker. Use
  37. // NewTicker from WithTicker instead.
  38. Tick(d time.Duration) <-chan time.Time
  39. }
  40. // WithTicker allows for injecting fake or real clocks into code that
  41. // needs to do arbitrary things based on time.
  42. type WithTicker interface {
  43. Clock
  44. // NewTicker returns a new Ticker.
  45. NewTicker(time.Duration) Ticker
  46. }
  47. // WithDelayedExecution allows for injecting fake or real clocks into
  48. // code that needs to make use of AfterFunc functionality.
  49. type WithDelayedExecution interface {
  50. Clock
  51. // AfterFunc executes f in its own goroutine after waiting
  52. // for d duration and returns a Timer whose channel can be
  53. // closed by calling Stop() on the Timer.
  54. AfterFunc(d time.Duration, f func()) Timer
  55. }
  56. // WithTickerAndDelayedExecution allows for injecting fake or real clocks
  57. // into code that needs Ticker and AfterFunc functionality
  58. type WithTickerAndDelayedExecution interface {
  59. WithTicker
  60. // AfterFunc executes f in its own goroutine after waiting
  61. // for d duration and returns a Timer whose channel can be
  62. // closed by calling Stop() on the Timer.
  63. AfterFunc(d time.Duration, f func()) Timer
  64. }
  65. // Ticker defines the Ticker interface.
  66. type Ticker interface {
  67. C() <-chan time.Time
  68. Stop()
  69. }
  70. var _ = WithTicker(RealClock{})
  71. // RealClock really calls time.Now()
  72. type RealClock struct{}
  73. // Now returns the current time.
  74. func (RealClock) Now() time.Time {
  75. return time.Now()
  76. }
  77. // Since returns time since the specified timestamp.
  78. func (RealClock) Since(ts time.Time) time.Duration {
  79. return time.Since(ts)
  80. }
  81. // After is the same as time.After(d).
  82. // This method does not allow to free/GC the backing timer before it fires. Use
  83. // NewTimer instead.
  84. func (RealClock) After(d time.Duration) <-chan time.Time {
  85. return time.After(d)
  86. }
  87. // NewTimer is the same as time.NewTimer(d)
  88. func (RealClock) NewTimer(d time.Duration) Timer {
  89. return &realTimer{
  90. timer: time.NewTimer(d),
  91. }
  92. }
  93. // AfterFunc is the same as time.AfterFunc(d, f).
  94. func (RealClock) AfterFunc(d time.Duration, f func()) Timer {
  95. return &realTimer{
  96. timer: time.AfterFunc(d, f),
  97. }
  98. }
  99. // Tick is the same as time.Tick(d)
  100. // This method does not allow to free/GC the backing ticker. Use
  101. // NewTicker instead.
  102. func (RealClock) Tick(d time.Duration) <-chan time.Time {
  103. return time.Tick(d)
  104. }
  105. // NewTicker returns a new Ticker.
  106. func (RealClock) NewTicker(d time.Duration) Ticker {
  107. return &realTicker{
  108. ticker: time.NewTicker(d),
  109. }
  110. }
  111. // Sleep is the same as time.Sleep(d)
  112. // Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.
  113. func (RealClock) Sleep(d time.Duration) {
  114. time.Sleep(d)
  115. }
  116. // Timer allows for injecting fake or real timers into code that
  117. // needs to do arbitrary things based on time.
  118. type Timer interface {
  119. C() <-chan time.Time
  120. Stop() bool
  121. Reset(d time.Duration) bool
  122. }
  123. var _ = Timer(&realTimer{})
  124. // realTimer is backed by an actual time.Timer.
  125. type realTimer struct {
  126. timer *time.Timer
  127. }
  128. // C returns the underlying timer's channel.
  129. func (r *realTimer) C() <-chan time.Time {
  130. return r.timer.C
  131. }
  132. // Stop calls Stop() on the underlying timer.
  133. func (r *realTimer) Stop() bool {
  134. return r.timer.Stop()
  135. }
  136. // Reset calls Reset() on the underlying timer.
  137. func (r *realTimer) Reset(d time.Duration) bool {
  138. return r.timer.Reset(d)
  139. }
  140. type realTicker struct {
  141. ticker *time.Ticker
  142. }
  143. func (r *realTicker) C() <-chan time.Time {
  144. return r.ticker.C
  145. }
  146. func (r *realTicker) Stop() {
  147. r.ticker.Stop()
  148. }