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