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 // Tick returns the channel of a new Ticker. 43 // This method does not allow to free/GC the backing ticker. Use 44 // NewTicker from WithTicker instead. 45 Tick(d time.Duration) <-chan time.Time 46 } 47 48 // WithTicker allows for injecting fake or real clocks into code that 49 // needs to do arbitrary things based on time. 50 type WithTicker interface { 51 Clock 52 // NewTicker returns a new Ticker. 53 NewTicker(time.Duration) Ticker 54 } 55 56 // WithDelayedExecution allows for injecting fake or real clocks into 57 // code that needs to make use of AfterFunc functionality. 58 type WithDelayedExecution 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 // WithTickerAndDelayedExecution allows for injecting fake or real clocks 67 // into code that needs Ticker and AfterFunc functionality 68 type WithTickerAndDelayedExecution interface { 69 WithTicker 70 // AfterFunc executes f in its own goroutine after waiting 71 // for d duration and returns a Timer whose channel can be 72 // closed by calling Stop() on the Timer. 73 AfterFunc(d time.Duration, f func()) Timer 74 } 75 76 // Ticker defines the Ticker interface. 77 type Ticker interface { 78 C() <-chan time.Time 79 Stop() 80 } 81 82 var _ = WithTicker(RealClock{}) 83 84 // RealClock really calls time.Now() 85 type RealClock struct{} 86 87 // Now returns the current time. 88 func (RealClock) Now() time.Time { 89 return time.Now() 90 } 91 92 // Since returns time since the specified timestamp. 93 func (RealClock) Since(ts time.Time) time.Duration { 94 return time.Since(ts) 95 } 96 97 // After is the same as time.After(d). 98 // This method does not allow to free/GC the backing timer before it fires. Use 99 // NewTimer instead. 100 func (RealClock) After(d time.Duration) <-chan time.Time { 101 return time.After(d) 102 } 103 104 // NewTimer is the same as time.NewTimer(d) 105 func (RealClock) NewTimer(d time.Duration) Timer { 106 return &realTimer{ 107 timer: time.NewTimer(d), 108 } 109 } 110 111 // AfterFunc is the same as time.AfterFunc(d, f). 112 func (RealClock) AfterFunc(d time.Duration, f func()) Timer { 113 return &realTimer{ 114 timer: time.AfterFunc(d, f), 115 } 116 } 117 118 // Tick is the same as time.Tick(d) 119 // This method does not allow to free/GC the backing ticker. Use 120 // NewTicker instead. 121 func (RealClock) Tick(d time.Duration) <-chan time.Time { 122 return time.Tick(d) 123 } 124 125 // NewTicker returns a new Ticker. 126 func (RealClock) NewTicker(d time.Duration) Ticker { 127 return &realTicker{ 128 ticker: time.NewTicker(d), 129 } 130 } 131 132 // Sleep is the same as time.Sleep(d) 133 // Consider making the sleep interruptible by using 'select' on a context channel and a timer channel. 134 func (RealClock) Sleep(d time.Duration) { 135 time.Sleep(d) 136 } 137 138 // Timer allows for injecting fake or real timers into code that 139 // needs to do arbitrary things based on time. 140 type Timer interface { 141 C() <-chan time.Time 142 Stop() bool 143 Reset(d time.Duration) bool 144 } 145 146 var _ = Timer(&realTimer{}) 147 148 // realTimer is backed by an actual time.Timer. 149 type realTimer struct { 150 timer *time.Timer 151 } 152 153 // C returns the underlying timer's channel. 154 func (r *realTimer) C() <-chan time.Time { 155 return r.timer.C 156 } 157 158 // Stop calls Stop() on the underlying timer. 159 func (r *realTimer) Stop() bool { 160 return r.timer.Stop() 161 } 162 163 // Reset calls Reset() on the underlying timer. 164 func (r *realTimer) Reset(d time.Duration) bool { 165 return r.timer.Reset(d) 166 } 167 168 type realTicker struct { 169 ticker *time.Ticker 170 } 171 172 func (r *realTicker) C() <-chan time.Time { 173 return r.ticker.C 174 } 175 176 func (r *realTicker) Stop() { 177 r.ticker.Stop() 178 } 179