crontest

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package crontest tests an app's crons: Run runs one by name with its dependencies, and Clock is a virtual clock to test schedules with — time stands still until the test moves it.

clock := crontest.NewClock(time.Date(2026, 1, 5, 6, 59, 0, 0, time.UTC))
app := web.New(web.CronClock(clock), web.Provide(NewStore))
app.Start(ctx)
defer app.Stop(ctx)
clock.WaitTimers(1)          // the scheduler waits for its tick
clock.Advance(time.Minute)   // 07:00: the cron runs
crontest.Wait(app)           // and is done

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(app *web.App, name string) error

Run runs the app's cron name now, its dependencies built by the app, and returns its error (a panic's too).

func Wait

func Wait(app *web.App) error

Wait waits until no cron the schedule started is still running: call it after a run's work is seen and before moving the clock again, as a tick finding the previous run still going is skipped.

Types

type Clock

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

Clock is a virtual cron.Clock: Now is what the test set, and timers fire when Advance or Set moves time past them.

func NewClock

func NewClock(t time.Time) *Clock

NewClock is a virtual clock showing t.

func (*Clock) Advance

func (c *Clock) Advance(d time.Duration)

Advance moves the clock d forward, firing the timers due in order.

func (*Clock) NewTimer

func (c *Clock) NewTimer(d time.Duration) cron.Timer

NewTimer is a timer firing once Now reaches d from now.

func (*Clock) Next

func (c *Clock) Next() time.Time

Next is when the earliest waiting timer fires; the zero time without one.

func (*Clock) Now

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

func (*Clock) Set

func (c *Clock) Set(t time.Time)

Set moves the clock to t, firing the timers due in order. A scheduler makes its next timer only after the previous one fired: to see every tick, move the clock one tick at a time, waiting for the timers between (WaitTimers).

func (*Clock) WaitTimers

func (c *Clock) WaitTimers(n int) bool

WaitTimers waits until n timers are waiting — the scheduler is waiting for its crons' next ticks — for at most 5 seconds, reporting whether they are.

Jump to

Keyboard shortcuts

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