mockable

package module
v0.0.0-...-106941e Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: Apache-2.0 Imports: 2 Imported by: 7

README

mockable

The mockable interfaces for Go programming language.

Nower

A mockable interface of time.Now().

Timer

A mockable interface equivalent to time.Timer.

Clock

Clock is an interface where Nower and Timer are combined.

The timer protocol sends the current time when it is expired. This means Timer itself is not sufficient to mock timer's behavior.

Clock has an implementation in this repository while Timer does not.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Nower
	Timer
}

type ClockFake

type ClockFake struct {
	sync.Mutex

	TimeCh chan time.Time

	// ResetCh can be used to synchronize to or wait for Reset calls.
	// If an instance is initialized with NewTimerFake, ResetCh is buffered with size of 1.
	ResetCh chan time.Duration
	// StopCh can be used to synchronize to or wait for Stop calls.
	// If an instance is initialized with NewTimerFake, StopCh is buffered with size of 1.
	StopCh chan struct{}
	// contains filtered or unexported fields
}

func NewClockFake

func NewClockFake(current time.Time) *ClockFake

func (*ClockFake) C

func (c *ClockFake) C() <-chan time.Time

func (*ClockFake) CloneResetArg

func (c *ClockFake) CloneResetArg() []*time.Duration

CloneResetArg clones t.ResetArg.

func (*ClockFake) ExhaustCh

func (c *ClockFake) ExhaustCh()

ExhaustCh exhausts ResetCh and StopCh.

func (*ClockFake) IsScheduled

func (c *ClockFake) IsScheduled() bool

func (*ClockFake) IsSending

func (c *ClockFake) IsSending() bool

IsSending determines t is sending a time value to TimeCh. Be cautious that there is always a race condition between channel send and status update.

func (*ClockFake) LastReset

func (c *ClockFake) LastReset() (dur time.Duration, ok bool)

LastReset peeks last element of t.ResetArg. If t is never Reset, returns false for ok.

func (*ClockFake) Now

func (c *ClockFake) Now() time.Time

Now implements Nower.

func (*ClockFake) Reset

func (c *ClockFake) Reset(d time.Duration)

func (*ClockFake) Send

func (c *ClockFake) Send() (prev time.Time)

Send sends the time advanced from the Current by the last Reset duration. If c is never reset, it behave as it is Reset with 0.

Send keeps invariants where (<-c.C()).Before(c.Now()) is always true by stepping the current time slightly forward. Taking the time from the runtime must take a few nano seconds.

func (*ClockFake) SetNow

func (c *ClockFake) SetNow(t time.Time) (prev time.Time)

func (*ClockFake) Stop

func (c *ClockFake) Stop() bool

true if it successfully stopped the timer, false if it has already expired or been stopped.

type ClockReal

type ClockReal struct {
	T *time.Timer
}

ClockReal implements Clock using a runtime timer.

func NewClockReal

func NewClockReal() *ClockReal

NewClockReal returns newly created ClockReal. This creates stopped timer unlike time.NewTimer.

func (*ClockReal) C

func (c *ClockReal) C() <-chan time.Time

func (*ClockReal) Now

func (c *ClockReal) Now() time.Time

func (*ClockReal) Reset

func (t *ClockReal) Reset(d time.Duration)

func (*ClockReal) Stop

func (c *ClockReal) Stop() bool

type Nower

type Nower interface {
	Now() time.Time
}

The Nower is a mockable interface where callers acquire current time by calling Now.

type NowerFake

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

func (*NowerFake) Now

func (n *NowerFake) Now() time.Time

func (*NowerFake) SetNow

func (n *NowerFake) SetNow(t time.Time) (prev time.Time)

type NowerReal

type NowerReal struct{}

NowerReal is an implementation of the Nower interface. It only wraps time.Now.

func (NowerReal) Now

func (_ NowerReal) Now() time.Time

Now implements Clock interface. Now returns the current local time using runtime timer.

type Timer

type Timer interface {
	// C is equivalent of timer.C
	C() <-chan time.Time
	// Stop prevents timer from firing. It returns true if it successfully stopped the timer, false if it has already expired or been stopped.
	Stop() bool
	// Reset changes the timer to expire after duration d. Call Reset only for explicitly stopped and drained timer.
	Reset(d time.Duration)
}

The Timer is a mockable interface equivalent to the time.Timer.

Unlike the time.Timer, a New function for the Timer must create it at stopped state. Also its Reset method has no return value since it is there only for backward compatibility. It will try to Stop the timer before Reset for best effort, however still, this does not prevent the race condition of timer expiration.

Use this as an unexported field and swap out in tests. In non-test env, TimerReal should suffice. in tests, use FakeTimer or other implementations.

Jump to

Keyboard shortcuts

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