fakeclock

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package fakeclock provides support for testing users of a clock.

Index

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 Time

func Time(now time.Time) *Clock

Time returns a clock set to the given now time.

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

func Y2038(d time.Duration) *Clock

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

func (c *Clock) Add(d time.Duration) time.Time

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

func (c *Clock) AddDate(years, months, days int) time.Time

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

func (c *Clock) Next() (time.Time, bool)

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) Now

func (c *Clock) Now() time.Time

Now returns the current clock time.

func (*Clock) Schedule

func (c *Clock) Schedule(d time.Duration, f func(now time.Time)) clock.Event

Schedule implements the clock.Scheduler interface.

func (*Clock) Set

func (c *Clock) Set(t time.Time)

Set sets the given time to be the current clock time. It is possible to set t that is before the current clock time.

func (*Clock) Ticker

func (c *Clock) Ticker(d time.Duration) clock.Ticker

Ticker implements the clock.TickerScheduler interface. Note that the returned ticker does not adjust the time interval or drop ticks to make up for slow receivers.

func (*Clock) Timer

func (c *Clock) Timer(d time.Duration) clock.Timer

Timer implements the clock.TimerScheduler interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL