-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_test.go
More file actions
233 lines (179 loc) · 4.6 KB
/
timer_test.go
File metadata and controls
233 lines (179 loc) · 4.6 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package gotimer
import (
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/pkg/errors"
)
func waitFor(t *testing.T, timeout time.Duration, condition func() bool) {
t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if condition() {
return
}
time.Sleep(5 * time.Millisecond)
}
t.Fatal("condition not satisfied before timeout")
}
func TestTimerAddFuncRunsPeriodically(t *testing.T) {
timer := NewTimer()
t.Cleanup(timer.Clear)
var count atomic.Int32
jobID := timer.AddFunc(10*time.Millisecond, func() {
count.Add(1)
})
if jobID == "" {
t.Fatal("expected non-empty job id")
}
waitFor(t, 200*time.Millisecond, func() bool {
return count.Load() >= 3
})
}
func TestTimerRemoveStopsJob(t *testing.T) {
timer := NewTimer()
var count atomic.Int32
jobID := timer.AddFunc(10*time.Millisecond, func() {
count.Add(1)
})
waitFor(t, 200*time.Millisecond, func() bool {
return count.Load() >= 2
})
timer.Remove(jobID)
current := count.Load()
time.Sleep(50 * time.Millisecond)
if after := count.Load(); after != current {
t.Fatalf("expected job to stop after Remove, before=%d after=%d", current, after)
}
}
func TestTimerClearStopsAllJobsAndClearsMap(t *testing.T) {
timer := NewTimer()
var count1 atomic.Int32
var count2 atomic.Int32
timer.AddFunc(10*time.Millisecond, func() {
count1.Add(1)
})
timer.AddFunc(10*time.Millisecond, func() {
count2.Add(1)
})
waitFor(t, 200*time.Millisecond, func() bool {
return count1.Load() >= 1 && count2.Load() >= 1
})
timer.Clear()
if got := len(timer.jobs); got != 0 {
t.Fatalf("expected no jobs after Clear, got %d", got)
}
before1 := count1.Load()
before2 := count2.Load()
time.Sleep(50 * time.Millisecond)
if after1 := count1.Load(); after1 != before1 {
t.Fatalf("expected first job to stop after Clear, before=%d after=%d", before1, after1)
}
if after2 := count2.Load(); after2 != before2 {
t.Fatalf("expected second job to stop after Clear, before=%d after=%d", before2, after2)
}
}
func TestTimerRecoversFromPanic(t *testing.T) {
timer := NewTimer()
t.Cleanup(timer.Clear)
var count atomic.Int32
timer.AddFunc(10*time.Millisecond, func() {
count.Add(1)
err := fmt.Errorf("job error")
panic(errors.Wrap(err, "wrapped"))
})
waitFor(t, 200*time.Millisecond, func() bool {
return count.Load() >= 3
})
}
func TestTimerRemoveUnknownIDDoesNothing(t *testing.T) {
timer := NewTimer()
t.Cleanup(timer.Clear)
timer.AddFunc(10*time.Millisecond, func() {})
before := len(timer.jobs)
timer.Remove("not-exists")
after := len(timer.jobs)
if after != before {
t.Fatalf("expected removing unknown id to keep jobs unchanged, before=%d after=%d", before, after)
}
}
func TestTimerClearIsIdempotent(t *testing.T) {
timer := NewTimer()
timer.AddFunc(10*time.Millisecond, func() {})
timer.Clear()
timer.Clear()
if got := len(timer.jobs); got != 0 {
t.Fatalf("expected no jobs after repeated Clear, got %d", got)
}
}
func TestTimerRemoveIsIdempotent(t *testing.T) {
timer := NewTimer()
jobID := timer.AddFunc(10*time.Millisecond, func() {})
timer.Remove(jobID)
timer.Remove(jobID)
if got := len(timer.jobs); got != 0 {
t.Fatalf("expected no jobs after repeated Remove, got %d", got)
}
}
func TestTimerConcurrentAddAndRemove(t *testing.T) {
timer := NewTimer()
t.Cleanup(timer.Clear)
const workers = 10
const jobsPerWorker = 20
ids := make(chan string, workers*jobsPerWorker)
var addWG sync.WaitGroup
for i := 0; i < workers; i++ {
addWG.Add(1)
go func() {
defer addWG.Done()
for j := 0; j < jobsPerWorker; j++ {
id := timer.AddFunc(time.Hour, func() {})
ids <- id
}
}()
}
addWG.Wait()
close(ids)
if got := len(timer.jobs); got != workers*jobsPerWorker {
t.Fatalf("expected %d jobs after concurrent add, got %d", workers*jobsPerWorker, got)
}
var removeWG sync.WaitGroup
for id := range ids {
removeWG.Add(1)
go func(jobID string) {
defer removeWG.Done()
timer.Remove(jobID)
}(id)
}
removeWG.Wait()
if got := len(timer.jobs); got != 0 {
t.Fatalf("expected no jobs after concurrent remove, got %d", got)
}
}
func BenchmarkTimerAddFunc(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
timer := NewTimer()
_ = timer.AddFunc(time.Hour, func() {})
timer.Clear()
}
}
func BenchmarkTimerRemove(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
timer := NewTimer()
jobID := timer.AddFunc(time.Hour, func() {})
timer.Remove(jobID)
}
}
func BenchmarkJobDo(b *testing.B) {
job := NewJob(time.Hour)
defer job.stop()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
job.do(func() {})
}
}