Documentation ¶
Overview ¶
Package timing provides a testable interface for timing and scheduling.
This makes it simple to update a module at a fixed interval or at a fixed point in time.
Typically, modules will make a scheduler:
mod.sch = timing.NewScheduler()
and use the scheduling calls to control the update timing:
mod.sch.Every(time.Second)
The Stream() goroutine will then loop over the ticker, and update the module with fresh information:
for range mod.sch.C { // update code. }
This will automatically suspend processing when the bar is hidden.
Modules should also use timing.Now() instead of time.Now() to control time during tests, as well as correctly track the machine's time zone.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdvanceBy ¶
AdvanceBy increments the test time by the given duration, and triggers any schedulers that were scheduled in the meantime.
func AdvanceTo ¶
AdvanceTo increments the test time to the given time, and triggers any schedulers that were scheduled in the meantime.
func ExitTestMode ¶
func ExitTestMode()
ExitTestMode exits test mode for all schedulers. Any schedulers created after this call will be real.
func NextTick ¶
NextTick triggers the next scheduler and returns the trigger time. It also advances test time to match.
func Now ¶
Now returns the current time, in the machine's local time zone.
IMPORTANT: This function differs from time.Now() in that the time zone can change between invocations. Use timing.Now().Local() to get a consistent zone, however note that the zone will be the machine's zone at the start of the bar and may be out of date.
Types ¶
type Scheduler ¶
type Scheduler struct { // A channel that receives an empty struct for each tick of the scheduler. C <-chan struct{} // contains filtered or unexported fields }
Scheduler represents a trigger that can be repeating or one-off, and is intrinsically tied to the running bar. This means that if the trigger condition occurs while the bar is paused, it will not fire until the bar is next resumed, making it ideal for scheduling work that should only be performed while the bar is active.
func (*Scheduler) After ¶
After sets the scheduler to trigger after a delay. This will replace any pending triggers.
func (*Scheduler) At ¶
At sets the scheduler to trigger a specific time. This will replace any pending triggers.
func (*Scheduler) Every ¶
Every sets the scheduler to trigger at an interval. This will replace any pending triggers.