-
Notifications
You must be signed in to change notification settings - Fork 5
/
clock.go
109 lines (80 loc) · 2.98 KB
/
clock.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package clock
import "time"
// Work mirrors the behaviour of Go's time package.
var Work Clock
func init() {
Work = New()
}
// Now returns the current local time.
func Now() time.Time {
return Work.Now()
}
// Sleep pauses the current goroutine for at least the duration d.
// A negative or zero duration causes Sleep to return immediately.
func Sleep(d time.Duration) {
Work.Sleep(d)
}
// After waits for the duration to elapse and then sends the current time
// on the returned channel. It is equivalent to NewTimer(d).C.
func After(d time.Duration) <-chan time.Time {
return Work.After(d)
}
// Tick is a convenience wrapper for NewTicker providing access to the
// ticking channel only.
func Tick(d time.Duration) <-chan time.Time {
return Work.Tick(d)
}
// Ticker returns a new Ticker containing a channel that will send the
// time with a period specified by the duration argument. It adjusts
// the intervals or drops ticks to make up for slow receivers.
// The duration d must be greater than zero; if not, Ticker will panic.
func Ticker(d time.Duration) *time.Ticker {
return Work.Ticker(d)
}
// Clock provides the functions from the time package.
type Clock interface {
// Now returns the current local time.
Now() time.Time
// Sleep pauses the current goroutine for at least the duration d.
// A negative or zero duration causes Sleep to return immediately.
Sleep(d time.Duration)
// After waits for the duration to elapse and then sends the current time
// on the returned channel. It is equivalent to NewTimer(d).C.
After(d time.Duration) <-chan time.Time
// Tick is a convenience wrapper for NewTicker providing access to the
// ticking channel only.
Tick(d time.Duration) <-chan time.Time
// Ticker returns a new Ticker containing a channel that will send the
// time with a period specified by the duration argument. It adjusts
// the intervals or drops ticks to make up for slow receivers.
// The duration d must be greater than zero; if not, Ticker will panic.
Ticker(d time.Duration) *time.Ticker
// TODO: At(t time.Time) <-chan time.Time
}
// Mock represents a manipulable Work. It is concurrent-friendly.
type Mock interface {
Clock
// ==== manipulate Now()
// Set applies the passed-in time to the Clock's time.
Set(t time.Time) Mock
// Add changes the Clock's time by the passed-in duration.
Add(d time.Duration) Mock
// Freeze stops the clock's time.
Freeze() Mock
// Freeze stops the clock's time at the passed-in moment.
FreezeAt(t time.Time) Mock
// IsFrozen is whether the clock's time is stopped.
IsFrozen() bool
// Unfreeze starts the clock's time again.
Unfreeze() Mock
// ==== manipulate Sleep()
// SetSleep overrides the passed-in argument to the Sleep method.
SetSleep(d time.Duration) Mock
// NoSleep disables the Sleep method.
NoSleep() Mock
// ResetSleep re-enables the default Sleep behaviour.
ResetSleep() Mock
// ==== manipulate After()
// SetAfter overrides the passed-in argument to the After method.
// SetAfter(d time.Duration) Mock
}