mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-12-22 13:00:19 +00:00
95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
/*
|
|
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)
|
|
}
|
|
|
|
// Same as time.After(d).
|
|
func (RealClock) After(d time.Duration) <-chan time.Time {
|
|
return time.After(d)
|
|
}
|
|
|
|
func (RealClock) NewTimer(d time.Duration) Timer {
|
|
return &realTimer{
|
|
timer: time.NewTimer(d),
|
|
}
|
|
}
|
|
|
|
func (RealClock) Tick(d time.Duration) <-chan time.Time {
|
|
return time.Tick(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)
|
|
}
|