2019-11-24 20:27:59 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Clock is an interface for a clock.
|
2019-11-24 20:27:59 +00:00
|
|
|
type Clock interface {
|
|
|
|
Now() time.Time
|
|
|
|
After(d time.Duration) <-chan time.Time
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// RealClock is the implementation of a clock for production code.
|
2019-11-24 20:27:59 +00:00
|
|
|
type RealClock struct{}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Now return the current time.
|
2019-11-24 20:27:59 +00:00
|
|
|
func (RealClock) Now() time.Time {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// After return a channel receiving the time after the defined duration.
|
2019-11-24 20:27:59 +00:00
|
|
|
func (RealClock) After(d time.Duration) <-chan time.Time {
|
|
|
|
return time.After(d)
|
|
|
|
}
|
2022-12-21 10:31:21 +00:00
|
|
|
|
|
|
|
// TestingClock implementation of clock for tests.
|
|
|
|
type TestingClock struct {
|
|
|
|
now time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now return the stored clock.
|
|
|
|
func (dc *TestingClock) Now() time.Time {
|
|
|
|
return dc.now
|
|
|
|
}
|
|
|
|
|
|
|
|
// After return a channel receiving the time after duration has elapsed.
|
|
|
|
func (dc *TestingClock) After(d time.Duration) <-chan time.Time {
|
|
|
|
return time.After(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set set the time of the clock.
|
|
|
|
func (dc *TestingClock) Set(now time.Time) {
|
|
|
|
dc.now = now
|
|
|
|
}
|