Documentation
¶
Overview ¶
Package clock 可以模拟 time 和 context 标准库的部分行为。
All methods are safe for concurrent use.
Index ¶
- func After(ctx context.Context, d time.Duration) <-chan time.Time
- func ContextWithDeadline(ctx context.Context, d time.Time) (context.Context, context.CancelFunc)
- func ContextWithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
- func Now(ctx context.Context) time.Time
- func Set(ctx context.Context, c Clock) context.Context
- func Since(ctx context.Context, t time.Time) time.Duration
- func Sleep(ctx context.Context, d time.Duration)
- func Tick(ctx context.Context, d time.Duration) <-chan time.Time
- func Until(ctx context.Context, t time.Time) time.Duration
- type Clock
- type Simulator
- func (s *Simulator) Add(d time.Duration) time.Time
- func (s *Simulator) AddOrPanic(d time.Duration) time.Time
- func (s *Simulator) After(d time.Duration) <-chan time.Time
- func (s *Simulator) AfterFunc(d time.Duration, f func()) *Timer
- func (s *Simulator) ContextWithDeadline(parent context.Context, deadline time.Time) (context.Context, context.CancelFunc)
- func (s *Simulator) ContextWithTimeout(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
- func (s *Simulator) EveryDay(hour, minute, second int) <-chan time.Time
- func (s *Simulator) Move() (time.Time, time.Duration)
- func (s *Simulator) NewTicker(d time.Duration) *Ticker
- func (s *Simulator) NewTimer(d time.Duration) *Timer
- func (s *Simulator) Now() time.Time
- func (s *Simulator) Set(t time.Time) time.Duration
- func (s *Simulator) SetOrPanic(t time.Time) time.Duration
- func (s *Simulator) Since(t time.Time) time.Duration
- func (s *Simulator) Sleep(d time.Duration)
- func (s *Simulator) Tick(d time.Duration) <-chan time.Time
- func (s *Simulator) Until(t time.Time) time.Duration
- type Ticker
- type Timer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithDeadline ¶
ContextWithDeadline is a convenience wrapper for Get(ctx).ContextWithDeadline.
func ContextWithTimeout ¶
func ContextWithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
ContextWithTimeout is a convenience wrapper for Get(ctx).ContextWithTimeout.
Types ¶
type Clock ¶
type Clock interface { After(d time.Duration) <-chan time.Time AfterFunc(d time.Duration, f func()) *Timer NewTicker(d time.Duration) *Ticker NewTimer(d time.Duration) *Timer Now() time.Time Since(t time.Time) time.Duration Sleep(d time.Duration) Tick(d time.Duration) <-chan time.Time Until(t time.Time) time.Duration // ContextWithDeadline 与 context.ContextWithDeadline 具有相同的功能 // 只是基于 Clock 的时间线 ContextWithDeadline(parent context.Context, d time.Time) (context.Context, context.CancelFunc) // ContextWithTimeout 是 ContextWithDeadline(parent, Now(parent).Add(timeout)). ContextWithTimeout(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) }
Clock 是对 time 和 context 标准库的部分 API 进行的封装 就是需要在时间轴上进行跳转那些部分。
type Simulator ¶
Simulator 实现了 Clock 接口,并提供了 .Add*,.Set* 和 .Move 方法驱动时钟运行
为了尽可能真实地模拟时间的流逝,Simulator.now 只会不断变大,不会出现逆转情况。
RWMutex 锁住 Simulator 时,其他 goroutine 的 Simulator 方法会被阻塞。 Simulator 的运行也不适均匀的,有可能下一个时刻就是很久以后。 这是与 time 标准库的主要差异,使用 Simulator 时,请特别注意。
func (*Simulator) Add ¶
Add advances the current time by duration d and fires all expired timers if d >= 0, else DO NOTHING
Returns the current time. 推荐使用 AddOrPanic 替换此方法
func (*Simulator) AddOrPanic ¶
AddOrPanic advances the current time by duration d and fires all expired timers if d >= 0 else panic Returns the new current time.
func (*Simulator) After ¶
After waits for the duration to elapse and then sends the current time on the returned channel.
A negative or zero duration fires the underlying timer immediately.
func (*Simulator) AfterFunc ¶
AfterFunc waits for the duration to elapse and then calls f in its own goroutine. It returns a Timer that can be used to cancel the call using its Stop method.
A negative or zero duration fires the timer immediately.
func (*Simulator) ContextWithDeadline ¶
func (s *Simulator) ContextWithDeadline(parent context.Context, deadline time.Time) (context.Context, context.CancelFunc)
ContextWithDeadline implements Clock. NOTICE: 在程序中,不要混用 realClock 和 simulator
func (*Simulator) ContextWithTimeout ¶
func (s *Simulator) ContextWithTimeout(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
ContextWithTimeout implements Clock. NOTICE: 在程序中,不要混用 realClock 和 simulator
func (*Simulator) EveryDay ¶ added in v0.10.0
EveryDay returns a channel which output a time.Time by your setting
func (*Simulator) Move ¶
Move advances the current time to the next available timer deadline Returns the new current time and the advanced duration.
func (*Simulator) NewTicker ¶
NewTicker returns a new Ticker containing a channel that will send the current time with a period specified by the duration d.
func (*Simulator) NewTimer ¶
NewTimer creates a new Timer that will send the current time on its channel after at least duration d.
A negative or zero duration fires the timer immediately.
func (*Simulator) Set ¶
Set advances the current time to t and fires all expired timers if s.now <= t else DO NOTHING Returns the advanced duration. NOTICE: 返回 0 还有可能是 t < s.now,不仅仅是 t = s.now 推荐使用 SetOrPanic 替代此方法
func (*Simulator) SetOrPanic ¶
SetOrPanic advances the current time to t and fires all expired timers if s.now <= t else panic with time reversal Returns the advanced duration.
func (*Simulator) Sleep ¶
Sleep pauses the current goroutine for at least the duration d.
A negative or zero duration causes Sleep to return immediately.
type Timer ¶
type Timer struct { C <-chan time.Time // Stop prevents the Timer from firing. // It returns true if the call stops the timer, false if the timer has already expired or been stopped. Stop func() bool // Reset changes the timer to expire after duration d. // It returns true if the timer had been active, false if the timer had expired or been stopped. // // A negative or zero duration fires the timer immediately. Reset func(d time.Duration) bool // contains filtered or unexported fields }
Timer 替代 time.Timer.