clock

package module
v0.0.0-...-768e304 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2018 License: MIT Imports: 3 Imported by: 2

README

clock

Build Status GoDoc

Small timer-driven library for mocking time in Go. Clockwork drop in replacement.

Why another one time mocking library?
  • Race free
  • Full featured
  • Redesigned
Example

Suppose we have some type with time-dependent method that we wanna test. Instead of direct use time package we specify the clock field:

const incrementStateDelay = time.Hour

// myType is a type with time-dependent method that we will test.
type myType struct {
	clock clock.Clock
	state int
}

// incrementState increments myType's state with delay.
func (f *myType) incrementState() {
	f.clock.Sleep(incrementStateDelay)
	f.state++
}

Now in tests we just inject FakeClock to the tested struct. This allows us to manipulate time:

func TestExample(t *testing.T) {
	fakeClock := clock.NewFakeClock()

	// create the myType instance with fake clock.
	mt := myType{clock: fakeClock}

	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		mt.incrementState()
		wg.Done()
	}()

	// wait until incrementState is actually sleeping.
	fakeClock.BlockUntil(1)

	// assert state not changed.
	if mt.state != 0 {
		t.Fatalf("Unxepected state, expected=0 actual=%d", mt.state)
	}

	// move time forward and wait for incrementState done.
	fakeClock.Advance(incrementStateDelay)
	wg.Wait()

	// assert state incremented.
	if mt.state != 1 {
		t.Fatalf("Unxepected state, expected=1 actual=%d", mt.state)
	}
}

In production simply inject the real clock instead

mt := myType{clock: clock.NewRealClock()}
Inspired by:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

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

Clock contains various time's functions that can be mocked.

func NewRealClock

func NewRealClock() Clock

NewRealClock returns a new instance of the real clock.

type FakeClock

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

FakeClock is an internalClock's shallow wrapper. It provides special mock methods such Advance or WaitersCount.

func NewFakeClock

func NewFakeClock() FakeClock

NewFakeClock returns a new instance of the fake clock.

func NewFakeClockAt

func NewFakeClockAt(t time.Time) FakeClock

NewFakeClockAt returns a new instance of the fake clock. Specified time will be used as a current clock's time.

func (FakeClock) Advance

func (c FakeClock) Advance(d time.Duration)

Advance moves current clock's time forward. It affects all active timers/tickers/sleepers.

func (FakeClock) After

func (c FakeClock) After(d time.Duration) <-chan time.Time

After implements Clock.

func (FakeClock) AfterFunc

func (c FakeClock) AfterFunc(d time.Duration, f func()) Timer

AfterFunc implements Clock.

func (FakeClock) BlockUntil

func (c FakeClock) BlockUntil(n int)

BlockUntil waits for the specified count of active timers/tickers/sleepers.

func (FakeClock) NewTicker

func (c FakeClock) NewTicker(d time.Duration) Ticker

NewTicker implements Clock. It returns a new instance of the mock ticker.

func (FakeClock) NewTimer

func (c FakeClock) NewTimer(d time.Duration) Timer

NewTimer implements Clock. It returns a new instance of the mock timer.

func (FakeClock) Now

func (c FakeClock) Now() time.Time

Now implements Clock.

func (FakeClock) Since

func (c FakeClock) Since(t time.Time) time.Duration

Since implements Clock.

func (FakeClock) Sleep

func (c FakeClock) Sleep(d time.Duration)

Sleep implements Clock.

func (FakeClock) Tick

func (c FakeClock) Tick(d time.Duration) <-chan time.Time

Tick implements Clock.

func (FakeClock) Until

func (c FakeClock) Until(t time.Time) time.Duration

Until implements Clock.

func (FakeClock) WaitersCount

func (c FakeClock) WaitersCount() int

WaitersCount returns current active timers/tickers/sleepers count.

type Ticker

type Ticker interface {
	Chan() <-chan time.Time
	Stop()
}

Ticker is an interface that represents both real and mock tickers.

type Timer

type Timer interface {
	Chan() <-chan time.Time
	Reset(d time.Duration) bool
	Stop() bool
}

Timer is an interface that represents both real and mock timers.

Jump to

Keyboard shortcuts

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