Documentation
¶
Index ¶
- type Clock
- type ClockFake
- func (c *ClockFake) C() <-chan time.Time
- func (c *ClockFake) CloneResetArg() []*time.Duration
- func (c *ClockFake) ExhaustCh()
- func (c *ClockFake) IsScheduled() bool
- func (c *ClockFake) IsSending() bool
- func (c *ClockFake) LastReset() (dur time.Duration, ok bool)
- func (c *ClockFake) Now() time.Time
- func (c *ClockFake) Reset(d time.Duration)
- func (c *ClockFake) Send() (prev time.Time)
- func (c *ClockFake) SetNow(t time.Time) (prev time.Time)
- func (c *ClockFake) Stop() bool
- type ClockReal
- type Nower
- type NowerFake
- type NowerReal
- type Timer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 (*ClockFake) CloneResetArg ¶
CloneResetArg clones t.ResetArg.
func (*ClockFake) ExhaustCh ¶
func (c *ClockFake) ExhaustCh()
ExhaustCh exhausts ResetCh and StopCh.
func (*ClockFake) IsScheduled ¶
func (*ClockFake) IsSending ¶
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 ¶
LastReset peeks last element of t.ResetArg. If t is never Reset, returns false for ok.
func (*ClockFake) Send ¶
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.
type ClockReal ¶
ClockReal implements Clock using a runtime timer.
func NewClockReal ¶
func NewClockReal() *ClockReal
NewClockReal returns newly created ClockReal. This creates stopped timer unlike time.NewTimer.
type NowerReal ¶
type NowerReal struct{}
NowerReal is an implementation of the Nower interface. It only wraps time.Now.
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.