clock

package
Version: v1.3.13 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2021 License: Apache-2.0 Imports: 4 Imported by: 8

Documentation

Overview

Package clock contains time utilities, and types that allow mocking system time in tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SecondsSince

func SecondsSince(ts TimeSource, t time.Time) float64

SecondsSince returns the time in seconds elapsed since t until now, as measured by the TimeSource.

func SleepContext

func SleepContext(ctx context.Context, d time.Duration) error

SleepContext sleeps for at least the specified duration. Returns ctx.Err() iff the context is done before the deadline.

func SleepSource

func SleepSource(ctx context.Context, d time.Duration, s TimeSource) error

SleepSource sleeps for at least the specified duration, as measured by the TimeSource. Returns ctx.Err() iff the context is done before the deadline.

Types

type FakeTimeSource

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

FakeTimeSource provides time that can be arbitrarily set. For tests only.

func NewFake

func NewFake(t time.Time) *FakeTimeSource

NewFake creates a FakeTimeSource instance.

func (*FakeTimeSource) NewTimer

func (f *FakeTimeSource) NewTimer(d time.Duration) Timer

NewTimer returns a fake Timer.

func (*FakeTimeSource) Now

func (f *FakeTimeSource) Now() time.Time

Now returns the time value this instance contains.

func (*FakeTimeSource) Set

func (f *FakeTimeSource) Set(t time.Time)

Set updates the time that this instance will report.

type PredefinedFake

type PredefinedFake struct {
	Base   time.Time
	Delays []time.Duration
	Next   int
}

PredefinedFake is a TimeSource that returns a predefined set of times computed as base time + delays[i]. Delays don't have to be monotonic.

func (*PredefinedFake) NewTimer

func (p *PredefinedFake) NewTimer(d time.Duration) Timer

NewTimer creates a timer with the specified delay. Not implemented.

func (*PredefinedFake) Now

func (p *PredefinedFake) Now() time.Time

Now returns the current time, which depends on how many times this method has already been invoked. Must not be called more than len(delays) times.

type TimeSource

type TimeSource interface {
	// Now returns the current time as seen by this TimeSource.
	Now() time.Time
	// NewTimer creates a timer that fires after the specified duration.
	NewTimer(d time.Duration) Timer
}

TimeSource can provide the current time, or be replaced by a mock in tests to return specific values.

var System TimeSource = systemTimeSource{}

System is a default TimeSource that provides system time.

type Timer

type Timer interface {
	// Chan returns a channel which is used to deliver the event.
	Chan() <-chan time.Time
	// Stop prevents the Timer from firing. Returns false if the event has
	// already fired, or the Timer has been stopped.
	Stop() bool
}

Timer represents an event that fires with time passage. See time.Timer type for intuition on how it works.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL