Documentation
¶
Overview ¶
Package quartz is a library for testing time related code. It exports an interface Clock that mimics the standard library time package functions. In production, an implementation that calls thru to the standard library is used. In testing, a Mock clock is used to precisely control and intercept time functions.
Index ¶
- Variables
- type AdvanceWaiter
- type Call
- type Clock
- type Logger
- type Mock
- func (m *Mock) Advance(d time.Duration) AdvanceWaiter
- func (m *Mock) AdvanceNext() (time.Duration, AdvanceWaiter)
- func (m *Mock) AfterFunc(d time.Duration, f func(), tags ...string) *Timer
- func (m *Mock) NewTicker(d time.Duration, tags ...string) *Ticker
- func (m *Mock) NewTimer(d time.Duration, tags ...string) *Timer
- func (m *Mock) Now(tags ...string) time.Time
- func (m *Mock) Peek() (d time.Duration, ok bool)
- func (m *Mock) Set(t time.Time) AdvanceWaiter
- func (m *Mock) Since(t time.Time, tags ...string) time.Duration
- func (m *Mock) TickerFunc(ctx context.Context, d time.Duration, f func() error, tags ...string) Waiter
- func (m *Mock) Trap() Trapper
- func (m *Mock) Until(t time.Time, tags ...string) time.Duration
- func (m *Mock) WithLogger(l Logger) *Mock
- type Ticker
- type Timer
- type Trap
- type Trapper
- func (t Trapper) AfterFunc(tags ...string) *Trap
- func (t Trapper) NewTicker(tags ...string) *Trap
- func (t Trapper) NewTimer(tags ...string) *Trap
- func (t Trapper) Now(tags ...string) *Trap
- func (t Trapper) Since(tags ...string) *Trap
- func (t Trapper) TickerFunc(tags ...string) *Trap
- func (t Trapper) TickerFuncWait(tags ...string) *Trap
- func (t Trapper) TickerReset(tags ...string) *Trap
- func (t Trapper) TickerStop(tags ...string) *Trap
- func (t Trapper) TimerReset(tags ...string) *Trap
- func (t Trapper) TimerStop(tags ...string) *Trap
- func (t Trapper) Until(tags ...string) *Trap
- type Waiter
Constants ¶
This section is empty.
Variables ¶
var ErrTrapClosed = errors.New("trap closed")
Functions ¶
This section is empty.
Types ¶
type AdvanceWaiter ¶
type AdvanceWaiter struct {
// contains filtered or unexported fields
}
AdvanceWaiter is returned from Advance and Set calls and allows you to wait for ticks and timers to complete. In the case of functions passed to AfterFunc or TickerFunc, it waits for the functions to return. For other ticks & timers, it just waits for the tick to be delivered to the channel.
If multiple timers or tickers trigger simultaneously, they are all run on separate go routines.
func (AdvanceWaiter) Done ¶
func (w AdvanceWaiter) Done() <-chan struct{}
Done returns a channel that is closed when all timers and ticks complete.
func (AdvanceWaiter) MustWait ¶
func (w AdvanceWaiter) MustWait(ctx context.Context)
MustWait waits for all timers and ticks to complete, and fails the test immediately if the context completes first. MustWait must be called from the goroutine running the test or benchmark, similar to `t.FailNow()`.
type Call ¶
type Call struct {
Time time.Time
Duration time.Duration
Tags []string
// contains filtered or unexported fields
}
Call represents an apiCall that has been trapped.
func (*Call) MustRelease ¶ added in v0.2.0
MustRelease releases the call and waits for it to complete. If the provided context expires before the call completes, it fails the test.
IMPORTANT: If a call is trapped by more than one trap, they all must release the call before it can complete, and they must do so from different goroutines.
func (*Call) Release ¶
Release the call and wait for it to complete. If the provided context expires before the call completes, it returns an error.
IMPORTANT: If a call is trapped by more than one trap, they all must release the call before it can complete, and they must do so from different goroutines.
type Clock ¶
type Clock interface {
// NewTicker returns a new Ticker containing a channel that will send the current time on the
// channel after each tick. The period of the ticks is specified by the duration argument. The
// ticker will adjust the time interval or drop ticks to make up for slow receivers. The
// duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to
// release associated resources.
NewTicker(d time.Duration, tags ...string) *Ticker
// TickerFunc is a convenience function that calls f on the interval d until either the given
// context expires or f returns an error. Callers may call Wait() on the returned Waiter to
// wait until this happens and obtain the error. The duration d must be greater than zero; if
// not, TickerFunc will panic.
TickerFunc(ctx context.Context, d time.Duration, f func() error, tags ...string) Waiter
// NewTimer creates a new Timer that will send the current time on its channel after at least
// duration d.
NewTimer(d time.Duration, tags ...string) *Timer
// AfterFunc waits for the duration to elapse and then calls f in its own goroutine. It returns
// a Timer that can be used to cancel the call using its Stop method. The returned Timer's C
// field is not used and will be nil.
AfterFunc(d time.Duration, f func(), tags ...string) *Timer
// Now returns the current local time.
Now(tags ...string) time.Time
// Since returns the time elapsed since t. It is shorthand for Clock.Now().Sub(t).
Since(t time.Time, tags ...string) time.Duration
// Until returns the duration until t. It is shorthand for t.Sub(Clock.Now()).
Until(t time.Time, tags ...string) time.Duration
}
type Logger ¶ added in v0.3.0
var NoOpLogger Logger = noOpLogger{}
NoOpLogger is a Logger that discards all log messages.
type Mock ¶
type Mock struct {
// contains filtered or unexported fields
}
Mock is the testing implementation of Clock. It tracks a time that monotonically increases during a test, triggering any timers or tickers automatically.
func NewMock ¶
NewMock creates a new Mock with the time set to midnight UTC on Jan 1, 2024. You may re-set the time earlier than this, but only before timers or tickers are created.
func (*Mock) Advance ¶
func (m *Mock) Advance(d time.Duration) AdvanceWaiter
Advance moves the clock forward by d, triggering any timers or tickers. The returned value can be used to wait for all timers and ticks to complete. Advance sets the clock forward before returning, and can only advance up to the next timer or tick event. It will fail the test if you attempt to advance beyond.
If you need to advance exactly to the next event, and don't know or don't wish to calculate it, consider AdvanceNext().
func (*Mock) AdvanceNext ¶
func (m *Mock) AdvanceNext() (time.Duration, AdvanceWaiter)
AdvanceNext advances the clock to the next timer or tick event. It fails the test if there are none scheduled. It returns the duration the clock was advanced and a waiter that can be used to wait for the timer/tick event(s) to finish.
func (*Mock) NewTicker ¶
NewTicker creates a mocked ticker attached to this Mock. Note that it will cease sending ticks on its channel at the end of the test, to avoid leaking any goroutines. Ticks are suppressed even if the mock clock is advanced after the test completes. Best practice is to only manipulate the mock time in the main goroutine of the test.
func (*Mock) Peek ¶
Peek returns the duration until the next ticker or timer event and the value true, or, if there are no running tickers or timers, it returns zero and false.
func (*Mock) Set ¶
func (m *Mock) Set(t time.Time) AdvanceWaiter
Set the time to t. If the time is after the current mocked time, then this is equivalent to Advance() with the difference. You may only Set the time earlier than the current time before starting tickers and timers (e.g. at the start of your test case).
func (*Mock) TickerFunc ¶
func (*Mock) WithLogger ¶ added in v0.3.0
WithLogger replaces the default testing logger with a custom one.
This can be used to discard log messages with:
quartz.NewMock(t).WithLogger(quartz.NoOpLogger)
type Ticker ¶
A Ticker holds a channel that delivers “ticks” of a clock at intervals.
type Timer ¶
The Timer type represents a single event. When the Timer expires, the current time will be sent on C, unless the Timer was created by AfterFunc. A Timer must be created with NewTimer or AfterFunc.
func (*Timer) Reset ¶
Reset changes the timer to expire after duration d. It returns true if the timer had been active, false if the timer had expired or been stopped.
See https://pkg.go.dev/time#Timer.Reset for more information.
func (*Timer) Stop ¶
Stop prevents the Timer from firing. It returns true if the call stops the timer, false if the timer has already expired or been stopped. Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.
See https://pkg.go.dev/time#Timer.Stop for more information.
type Trap ¶
type Trap struct {
// contains filtered or unexported fields
}
type Trapper ¶
type Trapper struct {
// contains filtered or unexported fields
}
Trapper allows the creation of Traps