clock

package
v1.22.2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: MIT Imports: 2 Imported by: 2

Documentation

Overview

Package clock provides extensions to the time package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventTimeSource

type EventTimeSource struct {
	// contains filtered or unexported fields
}

EventTimeSource is a fake TimeSource. Unlike other fake clock implementations, the methods are synchronous, so when you call Advance or Update, all triggered timers from AfterFunc will fire before the method returns, in the same goroutine.

Example
package main

import (
	"fmt"
	"time"

	"go.temporal.io/server/common/clock"
)

func main() {
	// Create a new fake timeSource.
	source := clock.NewEventTimeSource()

	// Create a timer which fires after 1 second.
	source.AfterFunc(time.Second, func() {
		fmt.Println("timer fired")
	})

	// Advance the time source by 1 second.
	fmt.Println("advancing time source by 1 second")
	source.Advance(time.Second)
	fmt.Println("time source advanced")

}
Output:

advancing time source by 1 second
timer fired
time source advanced

func NewEventTimeSource

func NewEventTimeSource() *EventTimeSource

NewEventTimeSource returns a EventTimeSource with the current time set to Unix zero: 1970-01-01 00:00:00 +0000 UTC.

func (*EventTimeSource) Advance added in v1.22.0

func (ts *EventTimeSource) Advance(d time.Duration)

Advance the timer by the specified duration.

func (*EventTimeSource) AfterFunc added in v1.22.0

func (ts *EventTimeSource) AfterFunc(d time.Duration, f func()) Timer

AfterFunc return a timer that will fire after the specified duration. It is important to note that the timeSource is locked while the callback is called. This means that you must be cautious about calling any other mutating methods on the timeSource from within the callback. Doing so will probably result in a deadlock. To avoid this, you may want to wrap all such calls in a goroutine. If the duration is non-positive, the callback will fire immediately before AfterFunc returns.

func (*EventTimeSource) Now

func (ts *EventTimeSource) Now() time.Time

Now return the current time.

func (*EventTimeSource) Update

func (ts *EventTimeSource) Update(now time.Time) *EventTimeSource

Update the fake current time. It returns the timeSource so that you can chain calls like this: timeSource := NewEventTimeSource().Update(time.Now())

type RealTimeSource

type RealTimeSource struct{}

RealTimeSource is a timeSource that uses the real wall timeSource time. The zero value is valid.

func NewRealTimeSource

func NewRealTimeSource() RealTimeSource

NewRealTimeSource returns a timeSource that uses the real wall timeSource time.

func (RealTimeSource) AfterFunc added in v1.22.0

func (ts RealTimeSource) AfterFunc(d time.Duration, f func()) Timer

AfterFunc is a pass-through to time.AfterFunc.

func (RealTimeSource) Now

func (ts RealTimeSource) Now() time.Time

Now returns the current time, with the location set to UTC.

type TimeSource

type TimeSource interface {
	Now() time.Time
	AfterFunc(d time.Duration, f func()) Timer
}

TimeSource is an interface to make it easier to test code that uses time.

type Timer added in v1.22.0

type Timer interface {
	// Reset changes the expiration time of the timer. It returns true if the timer had been active, false if the
	// timer had expired or been stopped.
	Reset(d time.Duration) bool
	// Stop prevents the Timer from firing. It returns true if the call stops the timer, false if the timer has
	// already expired or been stopped.
	Stop() bool
}

Timer is a timer returned by TimeSource.AfterFunc. Unlike the timers returned by time.NewTimer or time.Ticker, this timer does not have a channel. That is because the callback already reacts to the timer firing.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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