Documentation
¶
Overview ¶
Package fakeclock provides support for testing users of a clock.
Index ¶
- type Clock
- func (c *Clock) Add(d time.Duration) time.Time
- func (c *Clock) AddDate(years, months, days int) time.Time
- func (c *Clock) Next() (time.Time, bool)
- func (c *Clock) Now() time.Time
- func (c *Clock) Schedule(d time.Duration, f func(now time.Time)) clock.Event
- func (c *Clock) Set(t time.Time)
- func (c *Clock) Ticker(d time.Duration) clock.Ticker
- func (c *Clock) Timer(d time.Duration) clock.Timer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock struct {
// contains filtered or unexported fields
}
Clock is a fake clock.Scheduler interface implementation that is safe for concurrent use by multiple goroutines.
Use Next, Set, Add and AddDate methods to change clock time. Advancing the time triggers scheduled events, timers and tickers.
Note that the order in which events scheduled for the same time are triggered is undefined, but it is guaranteed that all events that are not after the new current time are triggered on clock time change (even if old time is equal to the next time value).
The zero Clock defaults to zero time and is ready for use.
func Go ¶
func Go() *Clock
Go returns a clock set to the Go initial release date. That is, it is set to 2009-11-10 23:00:00 UTC.
func Unix ¶
func Unix() *Clock
Unix returns a clock set to the Unix epoch time. That is, it is set to 1970-01-01 00:00:00 UTC.
func Y2038 ¶
Y2038 returns a clock set to d duration after the Year 2038 problem time. That is, it is set to the given duration after 2038-01-19 03:14:07 UTC, the latest time that can be properly encoded as a 32-bit integer that is a number of seconds after the Unix epoch.
func (*Clock) Add ¶
Add adds the given duration to the current time and returns the resulting clock time. It is possible to add a negative duration.
It is safe for concurrent use and is a shorthand for
now := c.Now().Add(d) c.Set(now)
func (*Clock) AddDate ¶
AddDate adds the duration corresponding to the given number of years, months and days relative to the current time and returns the resulting clock time. It is possible to add negative values.
It is safe for concurrent use and is a shorthand for
now := c.Now().AddDate(years, months, days) c.Set(now)
func (*Clock) Next ¶
Next advances the time to the next timer or ticker event and returns the new current time. If there are events in the past, the time is not changed. It returns false if there are no scheduled events. Otherwise it returns true and runs at least one scheduled event.
Example ¶
var c Clock var wg sync.WaitGroup for i := 0; i < 3; i++ { i := i timer := c.Timer(time.Duration(i+1) * time.Second) wg.Add(1) go func() { defer wg.Done() defer timer.Stop() now := <-timer.C() fmt.Printf("timer %d fired at %v\n", i, now) }() } for { if _, ok := c.Next(); !ok { break } } wg.Wait()
Output: timer 0 fired at 0001-01-01 00:00:01 +0000 UTC timer 1 fired at 0001-01-01 00:00:02 +0000 UTC timer 2 fired at 0001-01-01 00:00:03 +0000 UTC
func (*Clock) Set ¶
Set sets the given time to be the current clock time. It is possible to set t that is before the current clock time.