clock: github.com/facebookgo/clock Index | Examples | Files

package clock

import "github.com/facebookgo/clock"

Index

Examples

Package Files

clock.go

type Calls Uses

type Calls struct {
    After     uint32
    AfterFunc uint32
    Now       uint32
    Sleep     uint32
    Tick      uint32
    Ticker    uint32
    Timer     uint32
}

Calls keeps track of the count of calls for each of the methods on the Clock interface.

type Clock Uses

type Clock interface {
    After(d time.Duration) <-chan time.Time
    AfterFunc(d time.Duration, f func()) *Timer
    Now() time.Time
    Sleep(d time.Duration)
    Tick(d time.Duration) <-chan time.Time
    Ticker(d time.Duration) *Ticker
    Timer(d time.Duration) *Timer
}

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 make forward progress when programmatically adjusted.

func New Uses

func New() Clock

New returns an instance of a real-time clock.

type Mock Uses

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 Uses

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 Uses

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

Add moves the current time of the mock clock forward by the duration. This should only be called from a single goroutine at a time.

func (*Mock) After Uses

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.

Code:

// Create a new mock clock.
clock := clock.NewMock()
count := 0

// Create a channel to execute after 10 mock seconds.
go func() {
    <-clock.After(10 * time.Second)
    count = 100
}()
runtime.Gosched()

// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)

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

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

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: 100

func (*Mock) AfterFunc Uses

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

AfterFunc waits for the duration to elapse and then executes a function. A Timer is returned that can be stopped.

Code:

// Create a new mock clock.
clock := clock.NewMock()
count := 0

// Execute a function after 10 mock seconds.
clock.AfterFunc(10*time.Second, func() {
    count = 100
})
runtime.Gosched()

// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)

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

Output:

1970-01-01 00:00:00 +0000 UTC: 0
1970-01-01 00:00:10 +0000 UTC: 100

func (*Mock) Now Uses

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

Now returns the current wall time on the mock clock.

func (*Mock) Sleep Uses

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.

Code:

// Create a new mock clock.
clock := clock.NewMock()
count := 0

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

// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)

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

Output:

1970-01-01 00:00:00 +0000 UTC: 0
1970-01-01 00:00:10 +0000 UTC: 100

func (*Mock) Tick Uses

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 Uses

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

Ticker creates a new instance of Ticker.

Code:

// Create a new mock clock.
clock := clock.NewMock()
count := 0

// Increment count every mock second.
go func() {
    ticker := clock.Ticker(1 * time.Second)
    for {
        <-ticker.C
        count++
    }
}()
runtime.Gosched()

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

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

Output:

Count is 10 after 10 seconds
Count is 15 after 15 seconds

func (*Mock) Timer Uses

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

Timer creates a new instance of Timer.

Code:

// Create a new mock clock.
clock := clock.NewMock()
count := 0

// Increment count after a mock second.
go func() {
    timer := clock.Timer(1 * time.Second)
    <-timer.C
    count++
}()
runtime.Gosched()

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

Output:

Count is 1 after 10 seconds

func (*Mock) Wait Uses

func (m *Mock) Wait(s Calls)

Wait waits for at least the relevant calls before returning. The expected Calls are always over the lifetime of the Mock. Values in the Calls struct are used as the minimum number of calls, this allows you to wait for only the calls you care about.

type Ticker Uses

type Ticker struct {
    C <-chan time.Time
    // contains filtered or unexported fields
}

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

func (*Ticker) Stop Uses

func (t *Ticker) Stop()

Stop turns off the ticker.

type Timer Uses

type Timer struct {
    C <-chan time.Time
    // 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 (*Timer) Stop Uses

func (t *Timer) Stop()

Stop turns off the ticker.

Package clock imports 4 packages (graph) and is imported by 102 packages. Updated 2018-11-28. Refresh now. Tools for package owners.