clock

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package clock is the module's one time port: the current time, one-shot timers and tickers behind an interface, so that a test can drive every interval of the stack — MRP retransmission, the acknowledgement pump, the subscription reporting loop, commissioning windows, cluster timers — by hand instead of waiting for the wall clock.

Every package of the module takes its time from here: nothing outside this package calls time.Now, time.AfterFunc, time.NewTimer, time.NewTicker, time.Sleep, time.After, time.Tick, time.Since or time.Until in production code (clock/noraw_test.go fails the build on one). System is the standard library and the default of every owner; github.com/SukramJ/go-fabric/clock/clocktest.Fake is the test clock.

A host injects a clock through the owner's configuration — the `Clock clock.Clock` field of github.com/SukramJ/go-fabric/node.Config, github.com/SukramJ/go-fabric/bridge.Config and the other owners. A nil clock is System. A node hands its clock on to the bridge, the store's persist loop, OTA and mDNS, so one field moves the whole device side.

Clock.AfterFunc runs f in its own goroutine, as time.AfterFunc does; f must not assume it runs on the goroutine that armed the timer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(c Clock, d time.Duration) <-chan time.Time

After is c.NewTimer(d).C(), time.After on c. The timer is not stopped when the caller stops waiting; use Clock.NewTimer where that matters.

func Since

func Since(c Clock, t time.Time) time.Duration

Since is c.Now().Sub(t), time.Since on c.

func Sleep

func Sleep(c Clock, d time.Duration, done <-chan struct{}) bool

Sleep blocks on c for d or until done is closed, whichever comes first, and reports whether the full d elapsed. A nil done never closes.

func Until

func Until(c Clock, t time.Time) time.Duration

Until is t.Sub(c.Now()), time.Until on c.

Types

type Clock

type Clock interface {
	// Now is the current time.
	Now() time.Time
	// AfterFunc calls f in its own goroutine once d has elapsed. The
	// returned Timer's C is nil.
	AfterFunc(d time.Duration, f func()) Timer
	// NewTimer sends the current time on the Timer's C once d has elapsed.
	NewTimer(d time.Duration) Timer
	// NewTicker sends the current time on the Ticker's C every d. d must
	// be greater than zero.
	NewTicker(d time.Duration) Ticker
}

Clock is a time source: the current time, one-shot timers and tickers. Its methods are safe for concurrent use.

func Or

func Or(c Clock) Clock

Or is c, or System when c is nil — the default every owner applies.

type System

type System struct{}

System is the Clock of the time package. Its zero value is ready.

func (System) AfterFunc

func (System) AfterFunc(d time.Duration, f func()) Timer

AfterFunc is time.AfterFunc.

func (System) NewTicker

func (System) NewTicker(d time.Duration) Ticker

NewTicker is time.NewTicker.

func (System) NewTimer

func (System) NewTimer(d time.Duration) Timer

NewTimer is time.NewTimer.

func (System) Now

func (System) Now() time.Time

Now is time.Now.

type Ticker

type Ticker interface {
	// Stop turns the ticker off; no more ticks are sent.
	Stop()
	// Reset stops the ticker and restarts it with period d.
	Reset(d time.Duration)
	// C is the channel the ticks are delivered on.
	C() <-chan time.Time
}

Ticker is a periodic ticker a Clock started, with time.Ticker's semantics: a slow receiver misses ticks rather than queueing them.

type Timer

type Timer interface {
	// Stop prevents the timer from firing. It reports whether it did:
	// false means the timer had already fired or been stopped.
	Stop() bool
	// Reset re-arms the timer to fire after d. It reports whether the
	// timer had been active.
	Reset(d time.Duration) bool
	// C is the channel a timer from [Clock.NewTimer] delivers on; nil for
	// a timer from [Clock.AfterFunc].
	C() <-chan time.Time
}

Timer is a one-shot timer a Clock started. Its semantics are those of time.Timer since Go 1.23: after Stop or Reset returns, no stale value is received from C.

Directories

Path Synopsis
Package clocktest is the test clock of the module: Fake, a clock.Clock whose time moves only when the test moves it.
Package clocktest is the test clock of the module: Fake, a clock.Clock whose time moves only when the test moves it.

Jump to

Keyboard shortcuts

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