clock

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2020 License: MIT Imports: 5 Imported by: 1

README

:timer_clock: Clock

Release Go Report Card Go Doc
Change Log Go Version MIT License

clock 中的 realClocksimulator 结构体,都实现了 clock.Clock 接口。需要实际时间时,使用前者;需要人为地操纵时间的场合(比如:测试)中,使用后者。

总体思路

timecontext 标准库中,有一些函数和方法依赖于系统的当前时间。Clock 接口就是由这些内容组成。

clock 模块分为真实与虚拟的两个部分。真实的部分是以 time 和 context 标准库为基础,封装成了 Clock 接口。 虚拟部分也实现了 Clock 接口,只是这一部分可以人为的操控时间的改变。

安装与更新

在命令行中输入以下内容,可以获取到最新版

go get -u github.com/jujili/clock

真实的 Clock

c := clock.NewRealClock()

// 输出操作系统的当前时间
fmt.Println(c.Now())

真实的 Clock 就是把 timecontext 标准库中的相关函数,封装成了 realClock 的方法。

模拟的 Clock

now,_ := time.Parse("06-01-02", "20-01-26")
c := clock.NewSimulator(now)

// 输出: 2020-01-26 00:00:00 +0000 UTC
fmt.Println(c.Now())

*SimulatorcontextSim 虽然是模拟的。但是,实现了与 timecontext 标准库中同名函数一样的行为

更新 mock

在 clock 目录下,输入

$ mockgen -source=./interface.go -destination=./clock_mock_test.go -package=clock
$

Documentation

Overview

Package clock 可以模拟 time 和 context 标准库的部分行为。

All methods are safe for concurrent use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(ctx context.Context, d time.Duration) <-chan time.Time

After is a convenience wrapper for Get(ctx).After.

func ContextWithDeadline

func ContextWithDeadline(ctx context.Context, d time.Time) (context.Context, context.CancelFunc)

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.

func Now

func Now(ctx context.Context) time.Time

Now is a convenience wrapper for Get(ctx).Now.

func Set

func Set(ctx context.Context, c Clock) context.Context

Set 把 Clock 放入 ctx 中 这样的话,*Simulator 可以通过 Context 传递。

func Since

func Since(ctx context.Context, t time.Time) time.Duration

Since is a convenience wrapper for Get(ctx).Since.

func Sleep

func Sleep(ctx context.Context, d time.Duration)

Sleep is a convenience wrapper for Get(ctx).Sleep.

func Tick

func Tick(ctx context.Context, d time.Duration) <-chan time.Time

Tick is a convenience wrapper for Get(ctx).Tick.

func Until

func Until(ctx context.Context, t time.Time) time.Duration

Until is a convenience wrapper for Get(ctx).Until.

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 进行的封装 就是需要在时间轴上进行跳转那些部分。

func Get

func Get(ctx context.Context) Clock

Get 取出 ctx 中的 Clock 为了简化从 Context 中取出 Clock 后的相关操作, 对相关的操作进行了封装。

func NewRealClock

func NewRealClock() Clock

NewRealClock 返回标准库中真实时间的时钟。 并实现了 Clock 接口

type Simulator

type Simulator struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Simulator 实现了 Clock 接口,并提供了 .Add*,.Set* 和 .Move 方法驱动时钟运行

为了尽可能真实地模拟时间的流逝,Simulator.now 只会不断变大,不会出现逆转情况。

RWMutex 锁住 Simulator 时,其他 goroutine 的 Simulator 方法会被阻塞。 Simulator 的运行也不适均匀的,有可能下一个时刻就是很久以后。 这是与 time 标准库的主要差异,使用 Simulator 时,请特别注意。

func NewSimulator

func NewSimulator(now time.Time) *Simulator

NewSimulator 返回一个以 now 为当前时间的虚拟时钟。

func (*Simulator) Add

func (s *Simulator) Add(d time.Duration) time.Time

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

func (s *Simulator) AddOrPanic(d time.Duration) time.Time

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

func (s *Simulator) After(d time.Duration) <-chan time.Time

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

func (s *Simulator) AfterFunc(d time.Duration, f func()) *Timer

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

func (s *Simulator) EveryDay(hour, minute, second int) <-chan time.Time

EveryDay returns a channel which output a time.Time by your setting

func (*Simulator) Move

func (s *Simulator) Move() (time.Time, time.Duration)

Move advances the current time to the next available timer deadline Returns the new current time and the advanced duration.

func (*Simulator) NewTicker

func (s *Simulator) NewTicker(d time.Duration) *Ticker

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

func (s *Simulator) NewTimer(d time.Duration) *Timer

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) Now

func (s *Simulator) Now() time.Time

Now returns the current time.

func (*Simulator) Set

func (s *Simulator) Set(t time.Time) time.Duration

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

func (s *Simulator) SetOrPanic(t time.Time) time.Duration

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) Since

func (s *Simulator) Since(t time.Time) time.Duration

Since returns the time elapsed since t.

func (*Simulator) Sleep

func (s *Simulator) Sleep(d time.Duration)

Sleep pauses the current goroutine for at least the duration d.

A negative or zero duration causes Sleep to return immediately.

func (*Simulator) Tick

func (s *Simulator) Tick(d time.Duration) <-chan time.Time

Tick is a convenience wrapper for NewTicker providing access to the ticking channel only.

func (*Simulator) Until

func (s *Simulator) Until(t time.Time) time.Duration

Until returns the duration until t.

type Ticker

type Ticker struct {
	C    <-chan time.Time
	Stop func()
	// contains filtered or unexported fields
}

Ticker represents a time.Ticker.

func NewTicker

func NewTicker(ctx context.Context, d time.Duration) *Ticker

NewTicker is a convenience wrapper for Get(ctx).NewTicker.

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.

func AfterFunc

func AfterFunc(ctx context.Context, d time.Duration, f func()) *Timer

AfterFunc is a convenience wrapper for Get(ctx).AfterFunc.

func NewTimer

func NewTimer(ctx context.Context, d time.Duration) *Timer

NewTimer is a convenience wrapper for Get(ctx).NewTimer.

Jump to

Keyboard shortcuts

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