glock

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2018 License: MIT Imports: 4 Imported by: 20

README

Glock

GoDoc Build Status Maintainability Test Coverage

Small go library for mocking parts of the time package.

Example

The package contains a Clock and Ticker interface which wrap the time.Now, time.After, adnd time.Sleep functions and the Ticker struct, respectively.

A real clock can be created for general (non-test) use. This implementation simply falls back to the functions provided in the time package.

clock := glock.NewRealClock()
clock.Now()                       // calls time.Now
clock.After(time.Second)          // calls time.After(time.Second)

t := clock.NewTicker(time.Second) // wraps time.NewTicker(time.Second)
t.Chan()                          // returns ticker's C field
t.Stop()                          // stops the ticker

In order to make unit tests that depend on time deterministic (and free of sleep calls), a mock clock can be used in place of the real clock. The mock clock allows you to control the current time with SetCurrent and Advance methods.

clock := glock.NewMockClock()

clock.Now() // returns time of creation
clock.Now() // returns time of creation
clock.SetCurrent(time.Unix(603288000, 0))
clock.Now() // returns Feb 12, 1989
clock.Advance(time.Day)
clock.Now() // returns Feb 13, 1989

The Advance method will also trigger a value on the channels created by the After and Ticker functions.

clock := glock.NewMockClockAt(time.Unix(603288000, 0))

c1 := clock.After(time.Second)
c2 := clock.After(time.Minute)
clock.GetAfterArgs()            // returns {time.Second, time.Minute}
clock.GetAfterArgs()            // returns {}
clock.Advance(time.Second * 30) // Fires c2
clock.Advance(time.Second * 30) // Fires c1
clock := glock.NewMockClock()
ticker := clock.NewTicker(time.Minute)

ch := ticker.Chan()
clock.Advance(time.Second * 30)
clock.Advance(time.Second * 30) // Fires ch
clock.Advance(time.Second * 30)
clock.Advance(time.Second * 30) // Fires ch

License

Copyright (c) 2017 Eric Fritz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

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

	// After returns a channel which receives the current time after
	// the given duration elapses.
	After(duration time.Duration) <-chan time.Time

	// Sleep blocks until the given duration elapses.
	Sleep(duration time.Duration)

	// Since returns the time elapsed since t.
	Since(t time.Time) time.Duration

	// Until returns the duration until t.
	Until(t time.Time) time.Duration

	// NewTicker will construct a ticker which will continually fire,
	// pausing for the given duration in between invocations.
	NewTicker(duration time.Duration) Ticker
}

Clock is a wrapper around common functions in the time package. This interface is designed to allow easy mocking of time functions.

func NewRealClock

func NewRealClock() Clock

NewRealClock returns a Clock whose implementation falls back to the methods available in the time package.

type MockClock

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

MockClock is an implementation of Clock that can be moved forward in time in increments for testing code that relies on timeouts or other time-sensitive constructs.

func NewMockClock

func NewMockClock() *MockClock

NewMockClock creates a new MockClock with the internal time set to time.Now()

func NewMockClockAt

func NewMockClockAt(now time.Time) *MockClock

NewMockClockAt creates a new MockClick with the internal time set to the provided time.

func (*MockClock) Advance

func (mc *MockClock) Advance(duration time.Duration)

Advance will advance the internal MockClock time by the supplied time.

func (*MockClock) After

func (mc *MockClock) After(duration time.Duration) <-chan time.Time

After returns a channel that will be sent the current internal MockClock time once the MockClock's internal time is at or past the provided duration

func (*MockClock) BlockedOnAfter

func (mc *MockClock) BlockedOnAfter() int

BlockedOnAfter returns the number of calls to After that are blocked waiting for a call to Advance to trigger them.

func (*MockClock) BlockingAdvance

func (mc *MockClock) BlockingAdvance(duration time.Duration)

BlockingAdvance will call Advance but only after there is another routine which is blocking on the channel result of a call to After.

func (*MockClock) GetAfterArgs

func (mc *MockClock) GetAfterArgs() []time.Duration

GetAfterArgs returns the duration of each call to After in the same order as they were called. The list is cleared each time GetAfterArgs is called.

func (*MockClock) GetTickerArgs

func (mc *MockClock) GetTickerArgs() []time.Duration

GetTickerArgs returns the duration of each call to create a new ticker in the same order as they were called. The list is cleared each time GetTickerArgs is called.

func (*MockClock) NewTicker

func (mc *MockClock) NewTicker(duration time.Duration) Ticker

NewTicker creates a new Ticker tied to the internal MockClock time that ticks at intervals similar to time.NewTicker(). It will also skip or drop ticks for slow readers similar to time.NewTicker() as well.

func (*MockClock) Now

func (mc *MockClock) Now() time.Time

Now returns the current time internal to the MockClock

func (*MockClock) SetCurrent

func (mc *MockClock) SetCurrent(current time.Time)

SetCurrent sets the internal MockClock time to the supplied time.

func (*MockClock) Since

func (mc *MockClock) Since(t time.Time) time.Duration

Since returns the time elapsed since t.

func (*MockClock) Sleep

func (mc *MockClock) Sleep(duration time.Duration)

Sleep will block until the internal MockClock time is at or past the provided duration

func (*MockClock) Until

func (mc *MockClock) Until(t time.Time) time.Duration

Until returns the duration until t.

type Ticker

type Ticker interface {
	// Chan returns the underlying ticker channel.
	Chan() <-chan time.Time

	// Stop stops the ticker.
	Stop()
}

Ticker is a wrapper around a time.Ticker, which allows interface access to the underlying channel (instead of bare access like the time.Ticker struct allows).

Jump to

Keyboard shortcuts

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