fakeclock

package
v1.9.30 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package fakeclock provides an injectable Clock for tests that exercise time-sensitive logic — hook freshness windows, heartbeat staleness, log-rotation maintenance — without sleeping or depending on wall-clock progress.

Per TUI-TEST-PLAN.md §6.3 (fakeClock): production call sites that currently invoke time.Now() directly (e.g. instance.go's hook fast-path window) must be refactored to take a Clock so they can be driven by Fake in tests. Real{} is the production wiring.

Usage:

c := fakeclock.New(time.Unix(0, 0))
inst.UpdateStatus("running", c.Now())
c.Advance(2 * time.Second)
inst.UpdateStatus("idle", c.Now()) // 2s later from the model's POV

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time
}

Clock is the seam production code should depend on instead of time.Now().

type Fake

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

Fake is a controllable clock. Concurrent Now / Advance / Set are safe.

func New

func New(seed time.Time) *Fake

New returns a Fake seeded at the given time.

func (*Fake) Advance

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

Advance moves the clock forward by d. Negative durations are ignored — time only moves forward, matching the contract production code expects from time.Now() between successive calls.

func (*Fake) Now

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

Now returns the current fake time.

func (*Fake) Set

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

Set jumps the clock to t (forward or backward). Tests that need to simulate a specific wall-clock instant (e.g. a known timestamp parsed out of a hook payload) should use Set; otherwise prefer Advance.

type Real

type Real struct{}

Real is the wall-clock implementation. Use this in production wiring.

func (Real) Now

func (Real) Now() time.Time

Now returns time.Now().

Jump to

Keyboard shortcuts

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