testtime

package
v0.0.0-...-d8279b7 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package testtime provides a mocked version of time.Timer for use in tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TestTimer

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

TestTimer is a mocked version of time.Timer for which the passage of time or the direct expiration of the timer is controlled manually.

TestTimer implements timeutil.Timer.

TestTimer also provides methods to introspect whether the timer is active or how many times it has fired.

func AfterFunc

func AfterFunc(d time.Duration, f func()) *TestTimer

AfterFunc waits for the timer to fire 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 returns a TestTimer which simulates the behavior of a timer which was created via time.AfterFunc.

See here for more details: https://pkg.go.dev/time#AfterFunc

func NewTimer

func NewTimer(d time.Duration) *TestTimer

NewTimer creates a new Timer that will send the current time on its channel after the timer fires.

NewTimer returns a TestTimer which simulates the behavior of a timer which was created via time.NewTimer.

See here for more details: https://pkg.go.dev/time#NewTimer

func (*TestTimer) Active

func (t *TestTimer) Active() bool

Active returns true if the timer is active, false if the timer has expired or been stopped.

func (*TestTimer) Elapse

func (t *TestTimer) Elapse(duration time.Duration)

Elapse simulates time advancing by the given duration, which potentially causes the timer to fire.

The timer will fire if the total elapsed time since the timer was created or reset is greater than the timer's duration and the timer has not yet fired.

func (*TestTimer) ExpiredC

func (t *TestTimer) ExpiredC() <-chan time.Time

ExpiredC returns the underlying C channel of the timer.

func (*TestTimer) Fire

func (t *TestTimer) Fire(currTime time.Time)

Fire causes the timer to fire. If the timer was created via NewTimer, then sends the given current time over the C channel.

To avoid accidental misuse, panics if the timer is not active (if it has already fired or been stopped).

func (*TestTimer) FireCount

func (t *TestTimer) FireCount() int

FireCount returns the number of times the timer has fired.

func (*TestTimer) Reset

func (t *TestTimer) Reset(d time.Duration) bool

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.

As the test timer does not actually count down, Reset sets the timer's elapsed time to 0 and set its duration to the given duration. The elapsed time must be advanced manually using Elapse.

This simulates the behavior of Timer.Reset() from the time package. See here fore more details: https://pkg.go.dev/time#Timer.Reset

func (*TestTimer) Stop

func (t *TestTimer) Stop() bool

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.

This simulates the behavior of Timer.Stop() from the time package. See here for more details: https://pkg.go.dev/time#Timer.Stop

Jump to

Keyboard shortcuts

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