clock

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package clock abstracts time with a real clock and a manually advanced concurrent-safe fake.

Design

Injected time makes certificate validity, token expiry, queue backoff and other time-dependent behavior testable without wall-clock delays. Fake coordinates Now, After and manual advancement under a lock. Scheduling and retry policy remain with callers; the package's timer interface is limited to After.

References

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// Now returns the current time.
	Now() time.Time
	// Since returns the elapsed time since t.
	Since(t time.Time) time.Duration
	// After waits for the duration to elapse and then sends the current time
	// on the returned channel.
	After(d time.Duration) <-chan time.Time
}

Clock is the subset of the time package that the library depends on.

type Fake

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

Fake is a manually advanced Clock for tests. It is safe for concurrent use.

func NewFake

func NewFake(now time.Time) *Fake

NewFake returns a Fake clock set to now.

func (*Fake) Advance

func (f *Fake) Advance(d time.Duration)

Advance moves the clock forward by d and fires any waiters that are due.

func (*Fake) After

func (f *Fake) After(d time.Duration) <-chan time.Time

After implements Clock. The returned channel fires once Advance or Set moves the clock to or past now+d. A non-positive duration fires immediately.

func (*Fake) Now

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

Now returns the fake clock's current time under its lock.

func (*Fake) Pending

func (f *Fake) Pending() int

Pending reports how many After waiters have not fired yet.

func (*Fake) Set

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

Set moves the clock to t (forward or backward) and fires any waiters that are due.

func (*Fake) Since

func (f *Fake) Since(t time.Time) time.Duration

Since implements Clock.

type Real

type Real struct{}

Real is a Clock backed by the time package.

func (Real) After

func (Real) After(d time.Duration) <-chan time.Time

After implements Clock.

func (Real) Now

func (Real) Now() time.Time

Now returns the current wall-clock time.

func (Real) Since

func (Real) Since(t time.Time) time.Duration

Since implements Clock.

Jump to

Keyboard shortcuts

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