mock

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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

func (m *Mock) Add(duration time.Duration)

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

func (m *Mock) After(d time.Duration) <-chan time.Time

After waits for the duration to elapse and then sends the current time on the returned channel.

Example
var ( // Create a new mock clock.
	clock = mock.NewMock()
	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

func (m *Mock) AfterFunc(duration time.Duration, f func()) pkg.Timer

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
var (
	// Create a new mock clock.
	clock = mock.NewMock()
	count counter
)

count.incr()

// Execute a function after 10 mock seconds.
clock.AfterFunc(
	10*time.Second, func() {
		count.incr()
	},
)
internal.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) Now

func (m *Mock) Now() time.Time

Now returns the current wall time on the mock clock.

func (*Mock) Set

func (m *Mock) Set(time time.Time)

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) Since

func (m *Mock) Since(t time.Time) time.Duration

Since returns time since `t` using the mock clock's wall time.

func (*Mock) Sleep

func (m *Mock) Sleep(d time.Duration)

Sleep pauses the goroutine for the given duration on the mock clock. The clock must be moved forward in a separate goroutine.

Example
var (
	// Create a new mock clock.
	clock = mock.NewMock()
	count counter
)

// Execute a function after 10 mock seconds.
go func() {
	clock.Sleep(10 * time.Second)
	count.incr()
}()

internal.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

func (m *Mock) Tick(d time.Duration) <-chan time.Time

Tick is a convenience function for Ticker(). It will return a ticker channel that cannot be stopped.

func (*Mock) Ticker

func (m *Mock) Ticker(duration time.Duration) pkg.Ticker

Ticker creates a new instance of Ticker.

Example
var (
	// Create a new mock clock.
	clock = mock.NewMock()
	count counter
)

ready := make(chan struct{})
// Increment count every mock second.
go func() {
	ticker := clock.Ticker(1 * time.Second)

	close(ready)

	for {
		<-ticker.Chan()
		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

func (m *Mock) Timer(duration time.Duration) pkg.Timer

Timer creates a new instance of Timer.

Example
var ( // Create a new mock clock.
	clock = mock.NewMock()
	count counter
)

ready := make(chan struct{})
// Increment count after a mock second.
go func() {
	timer := clock.Timer(1 * time.Second)

	close(ready)

	<-timer.Chan()
	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) Until

func (m *Mock) Until(t time.Time) time.Duration

Until returns time until `t` using the mock clock's wall time.

func (*Mock) WaitForAllTimers

func (m *Mock) WaitForAllTimers() time.Time

WaitForAllTimers sets the clock until all timers are expired

func (*Mock) WithDeadline

func (m *Mock) WithDeadline(parent context.Context, deadline time.Time) (context.Context, context.CancelFunc)

func (*Mock) WithTimeout

func (m *Mock) WithTimeout(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

type Ticker

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

Ticker holds a channel that receives "ticks" at regular intervals.

func NewTicker

func NewTicker(c chan time.Time, m *Mock, duration time.Duration) *Ticker

func (*Ticker) Chan

func (t *Ticker) Chan() <-chan time.Time

func (*Ticker) Next

func (t *Ticker) Next() time.Time

func (*Ticker) Reset

func (t *Ticker) Reset(duration time.Duration)

Reset resets the ticker to a new duration.

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop turns off the ticker.

func (*Ticker) Tick

func (t *Ticker) Tick(now time.Time)

type Timer

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

Timer represents a single event. The current time will be sent on C, unless the timer was created by AfterFunc.

func NewTimer

func NewTimer(c chan time.Time, f func(), m *Mock, d time.Duration) *Timer

func (*Timer) Chan

func (t *Timer) Chan() <-chan time.Time

func (*Timer) Next

func (t *Timer) Next() time.Time

func (*Timer) Reset

func (t *Timer) Reset(duration time.Duration) bool

Reset changes the expiry time of the timer

func (*Timer) Stop

func (t *Timer) Stop() bool

Stop turns off the ticker.

func (*Timer) Tick

func (t *Timer) Tick(now time.Time)

Jump to

Keyboard shortcuts

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