-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.go
More file actions
192 lines (176 loc) · 5.61 KB
/
Copy pathtimer.go
File metadata and controls
192 lines (176 loc) · 5.61 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package clock
import (
"sync"
"time"
)
// Timer is an abstraction for the type time.Timer that can be mocked for tests.
type Timer interface {
// Chan returns the readonly channel of the ticker
Chan() <-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 does not close the channel, to prevent a read from the channel succeeding
// incorrectly.
//
// From the official docs:
// To prevent a timer created with NewTimer from firing after a call to Stop,
// check the return value and drain the channel.
// For example, assuming the program has not received from t.C already:
//
// if !t.Stop() {
// <-t.C
// }
//
// This cannot be done concurrent to other receives from the Timer's
// channel.
//
// For a timer created with AfterFunc(d, f), if t.Stop returns false, then the timer
// has already expired and the function f has been started in its own goroutine;
// Stop does not wait for f to complete before returning.
// If the caller needs to know whether f is completed, it must coordinate
// with f explicitly.
Stop() 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.
//
// From the official docs:
// Resetting a timer must take care not to race with the send into t.C
// that happens when the current timer expires.
// If a program has already received a value from t.C, the timer is known
// to have expired, and t.Reset can be used directly.
// If a program has not yet received a value from t.C, however,
// the timer must be stopped and—if Stop reports that the timer expired
// before being stopped—the channel explicitly drained:
//
// if !t.Stop() {
// <-t.C
// }
// t.Reset(d)
//
// This should not be done concurrent to other receives from the Timer's
// channel.
//
// Note that it is not possible to use Reset's return value correctly, as there
// is a race condition between draining the channel and the new timer expiring.
// Reset should always be invoked on stopped or expired channels, as described above.
// The return value exists to preserve compatibility with existing programs.
Reset(time.Duration) bool
}
// realTimer is just the type time.Timer and implements the Timer interface.
type realTimer struct {
*time.Timer
}
// Chan returns the readonly channel of the Timer.
func (r *realTimer) Chan() <-chan time.Time {
return r.C
}
// fakeTimer is an implementation of Timer that's based on the time mocking done in Mock.
type fakeTimer struct {
mu sync.RWMutex
ch chan time.Time
fn func()
due time.Time
clock *Mock
stopped bool
}
// Chan returns the readonly channel of the Timer.
func (f *fakeTimer) Chan() <-chan time.Time {
return f.ch
}
// 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 does not close the channel, to prevent a read from the channel succeeding
// incorrectly.
//
// From the official docs:
// To prevent a timer created with NewTimer from firing after a call to Stop,
// check the return value and drain the channel.
// For example, assuming the program has not received from t.C already:
//
// if !t.Stop() {
// <-t.C
// }
//
// This cannot be done concurrent to other receives from the Timer's
// channel.
//
// For a timer created with AfterFunc(d, f), if t.Stop returns false, then the timer
// has already expired and the function f has been started in its own goroutine;
// Stop does not wait for f to complete before returning.
// If the caller needs to know whether f is completed, it must coordinate
// with f explicitly.
func (f *fakeTimer) Stop() bool {
f.clock.removeTimer(f)
f.mu.Lock()
defer f.mu.Unlock()
if f.stopped {
return false
}
f.stopped = true
return true
}
// 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.
//
// From the official docs:
// Resetting a timer must take care not to race with the send into t.C
// that happens when the current timer expires.
// If a program has already received a value from t.C, the timer is known
// to have expired, and t.Reset can be used directly.
// If a program has not yet received a value from t.C, however,
// the timer must be stopped and—if Stop reports that the timer expired
// before being stopped—the channel explicitly drained:
//
// if !t.Stop() {
// <-t.C
// }
// t.Reset(d)
//
// This should not be done concurrent to other receives from the Timer's
// channel.
//
// Note that it is not possible to use Reset's return value correctly, as there
// is a race condition between draining the channel and the new timer expiring.
// Reset should always be invoked on stopped or expired channels, as described above.
// The return value exists to preserve compatibility with existing programs.
func (f *fakeTimer) Reset(d time.Duration) bool {
now := f.clock.Now()
f.mu.Lock()
f.due = now.Add(d)
if f.stopped {
f.stopped = false
f.clock.addTimer(f)
f.mu.Unlock()
return false
}
f.mu.Unlock()
return true
}
// Execute executes the Timer object
func (f *fakeTimer) Execute(t time.Time) {
f.mu.RLock()
if f.stopped {
f.mu.RUnlock()
return
}
f.mu.RUnlock()
f.mu.Lock()
if f.ch == nil {
f.fn()
} else {
f.ch <- f.due
}
f.stopped = true
f.mu.Unlock()
f.clock.removeTimer(f)
}
// NextExecution returns the next execution time
func (f *fakeTimer) NextExecution() time.Time {
f.mu.RLock()
defer f.mu.RUnlock()
return f.due
}