summaryrefslogtreecommitdiff
path: root/vendor/k8s.io/apimachinery/pkg/util/clock/clock.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/k8s.io/apimachinery/pkg/util/clock/clock.go')
-rw-r--r--vendor/k8s.io/apimachinery/pkg/util/clock/clock.go39
1 files changed, 32 insertions, 7 deletions
diff --git a/vendor/k8s.io/apimachinery/pkg/util/clock/clock.go b/vendor/k8s.io/apimachinery/pkg/util/clock/clock.go
index 0d739d961..1689e62e8 100644
--- a/vendor/k8s.io/apimachinery/pkg/util/clock/clock.go
+++ b/vendor/k8s.io/apimachinery/pkg/util/clock/clock.go
@@ -21,11 +21,18 @@ 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 {
- Now() time.Time
- Since(time.Time) time.Duration
+ PassiveClock
After(time.Duration) <-chan time.Time
NewTimer(time.Duration) Timer
Sleep(time.Duration)
@@ -66,10 +73,15 @@ func (RealClock) Sleep(d time.Duration) {
time.Sleep(d)
}
-// FakeClock implements Clock, but returns an arbitrary time.
-type FakeClock struct {
+// FakePassiveClock implements PassiveClock, but returns an arbitrary time.
+type FakePassiveClock struct {
lock sync.RWMutex
time time.Time
+}
+
+// FakeClock implements Clock, but returns an arbitrary time.
+type FakeClock struct {
+ FakePassiveClock
// waiters are waiting for the fake time to pass their specified time
waiters []fakeClockWaiter
@@ -82,26 +94,39 @@ type fakeClockWaiter struct {
destChan chan time.Time
}
+func NewFakePassiveClock(t time.Time) *FakePassiveClock {
+ return &FakePassiveClock{
+ time: t,
+ }
+}
+
func NewFakeClock(t time.Time) *FakeClock {
return &FakeClock{
- time: t,
+ FakePassiveClock: *NewFakePassiveClock(t),
}
}
// Now returns f's time.
-func (f *FakeClock) Now() time.Time {
+func (f *FakePassiveClock) Now() time.Time {
f.lock.RLock()
defer f.lock.RUnlock()
return f.time
}
// Since returns time since the time in f.
-func (f *FakeClock) Since(ts time.Time) time.Duration {
+func (f *FakePassiveClock) Since(ts time.Time) time.Duration {
f.lock.RLock()
defer f.lock.RUnlock()
return f.time.Sub(ts)
}
+// Sets the time.
+func (f *FakePassiveClock) SetTime(t time.Time) {
+ f.lock.Lock()
+ defer f.lock.Unlock()
+ f.time = t
+}
+
// Fake version of time.After(d).
func (f *FakeClock) After(d time.Duration) <-chan time.Time {
f.lock.Lock()