fake_clock.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 testing
  14. import (
  15. "sync"
  16. "time"
  17. "k8s.io/utils/clock"
  18. )
  19. var (
  20. _ = clock.PassiveClock(&FakePassiveClock{})
  21. _ = clock.WithTicker(&FakeClock{})
  22. _ = clock.Clock(&IntervalClock{})
  23. )
  24. // FakePassiveClock implements PassiveClock, but returns an arbitrary time.
  25. type FakePassiveClock struct {
  26. lock sync.RWMutex
  27. time time.Time
  28. }
  29. // FakeClock implements clock.Clock, but returns an arbitrary time.
  30. type FakeClock struct {
  31. FakePassiveClock
  32. // waiters are waiting for the fake time to pass their specified time
  33. waiters []*fakeClockWaiter
  34. }
  35. type fakeClockWaiter struct {
  36. targetTime time.Time
  37. stepInterval time.Duration
  38. skipIfBlocked bool
  39. destChan chan time.Time
  40. fired bool
  41. afterFunc func()
  42. }
  43. // NewFakePassiveClock returns a new FakePassiveClock.
  44. func NewFakePassiveClock(t time.Time) *FakePassiveClock {
  45. return &FakePassiveClock{
  46. time: t,
  47. }
  48. }
  49. // NewFakeClock constructs a fake clock set to the provided time.
  50. func NewFakeClock(t time.Time) *FakeClock {
  51. return &FakeClock{
  52. FakePassiveClock: *NewFakePassiveClock(t),
  53. }
  54. }
  55. // Now returns f's time.
  56. func (f *FakePassiveClock) Now() time.Time {
  57. f.lock.RLock()
  58. defer f.lock.RUnlock()
  59. return f.time
  60. }
  61. // Since returns time since the time in f.
  62. func (f *FakePassiveClock) Since(ts time.Time) time.Duration {
  63. f.lock.RLock()
  64. defer f.lock.RUnlock()
  65. return f.time.Sub(ts)
  66. }
  67. // SetTime sets the time on the FakePassiveClock.
  68. func (f *FakePassiveClock) SetTime(t time.Time) {
  69. f.lock.Lock()
  70. defer f.lock.Unlock()
  71. f.time = t
  72. }
  73. // After is the fake version of time.After(d).
  74. func (f *FakeClock) After(d time.Duration) <-chan time.Time {
  75. f.lock.Lock()
  76. defer f.lock.Unlock()
  77. stopTime := f.time.Add(d)
  78. ch := make(chan time.Time, 1) // Don't block!
  79. f.waiters = append(f.waiters, &fakeClockWaiter{
  80. targetTime: stopTime,
  81. destChan: ch,
  82. })
  83. return ch
  84. }
  85. // NewTimer constructs a fake timer, akin to time.NewTimer(d).
  86. func (f *FakeClock) NewTimer(d time.Duration) clock.Timer {
  87. f.lock.Lock()
  88. defer f.lock.Unlock()
  89. stopTime := f.time.Add(d)
  90. ch := make(chan time.Time, 1) // Don't block!
  91. timer := &fakeTimer{
  92. fakeClock: f,
  93. waiter: fakeClockWaiter{
  94. targetTime: stopTime,
  95. destChan: ch,
  96. },
  97. }
  98. f.waiters = append(f.waiters, &timer.waiter)
  99. return timer
  100. }
  101. // AfterFunc is the Fake version of time.AfterFunc(d, cb).
  102. func (f *FakeClock) AfterFunc(d time.Duration, cb func()) clock.Timer {
  103. f.lock.Lock()
  104. defer f.lock.Unlock()
  105. stopTime := f.time.Add(d)
  106. ch := make(chan time.Time, 1) // Don't block!
  107. timer := &fakeTimer{
  108. fakeClock: f,
  109. waiter: fakeClockWaiter{
  110. targetTime: stopTime,
  111. destChan: ch,
  112. afterFunc: cb,
  113. },
  114. }
  115. f.waiters = append(f.waiters, &timer.waiter)
  116. return timer
  117. }
  118. // Tick constructs a fake ticker, akin to time.Tick
  119. func (f *FakeClock) Tick(d time.Duration) <-chan time.Time {
  120. if d <= 0 {
  121. return nil
  122. }
  123. f.lock.Lock()
  124. defer f.lock.Unlock()
  125. tickTime := f.time.Add(d)
  126. ch := make(chan time.Time, 1) // hold one tick
  127. f.waiters = append(f.waiters, &fakeClockWaiter{
  128. targetTime: tickTime,
  129. stepInterval: d,
  130. skipIfBlocked: true,
  131. destChan: ch,
  132. })
  133. return ch
  134. }
  135. // NewTicker returns a new Ticker.
  136. func (f *FakeClock) NewTicker(d time.Duration) clock.Ticker {
  137. f.lock.Lock()
  138. defer f.lock.Unlock()
  139. tickTime := f.time.Add(d)
  140. ch := make(chan time.Time, 1) // hold one tick
  141. f.waiters = append(f.waiters, &fakeClockWaiter{
  142. targetTime: tickTime,
  143. stepInterval: d,
  144. skipIfBlocked: true,
  145. destChan: ch,
  146. })
  147. return &fakeTicker{
  148. c: ch,
  149. }
  150. }
  151. // Step moves the clock by Duration and notifies anyone that's called After,
  152. // Tick, or NewTimer.
  153. func (f *FakeClock) Step(d time.Duration) {
  154. f.lock.Lock()
  155. defer f.lock.Unlock()
  156. f.setTimeLocked(f.time.Add(d))
  157. }
  158. // SetTime sets the time.
  159. func (f *FakeClock) SetTime(t time.Time) {
  160. f.lock.Lock()
  161. defer f.lock.Unlock()
  162. f.setTimeLocked(t)
  163. }
  164. // Actually changes the time and checks any waiters. f must be write-locked.
  165. func (f *FakeClock) setTimeLocked(t time.Time) {
  166. f.time = t
  167. newWaiters := make([]*fakeClockWaiter, 0, len(f.waiters))
  168. for i := range f.waiters {
  169. w := f.waiters[i]
  170. if !w.targetTime.After(t) {
  171. if w.skipIfBlocked {
  172. select {
  173. case w.destChan <- t:
  174. w.fired = true
  175. default:
  176. }
  177. } else {
  178. w.destChan <- t
  179. w.fired = true
  180. }
  181. if w.afterFunc != nil {
  182. w.afterFunc()
  183. }
  184. if w.stepInterval > 0 {
  185. for !w.targetTime.After(t) {
  186. w.targetTime = w.targetTime.Add(w.stepInterval)
  187. }
  188. newWaiters = append(newWaiters, w)
  189. }
  190. } else {
  191. newWaiters = append(newWaiters, f.waiters[i])
  192. }
  193. }
  194. f.waiters = newWaiters
  195. }
  196. // HasWaiters returns true if After or AfterFunc has been called on f but not yet satisfied (so you can
  197. // write race-free tests).
  198. func (f *FakeClock) HasWaiters() bool {
  199. f.lock.RLock()
  200. defer f.lock.RUnlock()
  201. return len(f.waiters) > 0
  202. }
  203. // Sleep is akin to time.Sleep
  204. func (f *FakeClock) Sleep(d time.Duration) {
  205. f.Step(d)
  206. }
  207. // IntervalClock implements clock.PassiveClock, but each invocation of Now steps the clock forward the specified duration.
  208. // IntervalClock technically implements the other methods of clock.Clock, but each implementation is just a panic.
  209. //
  210. // Deprecated: See SimpleIntervalClock for an alternative that only has the methods of PassiveClock.
  211. type IntervalClock struct {
  212. Time time.Time
  213. Duration time.Duration
  214. }
  215. // Now returns i's time.
  216. func (i *IntervalClock) Now() time.Time {
  217. i.Time = i.Time.Add(i.Duration)
  218. return i.Time
  219. }
  220. // Since returns time since the time in i.
  221. func (i *IntervalClock) Since(ts time.Time) time.Duration {
  222. return i.Time.Sub(ts)
  223. }
  224. // After is unimplemented, will panic.
  225. // TODO: make interval clock use FakeClock so this can be implemented.
  226. func (*IntervalClock) After(d time.Duration) <-chan time.Time {
  227. panic("IntervalClock doesn't implement After")
  228. }
  229. // NewTimer is unimplemented, will panic.
  230. // TODO: make interval clock use FakeClock so this can be implemented.
  231. func (*IntervalClock) NewTimer(d time.Duration) clock.Timer {
  232. panic("IntervalClock doesn't implement NewTimer")
  233. }
  234. // AfterFunc is unimplemented, will panic.
  235. // TODO: make interval clock use FakeClock so this can be implemented.
  236. func (*IntervalClock) AfterFunc(d time.Duration, f func()) clock.Timer {
  237. panic("IntervalClock doesn't implement AfterFunc")
  238. }
  239. // Tick is unimplemented, will panic.
  240. // TODO: make interval clock use FakeClock so this can be implemented.
  241. func (*IntervalClock) Tick(d time.Duration) <-chan time.Time {
  242. panic("IntervalClock doesn't implement Tick")
  243. }
  244. // NewTicker has no implementation yet and is omitted.
  245. // TODO: make interval clock use FakeClock so this can be implemented.
  246. func (*IntervalClock) NewTicker(d time.Duration) clock.Ticker {
  247. panic("IntervalClock doesn't implement NewTicker")
  248. }
  249. // Sleep is unimplemented, will panic.
  250. func (*IntervalClock) Sleep(d time.Duration) {
  251. panic("IntervalClock doesn't implement Sleep")
  252. }
  253. var _ = clock.Timer(&fakeTimer{})
  254. // fakeTimer implements clock.Timer based on a FakeClock.
  255. type fakeTimer struct {
  256. fakeClock *FakeClock
  257. waiter fakeClockWaiter
  258. }
  259. // C returns the channel that notifies when this timer has fired.
  260. func (f *fakeTimer) C() <-chan time.Time {
  261. return f.waiter.destChan
  262. }
  263. // Stop stops the timer and returns true if the timer has not yet fired, or false otherwise.
  264. func (f *fakeTimer) Stop() bool {
  265. f.fakeClock.lock.Lock()
  266. defer f.fakeClock.lock.Unlock()
  267. newWaiters := make([]*fakeClockWaiter, 0, len(f.fakeClock.waiters))
  268. for i := range f.fakeClock.waiters {
  269. w := f.fakeClock.waiters[i]
  270. if w != &f.waiter {
  271. newWaiters = append(newWaiters, w)
  272. }
  273. }
  274. f.fakeClock.waiters = newWaiters
  275. return !f.waiter.fired
  276. }
  277. // Reset resets the timer to the fake clock's "now" + d. It returns true if the timer has not yet
  278. // fired, or false otherwise.
  279. func (f *fakeTimer) Reset(d time.Duration) bool {
  280. f.fakeClock.lock.Lock()
  281. defer f.fakeClock.lock.Unlock()
  282. active := !f.waiter.fired
  283. f.waiter.fired = false
  284. f.waiter.targetTime = f.fakeClock.time.Add(d)
  285. var isWaiting bool
  286. for i := range f.fakeClock.waiters {
  287. w := f.fakeClock.waiters[i]
  288. if w == &f.waiter {
  289. isWaiting = true
  290. break
  291. }
  292. }
  293. if !isWaiting {
  294. f.fakeClock.waiters = append(f.fakeClock.waiters, &f.waiter)
  295. }
  296. return active
  297. }
  298. type fakeTicker struct {
  299. c <-chan time.Time
  300. }
  301. func (t *fakeTicker) C() <-chan time.Time {
  302. return t.c
  303. }
  304. func (t *fakeTicker) Stop() {
  305. }