| /* |
| Copyright 2014 The Kubernetes Authors. |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| */ |
| |
| package clock |
| |
| import "time" |
| |
| // PassiveClock allows for injecting fake or real clocks into code |
| // that needs to read the current time but does not support scheduling |
| // activity in the future. |
| type PassiveClock interface { |
| Now() time.Time |
| Since(time.Time) time.Duration |
| } |
| |
| // Clock allows for injecting fake or real clocks into code that |
| // needs to do arbitrary things based on time. |
| type Clock interface { |
| PassiveClock |
| After(d time.Duration) <-chan time.Time |
| NewTimer(d time.Duration) Timer |
| Sleep(d time.Duration) |
| Tick(d time.Duration) <-chan time.Time |
| } |
| |
| var _ = Clock(RealClock{}) |
| |
| // RealClock really calls time.Now() |
| type RealClock struct{} |
| |
| // Now returns the current time. |
| func (RealClock) Now() time.Time { |
| return time.Now() |
| } |
| |
| // Since returns time since the specified timestamp. |
| func (RealClock) Since(ts time.Time) time.Duration { |
| return time.Since(ts) |
| } |
| |
| // After is the same as time.After(d). |
| func (RealClock) After(d time.Duration) <-chan time.Time { |
| return time.After(d) |
| } |
| |
| // NewTimer is the same as time.NewTimer(d) |
| func (RealClock) NewTimer(d time.Duration) Timer { |
| return &realTimer{ |
| timer: time.NewTimer(d), |
| } |
| } |
| |
| // Tick is the same as time.Tick(d) |
| func (RealClock) Tick(d time.Duration) <-chan time.Time { |
| return time.Tick(d) |
| } |
| |
| // Sleep is the same as time.Sleep(d) |
| func (RealClock) Sleep(d time.Duration) { |
| time.Sleep(d) |
| } |
| |
| // Timer allows for injecting fake or real timers into code that |
| // needs to do arbitrary things based on time. |
| type Timer interface { |
| C() <-chan time.Time |
| Stop() bool |
| Reset(d time.Duration) bool |
| } |
| |
| var _ = Timer(&realTimer{}) |
| |
| // realTimer is backed by an actual time.Timer. |
| type realTimer struct { |
| timer *time.Timer |
| } |
| |
| // C returns the underlying timer's channel. |
| func (r *realTimer) C() <-chan time.Time { |
| return r.timer.C |
| } |
| |
| // Stop calls Stop() on the underlying timer. |
| func (r *realTimer) Stop() bool { |
| return r.timer.Stop() |
| } |
| |
| // Reset calls Reset() on the underlying timer. |
| func (r *realTimer) Reset(d time.Duration) bool { |
| return r.timer.Reset(d) |
| } |