v1.21.0 Latest Latest

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 4 Imported by: 0




This section is empty.


This section is empty.


This section is empty.


type Clock

type Clock interface {
	Now() time.Time
	Since(time.Time) time.Duration
	WithTimeout(context.Context, time.Duration) (context.Context, context.CancelFunc)

Clock defines how Fx accesses time. We keep the interface pretty minimal.

var System Clock = systemClock{}

System is the default implementation of Clock based on real time.

type Mock added in v1.21.0

type Mock struct {
	// contains filtered or unexported fields

Mock is a fake source of time. It implements standard time operations, but allows the user to control the passage of time.

Use the [Add] method to progress time.

func NewMock added in v1.21.0

func NewMock() *Mock

NewMock builds a new mock clock using the current actual time as the initial time.

func (*Mock) Add added in v1.21.0

func (c *Mock) Add(d time.Duration)

Add progresses time by the given duration. Other operations waiting for the time to advance will be resolved if they are within range.

Side effects of operations waiting for the time to advance will take effect on a best-effort basis. Avoid racing with operations that have side effects.

Panics if the duration is negative.

func (*Mock) AwaitScheduled added in v1.21.0

func (c *Mock) AwaitScheduled(n int)

AwaitScheduled blocks until there are at least N operations scheduled for the future.

func (*Mock) Now added in v1.21.0

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

Now reports the current time.

func (*Mock) Since added in v1.21.0

func (c *Mock) Since(t time.Time) time.Duration

Since reports the time elapsed since t. This is short for Now().Sub(t).

func (*Mock) Sleep added in v1.21.0

func (c *Mock) Sleep(d time.Duration)

Sleep pauses the current goroutine for the given duration.

With the mock clock, this will freeze until the clock is advanced with [Add] past the deadline.

func (*Mock) WithTimeout added in v1.21.0

func (c *Mock) WithTimeout(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc)

WithTimeout returns a new context with a deadline of now + d.

When the deadline is passed, the returned context's Done channel is closed and the context's Err method returns context.DeadlineExceeded. If the cancel function is called before the deadline is passed, the context's Err method returns context.Canceled.

Jump to

Keyboard shortcuts

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