Documentation
¶
Index ¶
- type Clock
- type FixtureClock
- func (c *FixtureClock) Advance(d time.Duration)
- func (c *FixtureClock) Now() time.Time
- func (c *FixtureClock) Rewind(d time.Duration)
- func (c *FixtureClock) Set(t time.Time)
- func (c *FixtureClock) Since(t time.Time) time.Duration
- func (c *FixtureClock) Sleep(d time.Duration)
- func (c *FixtureClock) Ticker(d time.Duration) Ticker
- type SystemClock
- type Ticker
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 time elapsed since t
Since(t time.Time) time.Duration
// Sleep pauses execution for the given duration
Sleep(d time.Duration)
// Ticker creates a new ticker that ticks at the given interval
Ticker(d time.Duration) Ticker
}
Clock abstracts time operations for testability This allows tests to control time without relying on the system clock
type FixtureClock ¶
type FixtureClock struct {
// contains filtered or unexported fields
}
FixtureClock is a controllable clock for testing It allows tests to set specific times and advance time programmatically
func NewFixtureClock ¶
func NewFixtureClock(startTime time.Time) *FixtureClock
NewFixtureClock creates a fixture clock starting at the given time If zero time is provided, uses time.Now()
func (*FixtureClock) Advance ¶
func (c *FixtureClock) Advance(d time.Duration)
Advance moves the fixture clock forward by the given duration and triggers any tickers that should fire
func (*FixtureClock) Now ¶
func (c *FixtureClock) Now() time.Time
Now returns the current fixture time
func (*FixtureClock) Rewind ¶
func (c *FixtureClock) Rewind(d time.Duration)
Rewind moves the fixture clock backward by the given duration
func (*FixtureClock) Set ¶
func (c *FixtureClock) Set(t time.Time)
Set sets the fixture clock to a specific time
func (*FixtureClock) Since ¶ added in v0.5.0
func (c *FixtureClock) Since(t time.Time) time.Duration
Since returns the elapsed time since t relative to the fixture's current time
func (*FixtureClock) Sleep ¶
func (c *FixtureClock) Sleep(d time.Duration)
Sleep advances the fixture clock by the given duration without actually sleeping This simulates the passage of time deterministically in tests
type SystemClock ¶
type SystemClock struct{}
SystemClock uses the real system clock
func NewSystemClock ¶
func NewSystemClock() *SystemClock
NewSystemClock creates a clock that uses the real system time
func (*SystemClock) Since ¶ added in v0.5.0
func (c *SystemClock) Since(t time.Time) time.Duration
Since returns the time elapsed since t using time.Since
func (*SystemClock) Sleep ¶
func (c *SystemClock) Sleep(d time.Duration)
Sleep pauses for the given duration using time.Sleep
type Ticker ¶
type Ticker interface {
// Start begins calling the function periodically
// The function is called with a context that is cancelled when Stop is called
Start(fn func(context.Context)) error
// Stop stops the ticker and cancels any in-flight function calls
Stop()
}
Ticker is an interface for a ticker that periodically calls a function