-
Notifications
You must be signed in to change notification settings - Fork 0
/
intervalroutine_test.go
231 lines (212 loc) · 4.45 KB
/
intervalroutine_test.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
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
package goodroutine
import (
"errors"
"testing"
"time"
)
func TestRecover(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("Panic %v was not recovered in routine", r)
}
}()
called := make(chan bool)
f := func() error {
called <- true
panic("blah")
}
rt := NewIntervalRoutine(RunnerFunc(f), 0, 0)
rt.Start()
defer rt.Stop()
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
}
func TestTrigger(t *testing.T) {
called := make(chan bool)
f := func() error {
called <- true
return nil
}
rt := NewIntervalRoutine(RunnerFunc(f), 0, 0)
rt.Start()
defer rt.Stop()
// should be called at start
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
for i := 0; i < 100; i++ {
rt.TriggerRun()
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
}
}
func TestTriggerBlock(t *testing.T) {
called := make(chan bool)
barrier := make(chan bool)
f := func() error {
called <- true
<-barrier
return nil
}
rt := NewIntervalRoutine(RunnerFunc(f), 0, 0)
rt.Start()
defer rt.Stop()
// should be called at start
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
// here we're still in function due to barrier
// do many triggers
// they should never block
triggersDone := make(chan bool)
go func() {
for i := 0; i < 100; i++ {
rt.TriggerRun()
}
triggersDone <- true
}()
select {
case <-triggersDone:
case <-time.Tick(10 * time.Millisecond):
t.Error("triggers took too long")
}
// release barrier
close(barrier)
// only one more call should be made
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
select {
case <-called:
t.Error("function was called too many times")
case <-time.Tick(10 * time.Millisecond):
}
}
func TestStop(t *testing.T) {
called := make(chan bool)
barrier := make(chan bool)
f := func() error {
called <- true
<-barrier
return nil
}
rt := NewIntervalRoutine(RunnerFunc(f), 0, 0)
rt.Start()
// should be called at start
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
// here we're stuck in the function
// trigger another run, then stop
rt.TriggerRun()
rt.Stop()
// release barrier
close(barrier)
// no more calls should be made
// this relies on select priority, and may only fail sometimes
select {
case <-called:
t.Error("function called after stop()")
case <-time.Tick(10 * time.Millisecond):
}
}
func TestInterval(t *testing.T) {
called := make(chan bool)
f := func() error {
called <- true
return nil
}
interval := 100 * time.Millisecond
rt := NewIntervalRoutine(RunnerFunc(f), interval, 0)
rt.Start()
// should be called at start
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
// should be called at interval
for i := 0; i < 5; i++ {
time.Sleep(interval)
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
}
rt.Stop()
// no more calls should be made
select {
case <-called:
t.Error("function called after stop()")
case <-time.Tick(interval):
}
}
func TestRetryInterval(t *testing.T) {
called := make(chan bool)
f := func() error {
called <- true
return errors.New("error")
}
run := 1 * time.Second
retry := 100 * time.Millisecond
rt := NewIntervalRoutine(RunnerFunc(f), run, retry)
rt.Start()
// should be called at start
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
// should be called at exponential interval
sleep := retry
for i := 0; i < 5; i++ {
time.Sleep(sleep)
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
sleep = 2 * sleep
if retry > run {
sleep = run
}
}
// now again but no backoff
rt.RetryBackoffDisabled = true
time.Sleep(run)
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
for i := 0; i < 5; i++ {
time.Sleep(retry)
select {
case <-called:
case <-time.Tick(10 * time.Millisecond):
t.Error("function was not called")
}
}
rt.Stop()
// no more calls should be made
select {
case <-called:
t.Error("function called after stop()")
case <-time.Tick(run):
}
}