-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_test.go
395 lines (334 loc) · 9.06 KB
/
internal_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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package smartpoll
import (
"testing"
"time"
)
func TestInternal_Next(t *testing.T) {
// Initialize a new Scheduler
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {
next: time.Now().Add(time.Minute),
},
},
}
// Initialize a new Internal
internal := &Internal{
scheduler: scheduler,
}
// Call the Next method
next := internal.Next("task1")
// Verify that the returned time is the same as the one we set in the taskState
if next != scheduler.tasks["task1"].next {
t.Errorf("Expected %v, got %v", scheduler.tasks["task1"].next, next)
}
}
func TestInternal_Running(t *testing.T) {
// Initialize a new Scheduler
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {},
},
}
// Initialize a new Internal
internal := &Internal{
scheduler: scheduler,
}
// Set the task as running
scheduler.tasks["task1"].running.Store(1)
// Call the Running method
running := internal.Running("task1")
// Verify that the returned value is true
if !running {
t.Errorf("Expected task to be running")
}
// Set the task as not running
scheduler.tasks["task1"].running.Store(0)
// Call the Running method again
running = internal.Running("task1")
// Verify that the returned value is false
if running {
t.Errorf("Expected task to not be running")
}
}
func TestInternal_StopTimer_timerDrained(t *testing.T) {
// Initialize a new Scheduler
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {
timer: startTimer(1),
},
},
}
time.Sleep(time.Millisecond * 100)
// Initialize a new Internal
internal := &Internal{
scheduler: scheduler,
}
// Call the StopTimer method
ready := internal.StopTimer("task1")
// Verify that the returned value is true
if !ready {
t.Errorf("Expected task to be ready")
}
if scheduler.tasks["task1"].timer != readySentinel {
t.Errorf("Expected task timer to be nil")
}
}
func TestInternal_Schedule(t *testing.T) {
// Initialize a new Scheduler
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {},
},
}
// Initialize a new Internal
internal := &Internal{
scheduler: scheduler,
}
// Call the Schedule method
internal.Schedule("task1", time.Minute)
// Verify that the timer for the task is not nil
if scheduler.tasks["task1"].timer == nil {
t.Errorf("Expected task timer to not be nil")
}
// Verify that the next time for the task is not zero
if scheduler.tasks["task1"].next == (time.Time{}) {
t.Errorf("Expected task next time to not be zero")
}
}
func TestInternal_ScheduleAt(t *testing.T) {
// Initialize a new Scheduler
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {},
},
}
// Initialize a new Internal
internal := &Internal{
scheduler: scheduler,
}
// Define a time for scheduling
scheduleTime := time.Now().Add(time.Minute)
// Call the ScheduleAt method
internal.ScheduleAt("task1", scheduleTime)
// Verify that the timer for the task is not nil
if scheduler.tasks["task1"].timer == nil {
t.Errorf("Expected task timer to not be nil")
}
// Verify that the next time for the task is the same as the scheduled time
if scheduler.tasks["task1"].next != scheduleTime {
t.Errorf("Expected task next time to be %v, got %v", scheduleTime, scheduler.tasks["task1"].next)
}
}
func TestInternal_ScheduleSooner(t *testing.T) {
// Initialize a new Scheduler
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {},
},
}
// Initialize a new Internal
internal := &Internal{
scheduler: scheduler,
}
// Call the Schedule method with a longer duration
internal.Schedule("task1", time.Minute*2)
// Call the ScheduleSooner method with a shorter duration
internal.ScheduleSooner("task1", time.Minute)
// Verify that the timer for the task is not nil
if scheduler.tasks["task1"].timer == nil {
t.Errorf("Expected task timer to not be nil")
}
// Verify that the next time for the task is sooner than the original schedule
if scheduler.tasks["task1"].next.After(time.Now().Add(time.Minute * 2)) {
t.Errorf("Expected task next time to be sooner")
}
}
func TestInternal_StopTimer_timerNotDrained(t *testing.T) {
// Initialize a new Scheduler
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {
timer: startTimer(time.Minute),
},
},
}
// Initialize a new Internal
internal := &Internal{
scheduler: scheduler,
}
ready := internal.StopTimer("task1")
if ready {
t.Errorf("Expected task not to be ready")
}
if scheduler.tasks["task1"].timer != nil {
t.Errorf("Expected task timer to be nil")
}
}
func TestInternal_ScheduleAtSooner(t *testing.T) {
// Initialize a new Scheduler
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {},
},
}
// Initialize a new Internal
internal := &Internal{
scheduler: scheduler,
}
// Define a time for scheduling
laterTime := time.Now().Add(time.Minute * 2)
// Call the ScheduleAt method with a later time
internal.ScheduleAt("task1", laterTime)
// Define a sooner time for scheduling
soonerTime := time.Now().Add(time.Minute)
// Call the ScheduleAtSooner method with a sooner time
internal.ScheduleAtSooner("task1", soonerTime)
// Verify that the timer for the task is not nil
if scheduler.tasks["task1"].timer == nil {
t.Errorf("Expected task timer to not be nil")
}
// Verify that the next time for the task is the same as the sooner time
if scheduler.tasks["task1"].next != soonerTime {
t.Errorf("Expected task next time to be %v, got %v", soonerTime, scheduler.tasks["task1"].next)
}
// Define a later time for scheduling
laterTime = time.Now().Add(time.Minute * 3)
// Call the ScheduleAtSooner method with a later time
internal.ScheduleAtSooner("task1", laterTime)
// Verify that the next time for the task is still the same as the sooner time
if scheduler.tasks["task1"].next != soonerTime {
t.Errorf("Expected task next time to still be %v, got %v", soonerTime, scheduler.tasks["task1"].next)
}
// verify that scheduling with the same time does nothing
existingTimer := scheduler.tasks["task1"].timer
internal.ScheduleAtSooner("task1", soonerTime)
if scheduler.tasks["task1"].timer != existingTimer {
t.Fatal()
}
internal.ScheduleAtSooner("task1", soonerTime.UTC())
if scheduler.tasks["task1"].timer != existingTimer {
t.Fatal()
}
if scheduler.tasks["task1"].next != soonerTime {
t.Fatal()
}
// reschedule sooner, and verify that the old timer was stopped
internal.ScheduleAtSooner("task1", soonerTime.Add(-1))
if scheduler.tasks["task1"].timer == existingTimer || scheduler.tasks["task1"].timer == nil {
t.Fatal()
}
select {
case <-existingTimer.C:
t.Fatal()
default:
}
if existingTimer.Stop() {
t.Fatal()
}
if !scheduler.tasks["task1"].next.Equal(soonerTime.Add(-1)) {
t.Fatal()
}
}
func TestInternal_ScheduleSooner_InvalidDuration(t *testing.T) {
// Initialize a new Scheduler
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {},
},
}
// Initialize a new Internal
internal := &Internal{
scheduler: scheduler,
}
// Define a non-positive duration
invalidDuration := time.Duration(0)
// Call the ScheduleSooner method with the invalid duration and expect a panic
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
internal.ScheduleSooner("task1", invalidDuration)
}
func TestInternal_ScheduleAtSooner_zeroValue(t *testing.T) {
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {},
},
}
internal := &Internal{
scheduler: scheduler,
}
defer func() {
if r := recover(); r != `smartpoll: cannot schedule at sooner of zero time` {
t.Error(r)
}
}()
internal.ScheduleAtSooner("task1", time.Time{})
}
func TestInternal_ScheduleAtSooner_alreadyReady(t *testing.T) {
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {
timer: readySentinel,
},
},
}
internal := &Internal{
scheduler: scheduler,
}
internal.ScheduleAtSooner("task1", time.Unix(0, 0))
if v := scheduler.tasks["task1"].timer; v != readySentinel {
t.Error(v)
}
if v := scheduler.tasks["task1"].next; !v.Equal(time.Unix(0, 0)) {
t.Error(v)
}
}
func TestInternal_ScheduleAtSooner_alreadyReadyNotSentinel(t *testing.T) {
lower := time.Now().Add(time.Minute)
upper := lower.Add(1)
timer := time.NewTimer(1)
time.Sleep(time.Millisecond * 30)
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {
next: upper,
timer: timer,
},
},
}
internal := &Internal{
scheduler: scheduler,
}
internal.ScheduleAtSooner("task1", lower)
// N.B. will get swapped out to be readySentinel while checking if ready
if v := scheduler.tasks["task1"].timer; v != readySentinel {
t.Error(v)
}
if v := scheduler.tasks["task1"].next; !v.Equal(lower) || v.Equal(upper) {
t.Error(v)
}
}
func TestInternal_ScheduleAt_clearIfZeroValue(t *testing.T) {
scheduler := &Scheduler{
tasks: map[any]*taskState{
"task1": {
next: time.Now(),
timer: readySentinel,
},
},
}
internal := &Internal{
scheduler: scheduler,
}
internal.ScheduleAt("task1", time.Time{})
if v := scheduler.tasks["task1"].timer; v != nil {
t.Error(v)
}
if v := scheduler.tasks["task1"].next; v != (time.Time{}) {
t.Error(v)
}
}