-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtime.go
66 lines (57 loc) · 1.54 KB
/
xtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package xtime
import (
"context"
"time"
)
// TickerInterface 便于逻辑层传递Tikcer时使用,兼容系统接口与MockTicker
type TickerInterface interface {
Stop()
Reset(d time.Duration)
}
// TickerChan 获取底层ticker的chan
func TickerChan(t TickerInterface) <-chan time.Time {
if st, ok := t.(*time.Ticker); ok {
return st.C
}
return t.(*MockTicker).C
}
// TimerInterface 便于逻辑层传递Tikcer时使用,兼容系统接口与MockTimer
type TimerInterface interface {
Stop() bool
Reset(d time.Duration) bool
}
// TimerChan 获取底层timer的chan
func TimerChan(t TimerInterface) <-chan time.Time {
if st, ok := t.(*time.Timer); ok {
return st.C
}
return t.(*MockTimer).C
}
// Clock 兼容系统timer方法
type Clock interface {
After(d time.Duration) <-chan time.Time
Now() time.Time
Since(t time.Time) time.Duration
Until(t time.Time) time.Duration
Sleep(d time.Duration)
Tick(d time.Duration) <-chan time.Time
NewTicker(d time.Duration) *MockTicker
Timer(d time.Duration) *MockTimer
AfterFunc(d time.Duration, f func()) *MockTimer
WithDeadline(parent context.Context, d time.Time) (context.Context, context.CancelFunc)
WithTimeout(parent context.Context, t time.Duration) (context.Context, context.CancelFunc)
}
type Cop interface {
Freeze(t time.Time)
Travel(t time.Time)
Scale(scale float64)
Return()
Mocked() bool
ApplyOption(...Option) []Option
}
type Mock interface {
Clock
Cop
}
func NewSystemClock() Clock { return &clock{} }
func NewMock(opts ...Option) Mock { return newMock(NewOptions(opts...)) }