Documentation ¶
Index ¶
- type Clock
- type Duration
- type Mock
- func (m *Mock) Add(d time.Duration)
- func (m *Mock) After(d time.Duration) <-chan time.Time
- func (m *Mock) AfterFunc(d time.Duration, f func()) *Timer
- func (m *Mock) Now() time.Time
- func (m *Mock) Set(t time.Time)
- func (m *Mock) Since(t time.Time) time.Duration
- func (m *Mock) Sleep(d time.Duration)
- func (m *Mock) Tick(d time.Duration) <-chan time.Time
- func (m *Mock) Ticker(d time.Duration) *Ticker
- func (m *Mock) Timer(d time.Duration) *Timer
- func (m *Mock) Until(t time.Time) time.Duration
- func (m *Mock) WaitForAllTimers() time.Time
- func (m *Mock) WithDeadline(parent context.Context, deadline time.Time) (context.Context, context.CancelFunc)
- func (m *Mock) WithTimeout(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
- type Ticker
- type Timer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock interface { After(d time.Duration) <-chan time.Time AfterFunc(d time.Duration, f func()) *Timer Now() time.Time Since(t time.Time) time.Duration Until(t time.Time) time.Duration Sleep(d time.Duration) Tick(d time.Duration) <-chan time.Time Ticker(d time.Duration) *Ticker Timer(d time.Duration) *Timer WithDeadline(parent context.Context, d time.Time) (context.Context, context.CancelFunc) WithTimeout(parent context.Context, t time.Duration) (context.Context, context.CancelFunc) }
Clock represents an interface to the functions in the standard library time package. Two implementations are available in the clock package. The first is a real-time clock which simply wraps the time package's functions. The second is a mock clock which will only change when programmatically adjusted.
type Mock ¶
type Mock struct {
// contains filtered or unexported fields
}
Mock represents a mock clock that only moves forward programmically. It can be preferable to a real-time clock when testing time-based functionality.
func NewMock ¶
func NewMock() *Mock
NewMock returns an instance of a mock clock. The current time of the mock clock on initialization is the Unix epoch.
func (*Mock) Add ¶
Add moves the current time of the mock clock forward by the specified duration. This should only be called from a single goroutine at a time.
func (*Mock) After ¶
After waits for the duration to elapse and then sends the current time on the returned channel.
Example ¶
// Create a new mock clock. clock := NewMock() var count counter ready := make(chan struct{}) // Create a channel to execute after 10 mock seconds. go func() { ch := clock.After(10 * time.Second) close(ready) <-ch count.incr() }() <-ready // Print the starting value. fmt.Printf("%s: %d\n", clock.Now().UTC(), count.get()) // Move the clock forward 5 seconds and print the value again. clock.Add(5 * time.Second) fmt.Printf("%s: %d\n", clock.Now().UTC(), count.get()) // Move the clock forward 5 seconds to the tick time and check the value. clock.Add(5 * time.Second) fmt.Printf("%s: %d\n", clock.Now().UTC(), count.get())
Output: 1970-01-01 00:00:00 +0000 UTC: 0 1970-01-01 00:00:05 +0000 UTC: 0 1970-01-01 00:00:10 +0000 UTC: 1
func (*Mock) AfterFunc ¶
AfterFunc waits for the duration to elapse and then executes a function in its own goroutine. A Timer is returned that can be stopped.
Example ¶
// Create a new mock clock. clock := NewMock() var count counter count.incr() // Execute a function after 10 mock seconds. clock.AfterFunc(10*time.Second, func() { count.incr() }) gosched() // Print the starting value. fmt.Printf("%s: %d\n", clock.Now().UTC(), count.get()) // Move the clock forward 10 seconds and print the new value. clock.Add(10 * time.Second) fmt.Printf("%s: %d\n", clock.Now().UTC(), count.get())
Output: 1970-01-01 00:00:00 +0000 UTC: 1 1970-01-01 00:00:10 +0000 UTC: 2
func (*Mock) Set ¶
Set sets the current time of the mock clock to a specific one. This should only be called from a single goroutine at a time.
func (*Mock) Sleep ¶
Sleep pauses the goroutine for the given duration on the mock clock. The clock must be moved forward in a separate goroutine.
Example ¶
// Create a new mock clock. clock := NewMock() var count counter // Execute a function after 10 mock seconds. go func() { clock.Sleep(10 * time.Second) count.incr() }() gosched() // Print the starting value. fmt.Printf("%s: %d\n", clock.Now().UTC(), count.get()) // Move the clock forward 10 seconds and print the new value. clock.Add(10 * time.Second) fmt.Printf("%s: %d\n", clock.Now().UTC(), count.get())
Output: 1970-01-01 00:00:00 +0000 UTC: 0 1970-01-01 00:00:10 +0000 UTC: 1
func (*Mock) Tick ¶
Tick is a convenience function for Ticker(). It will return a ticker channel that cannot be stopped.
func (*Mock) Ticker ¶
Ticker creates a new instance of Ticker.
Example ¶
// Create a new mock clock. clock := NewMock() var count counter ready := make(chan struct{}) // Increment count every mock second. go func() { ticker := clock.Ticker(1 * time.Second) close(ready) for { <-ticker.C count.incr() } }() <-ready // Move the clock forward 10 seconds and print the new value. clock.Add(10 * time.Second) fmt.Printf("Count is %d after 10 seconds\n", count.get()) // Move the clock forward 5 more seconds and print the new value. clock.Add(5 * time.Second) fmt.Printf("Count is %d after 15 seconds\n", count.get())
Output: Count is 10 after 10 seconds Count is 15 after 15 seconds
func (*Mock) Timer ¶
Timer creates a new instance of Timer.
Example ¶
// Create a new mock clock. clock := NewMock() var count counter ready := make(chan struct{}) // Increment count after a mock second. go func() { timer := clock.Timer(1 * time.Second) close(ready) <-timer.C count.incr() }() <-ready // Move the clock forward 10 seconds and print the new value. clock.Add(10 * time.Second) fmt.Printf("Count is %d after 10 seconds\n", count.get())
Output: Count is 1 after 10 seconds
func (*Mock) WaitForAllTimers ¶ added in v1.3.4
WaitForAllTimers sets the clock until all timers are expired
func (*Mock) WithDeadline ¶ added in v1.3.0
type Ticker ¶
Ticker holds a channel that receives "ticks" at regular intervals.