clock

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToContext

func ToContext(ctx context.Context, clock IClock) context.Context

ToContext saves Clock into a given context, returning a new one

Types

type IClock

type IClock interface {
	// Now returns current time
	Now() time.Time
	// After returns a channel that will send one and only one
	// timestamp once it waited for the given duration
	//
	// Implementation of this method needs to guarantee that
	// the returned time is either equal or greater than the given
	// one plus duration
	After(time.Duration) <-chan time.Time
	// Until returns a channel that will send one and only one
	// timestamp once the provided time has occurred
	//
	// Implementation of this method needs to guarantee that
	// the returned time is either equal or greater than the given
	// one
	Until(time.Time) <-chan time.Time
	// Changes returns a channel that will send a message when
	// the clock detects a change (in case of a real clock, can
	// be a significant drift forward, or a drift backward; for
	// mock clock, an explicit change)
	Changes() <-chan time.Time
}

IClock is a generic interface for clocks

func FromContext

func FromContext(ctx context.Context) (c IClock, err error)

FromContext retrieves a Clock from a given context, if there's any. If there's none, it'll create a Host clock

func Host

func Host(ctx context.Context) (c IClock, err error)

Host is a clock source that uses time package as a source of time.

type Mock

type Mock struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Mock clock is a fake clock that doesn't change unless explicitly instructed to.

func NewMock

func NewMock() *Mock

NewMock creates a new Mock clock, set at the start of UNIX time

func NewMockAt

func NewMockAt(t time.Time) *Mock

NewMockAt creates a new Mock clock at a specific point in time

func (*Mock) Add

func (m *Mock) Add(duration time.Duration)

func (*Mock) After

func (m *Mock) After(duration time.Duration) <-chan time.Time

func (*Mock) Changes

func (m *Mock) Changes() <-chan time.Time

func (*Mock) Now

func (m *Mock) Now() time.Time

func (*Mock) Set

func (m *Mock) Set(t time.Time)

func (*Mock) Until

func (m *Mock) Until(t time.Time) <-chan time.Time

Jump to

Keyboard shortcuts

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