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