-
Notifications
You must be signed in to change notification settings - Fork 8
/
realtime.go
95 lines (76 loc) · 2.42 KB
/
realtime.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package abtime
import (
"context"
"time"
)
// NewRealTime returns a AbTime-conforming object that backs to the
// standard time module.
func NewRealTime() RealTime {
return RealTime{}
}
// TimerWrap wraps a Timer-conforming wrapper around a *time.Timer.
type TimerWrap struct {
T *time.Timer
}
// Channel returns the channel the *time.Timer will signal on.
func (tw TimerWrap) Channel() <-chan time.Time {
return tw.T.C
}
// Stop wraps the *time.Timer.Stop().
func (tw TimerWrap) Stop() bool {
return tw.T.Stop()
}
// Reset wraps the *time.Timer.Reset().
func (tw TimerWrap) Reset(d time.Duration) bool {
return tw.T.Reset(d)
}
// The RealTime object implements the direct calls to the time module.
type RealTime struct{}
// Now wraps time.Now.
func (rt RealTime) Now() time.Time {
return time.Now()
}
// After wraps time.After.
func (rt RealTime) After(d time.Duration, token int) <-chan time.Time {
return time.After(d)
}
// Sleep wraps time.Sleep.
func (rt RealTime) Sleep(d time.Duration, token int) {
time.Sleep(d)
}
// Tick wraps time.Tick.
func (rt RealTime) Tick(d time.Duration, token int) <-chan time.Time {
return time.Tick(d) // nolint: megacheck
}
// NewTicker wraps time.NewTicker. It returns something conforming to the
// abtime.Ticker interface.
func (rt RealTime) NewTicker(d time.Duration, token int) Ticker {
return tickerWrapper{time.NewTicker(d)}
}
// AfterFunc wraps time.AfterFunc. It returns something conforming to the
// abtime.Timer interface.
func (rt RealTime) AfterFunc(d time.Duration, f func(), token int) Timer {
return TimerWrap{time.AfterFunc(d, f)}
}
// NewTimer wraps time.NewTimer. It returns something conforming to the
// abtime.Timer interface.
func (rt RealTime) NewTimer(d time.Duration, token int) Timer {
return TimerWrap{time.NewTimer(d)}
}
type tickerWrapper struct {
*time.Ticker
}
func (tw tickerWrapper) Channel() <-chan time.Time {
return tw.C
}
func (tw tickerWrapper) Reset(d time.Duration) {
tw.Ticker.Reset(d)
}
// WithDeadline wraps context's normal WithDeadline invocation.
func (rt RealTime) WithDeadline(parent context.Context, deadline time.Time, _ int) (context.Context, context.CancelFunc) {
return context.WithDeadline(parent, deadline)
}
// WithTimeout wraps context's normal WithTimeout invocation.
func (rt RealTime) WithTimeout(parent context.Context, timeout time.Duration, _ int) (context.Context, context.CancelFunc) {
return context.WithTimeout(parent, timeout)
}