2018-01-09 18:57:14 +00:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
|
|
|
// Clock allows for injecting fake or real clocks into code that
|
|
|
|
// needs to do arbitrary things based on time.
|
|
|
|
type Clock interface {
|
|
|
|
Now() time.Time
|
|
|
|
Since(time.Time) time.Duration
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:23:56 +00:00
|
|
|
// After is the same as time.After(d).
|
2018-01-09 18:57:14 +00:00
|
|
|
func (RealClock) After(d time.Duration) <-chan time.Time {
|
|
|
|
return time.After(d)
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:23:56 +00:00
|
|
|
// NewTimer is the same as time.NewTimer(d)
|
2018-01-09 18:57:14 +00:00
|
|
|
func (RealClock) NewTimer(d time.Duration) Timer {
|
|
|
|
return &realTimer{
|
|
|
|
timer: time.NewTimer(d),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:23:56 +00:00
|
|
|
// Tick is the same as time.Tick(d)
|
2018-01-09 18:57:14 +00:00
|
|
|
func (RealClock) Tick(d time.Duration) <-chan time.Time {
|
|
|
|
return time.Tick(d)
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:23:56 +00:00
|
|
|
// Sleep is the same as time.Sleep(d)
|
2018-01-09 18:57:14 +00:00
|
|
|
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)
|
|
|
|
}
|