clock.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Clock allows for injecting fake or real clocks into code that
  16. // needs to do arbitrary things based on time.
  17. type Clock interface {
  18. Now() time.Time
  19. Since(time.Time) time.Duration
  20. After(d time.Duration) <-chan time.Time
  21. NewTimer(d time.Duration) Timer
  22. Sleep(d time.Duration)
  23. Tick(d time.Duration) <-chan time.Time
  24. }
  25. var _ = Clock(RealClock{})
  26. // RealClock really calls time.Now()
  27. type RealClock struct{}
  28. // Now returns the current time.
  29. func (RealClock) Now() time.Time {
  30. return time.Now()
  31. }
  32. // Since returns time since the specified timestamp.
  33. func (RealClock) Since(ts time.Time) time.Duration {
  34. return time.Since(ts)
  35. }
  36. // After is the same as time.After(d).
  37. func (RealClock) After(d time.Duration) <-chan time.Time {
  38. return time.After(d)
  39. }
  40. // NewTimer is the same as time.NewTimer(d)
  41. func (RealClock) NewTimer(d time.Duration) Timer {
  42. return &realTimer{
  43. timer: time.NewTimer(d),
  44. }
  45. }
  46. // Tick is the same as time.Tick(d)
  47. func (RealClock) Tick(d time.Duration) <-chan time.Time {
  48. return time.Tick(d)
  49. }
  50. // Sleep is the same as time.Sleep(d)
  51. func (RealClock) Sleep(d time.Duration) {
  52. time.Sleep(d)
  53. }
  54. // Timer allows for injecting fake or real timers into code that
  55. // needs to do arbitrary things based on time.
  56. type Timer interface {
  57. C() <-chan time.Time
  58. Stop() bool
  59. Reset(d time.Duration) bool
  60. }
  61. var _ = Timer(&realTimer{})
  62. // realTimer is backed by an actual time.Timer.
  63. type realTimer struct {
  64. timer *time.Timer
  65. }
  66. // C returns the underlying timer's channel.
  67. func (r *realTimer) C() <-chan time.Time {
  68. return r.timer.C
  69. }
  70. // Stop calls Stop() on the underlying timer.
  71. func (r *realTimer) Stop() bool {
  72. return r.timer.Stop()
  73. }
  74. // Reset calls Reset() on the underlying timer.
  75. func (r *realTimer) Reset(d time.Duration) bool {
  76. return r.timer.Reset(d)
  77. }