Documentation ¶
Overview ¶
Package flextime improves time testability by replacing the backend clock flexibly.
It has a set of following 9 functions similar to the standard time package, making it easy to migrate from standard time package.
now := flextime.Now() flextime.Sleep() d := flextime.Until(date) d := flextime.Since(date) <-flextime.After(5*time.Second) flextime.AfterFunc(5*time.Second, func() { fmt.Println("Done") }) timer := flextime.NewTimer(10*time.Second) ticker := flextime.NewTicker(10*time.Second) ch := flextime.Tick(3*time.Second)
By default, it behaves the same as the standard time package, but allows us to change or fix the current time by using `Fix` and `Set` function.
func () { // Set time restore := flextime.Set(time.Date(2001, time.May, 1, 10, 10, 10, 0, time.UTC)) defer restore() now = flextime.Now() // returned set time }() func () { // Fix time restore := flextime.Fix(time.Date(2001, time.May, 1, 10, 10, 10, 0, time.UTC)) defer restore() now = flextime.Now() // returned fixed time }()
Also, we can replace the backend clock by implementing our own `Clock` interface and combining it with the Switch function.
restore := flextime.Switch(clock) defer restore()
Index ¶
- func After(d time.Duration) <-chan time.Time
- func Fix(t time.Time) (restore func())
- func Now() time.Time
- func NowFunc(now func() time.Time) (restore func())
- func Restore()
- func Set(t time.Time) (restore func())
- func Since(t time.Time) time.Duration
- func Sleep(d time.Duration)
- func Switch(c Clock) (restore func())
- func Tick(d time.Duration) <-chan time.Time
- func Until(t time.Time) time.Duration
- type Clock
- type NowSleeper
- type Ticker
- type Timer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶
After waits for the duration to elapse and then sends the current time on the returned channel using backend Clock.
func Fix ¶
Fix switches backend Clock and fixes the current time to the specified time. This differs from `Set` in that the time does not change even if the time elapses during the test. However, when Sleep is called, the virtual time is passed without actually pausing. It returns a restore func and we can restore time behavior by calling it after the test.
func Set ¶
Set switches backend Clock and sets the current time to the specified time. It internally holds the offset and is affected by the passage of time during the test. When Sleep is called, the virtual time is passed without actually pausing. It returns a restore func and we can restore time behavior by calling it after the test.
func Switch ¶
func Switch(c Clock) (restore func())
Switch switches backend Clock. Is it useful for testing.
Types ¶
type Clock ¶
type Clock interface { Now() time.Time Sleep(d time.Duration) Since(t time.Time) time.Duration Until(t time.Time) time.Duration After(d time.Duration) <-chan time.Time AfterFunc(d time.Duration, f func()) *Timer NewTimer(d time.Duration) *Timer NewTicker(d time.Duration) *Ticker Tick(d time.Duration) <-chan time.Time }
Clock is an interface that implements the functions of the standard time package
func NewFakeClock ¶
func NewFakeClock(ns NowSleeper) Clock
NewFakeClock accepts a NowSleeper interface and returns an object with a Clock interface.
type NowSleeper ¶
NowSleeper is, as the name implies, an interface with Now and Sleep methods. By simply implementing these two methods, we can create an object with a Clock interface by combining it with the NewFakeClock function.
type Ticker ¶
A Ticker holds a channel that delivers `ticks' of a clock at intervals. It has same API with time.Ticker
func NewTicker ¶
NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument using backend Clock.
type Timer ¶
The Timer type represents a single event. It has same API with time.Timer
func AfterFunc ¶
AfterFunc waits for the duration to elapse and then calls f in its own goroutine using backend Clock. It returns a Timer that can be used to cancel the call using its Stop method.
func NewTimer ¶
NewTimer creates a new Timer that will send the current time on its channel after at least duration d using backend Clock.