-
Notifications
You must be signed in to change notification settings - Fork 23
/
tasks_test.go
254 lines (224 loc) · 6.12 KB
/
tasks_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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package tasks
import (
"fmt"
"testing"
"time"
)
func TestAdd(t *testing.T) {
// Create a base scheduler to use
scheduler := New()
defer scheduler.Stop()
t.Run("Add a valid task and look it up", func(t *testing.T) {
id, err := scheduler.Add(&Task{
Interval: time.Duration(1 * time.Minute),
TaskFunc: func() error { return nil },
ErrFunc: func(e error) {},
})
if err != nil {
t.Errorf("Unexpected errors when scheduling a valid task - %s", err)
}
_, err = scheduler.Lookup(id)
if err != nil {
t.Errorf("Unable to find newly scheduled task with Lookup - %s", err)
}
tt := scheduler.Tasks()
if len(tt) < 1 {
t.Errorf("Unable to find newly scheduled task with Tasks")
}
})
t.Run("Check for nil callback", func(t *testing.T) {
_, err := scheduler.Add(&Task{
Interval: time.Duration(1 * time.Minute),
ErrFunc: func(e error) {},
})
if err == nil {
t.Errorf("Unexpected success when scheduling an invalid task - %s", err)
}
})
t.Run("Check for nil interval", func(t *testing.T) {
_, err := scheduler.Add(&Task{
TaskFunc: func() error { return nil },
ErrFunc: func(e error) {},
})
if err == nil {
t.Errorf("Unexpected success when scheduling an invalid task - %s", err)
}
})
}
func TestScheduler(t *testing.T) {
// Create a base scheduler to use
scheduler := New()
t.Run("Verify Tasks Run when Added", func(t *testing.T) {
// Channel for orchestrating when the task ran
doneCh := make(chan struct{})
// Setup A task
id, err := scheduler.Add(&Task{
Interval: time.Duration(1 * time.Second),
TaskFunc: func() error {
doneCh <- struct{}{}
return nil
},
ErrFunc: func(e error) {},
})
if err != nil {
t.Errorf("Unexpected errors when scheduling a valid task - %s", err)
}
defer scheduler.Del(id)
// Make sure it runs especially when we want it too
for i := 0; i < 6; i++ {
select {
case <-doneCh:
continue
case <-time.After(2 * time.Second):
t.Errorf("Scheduler failed to execute the scheduled tasks %d run within 2 seconds", i)
}
}
})
t.Run("Verify StartAfter works as expected", func(t *testing.T) {
// Channel for orchestrating when the task ran
doneCh := make(chan struct{})
// Create a Start time
sa := time.Now().Add(10 * time.Second)
// Setup A task
id, err := scheduler.Add(&Task{
Interval: time.Duration(1 * time.Second),
StartAfter: sa,
TaskFunc: func() error {
doneCh <- struct{}{}
return nil
},
ErrFunc: func(e error) {},
})
if err != nil {
t.Errorf("Unexpected errors when scheduling a valid task - %s", err)
}
defer scheduler.Del(id)
// Make sure it runs especially when we want it too
select {
case <-doneCh:
if time.Now().Before(sa) {
t.Errorf("Task executed before the defined start time now %s, supposed to be %s", time.Now().String(), sa.String())
}
return
case <-time.After(15 * time.Second):
t.Errorf("Scheduler failed to execute the scheduled tasks within 15 seconds")
}
})
t.Run("Verify Cancelling a StartAfter works as expected", func(t *testing.T) {
// Channel for orchestrating when the task ran
doneCh := make(chan struct{})
// Create a Start time
sa := time.Now().Add(10 * time.Second)
// Setup A task
id, err := scheduler.Add(&Task{
Interval: time.Duration(1 * time.Second),
StartAfter: sa,
TaskFunc: func() error {
doneCh <- struct{}{}
return nil
},
ErrFunc: func(e error) {},
})
if err != nil {
t.Errorf("Unexpected errors when scheduling a valid task - %s", err)
}
// Remove task before it can be scheduled
scheduler.Del(id)
// Make sure it doesn't run
select {
case <-doneCh:
t.Errorf("Task executed it was supposed to be cancelled")
return
case <-time.After(15 * time.Second):
return
}
})
t.Run("Verify Tasks Dont run when Deleted", func(t *testing.T) {
// Channel for orchestrating when the task ran
doneCh := make(chan struct{})
// Setup A task
id, err := scheduler.Add(&Task{
Interval: time.Duration(1 * time.Second),
TaskFunc: func() error {
doneCh <- struct{}{}
return nil
},
ErrFunc: func(e error) {},
})
if err != nil {
t.Errorf("Unexpected errors when scheduling a valid task - %s", err)
}
defer scheduler.Del(id)
// Make sure it runs especially when we want it too
for i := 0; i < 6; i++ {
select {
case <-doneCh:
if i == 2 {
scheduler.Del(id)
}
if i > 2 {
t.Errorf("Task should not have exceeded 2, count is %d", i)
}
continue
case <-time.After(2 * time.Second):
if i > 2 {
return
}
t.Errorf("Scheduler failed to execute the scheduled tasks %d run within 2 seconds", i)
}
}
})
t.Run("Verify RunOnce works as expected", func(t *testing.T) {
// Channel for orchestrating when the task ran
doneCh := make(chan struct{})
// Setup A task
id, err := scheduler.Add(&Task{
Interval: time.Duration(1 * time.Second),
RunOnce: true,
TaskFunc: func() error {
doneCh <- struct{}{}
return nil
},
ErrFunc: func(e error) {},
})
if err != nil {
t.Errorf("Unexpected errors when scheduling a valid task - %s", err)
}
defer scheduler.Del(id)
// Make sure it runs especially when we want it too
for i := 0; i < 6; i++ {
select {
case <-doneCh:
if i >= 1 {
t.Errorf("Task should not have exceeded 1, count is %d", i)
}
continue
case <-time.After(2 * time.Second):
if i == 1 {
return
}
t.Errorf("Scheduler failed to execute the scheduled tasks %d run within 2 seconds", i)
}
}
})
t.Run("Test ErrFunc gets called on errors", func(t *testing.T) {
// Create a channel to signal function exec
doneCh := make(chan struct{})
// Add task
_, err := scheduler.Add(&Task{
Interval: time.Duration(1 * time.Second),
TaskFunc: func() error { return fmt.Errorf("Errors are bad") },
ErrFunc: func(e error) { doneCh <- struct{}{} },
})
if err != nil {
t.Errorf("Unexpected errors when scheduling a valid task - %s", err)
}
// Wait for success, or timeout
select {
case <-doneCh:
return
case <-time.After(2 * time.Second):
t.Errorf("Error function was not called when an error occurred")
}
})
}