@@ -10,7 +10,7 @@ import (
10
10
// Many tests schedule a job for every second, and then wait at most a second
11
11
// for it to run. This amount is just slightly larger than 1 second to
12
12
// compensate for a few milliseconds of runtime.
13
- const ONE_SECOND = 1 * time .Second + 10 * time .Millisecond
13
+ const OneSecond = 1 * time .Second + 10 * time .Millisecond
14
14
15
15
func TestFuncPanicRecovery (t * testing.T ) {
16
16
cron := New ()
@@ -19,7 +19,7 @@ func TestFuncPanicRecovery(t *testing.T) {
19
19
cron .AddFunc ("" , "* * * * * ?" , func () { panic ("YOLO" ) })
20
20
21
21
select {
22
- case <- time .After (ONE_SECOND ):
22
+ case <- time .After (OneSecond ):
23
23
return
24
24
}
25
25
}
@@ -39,7 +39,7 @@ func TestJobPanicRecovery(t *testing.T) {
39
39
cron .AddJob ("" , "* * * * * ?" , job )
40
40
41
41
select {
42
- case <- time .After (ONE_SECOND ):
42
+ case <- time .After (OneSecond ):
43
43
return
44
44
}
45
45
}
@@ -50,8 +50,8 @@ func TestNoEntries(t *testing.T) {
50
50
cron .Start ()
51
51
52
52
select {
53
- case <- time .After (ONE_SECOND ):
54
- t .FailNow ( )
53
+ case <- time .After (OneSecond ):
54
+ t .Fatal ( "expected cron will be stopped immediately" )
55
55
case <- stop (cron ):
56
56
}
57
57
}
@@ -67,10 +67,10 @@ func TestStopCausesJobsToNotRun(t *testing.T) {
67
67
cron .AddFunc ("" , "* * * * * ?" , func () { wg .Done () })
68
68
69
69
select {
70
- case <- time .After (ONE_SECOND ):
70
+ case <- time .After (OneSecond ):
71
71
// No job ran!
72
72
case <- wait (wg ):
73
- t .FailNow ( )
73
+ t .Fatal ( "expected stopped cron does not run any job" )
74
74
}
75
75
}
76
76
@@ -86,8 +86,8 @@ func TestAddBeforeRunning(t *testing.T) {
86
86
87
87
// Give cron 2 seconds to run our job (which is always activated).
88
88
select {
89
- case <- time .After (ONE_SECOND ):
90
- t .FailNow ( )
89
+ case <- time .After (OneSecond ):
90
+ t .Fatal ( "expected job runs" )
91
91
case <- wait (wg ):
92
92
}
93
93
}
@@ -103,8 +103,8 @@ func TestAddWhileRunning(t *testing.T) {
103
103
cron .AddFunc ("" , "* * * * * ?" , func () { wg .Done () })
104
104
105
105
select {
106
- case <- time .After (ONE_SECOND ):
107
- t .FailNow ( )
106
+ case <- time .After (OneSecond ):
107
+ t .Fatal ( "expected job runs" )
108
108
case <- wait (wg ):
109
109
}
110
110
}
@@ -118,10 +118,9 @@ func TestAddWhileRunningWithDelay(t *testing.T) {
118
118
var calls = 0
119
119
cron .AddFunc ("" , "* * * * * *" , func () { calls += 1 })
120
120
121
- <- time .After (ONE_SECOND )
121
+ <- time .After (OneSecond )
122
122
if calls != 1 {
123
- fmt .Printf ("called %d times, expected 1\n " , calls )
124
- t .Fail ()
123
+ t .Errorf ("called %d times, expected 1\n " , calls )
125
124
}
126
125
}
127
126
@@ -137,14 +136,14 @@ func TestSnapshotEntries(t *testing.T) {
137
136
138
137
// Cron should fire in 2 seconds. After 1 second, call Entries.
139
138
select {
140
- case <- time .After (ONE_SECOND ):
139
+ case <- time .After (OneSecond ):
141
140
cron .Entries ()
142
141
}
143
142
144
143
// Even though Entries was called, the cron should fire at the 2 second mark.
145
144
select {
146
- case <- time .After (ONE_SECOND ):
147
- t .FailNow ( )
145
+ case <- time .After (OneSecond ):
146
+ t .Error ( "expected job runs at 2 second mark" )
148
147
case <- wait (wg ):
149
148
}
150
149
@@ -168,8 +167,8 @@ func TestMultipleEntries(t *testing.T) {
168
167
defer cron .Stop ()
169
168
170
169
select {
171
- case <- time .After (ONE_SECOND ):
172
- t .FailNow ( )
170
+ case <- time .After (OneSecond ):
171
+ t .Error ( "expected job run in proper order" )
173
172
case <- wait (wg ):
174
173
}
175
174
}
@@ -188,8 +187,8 @@ func TestRunningJobTwice(t *testing.T) {
188
187
defer cron .Stop ()
189
188
190
189
select {
191
- case <- time .After (2 * ONE_SECOND ):
192
- t .FailNow ( )
190
+ case <- time .After (2 * OneSecond ):
191
+ t .Error ( "expected job fires 2 times" )
193
192
case <- wait (wg ):
194
193
}
195
194
}
@@ -210,8 +209,8 @@ func TestRunningMultipleSchedules(t *testing.T) {
210
209
defer cron .Stop ()
211
210
212
211
select {
213
- case <- time .After (2 * ONE_SECOND ):
214
- t .FailNow ( )
212
+ case <- time .After (2 * OneSecond ):
213
+ t .Error ( "expected job fires 2 times" )
215
214
case <- wait (wg ):
216
215
}
217
216
}
@@ -221,7 +220,7 @@ func TestLocalTimezone(t *testing.T) {
221
220
wg := & sync.WaitGroup {}
222
221
wg .Add (2 )
223
222
224
- now := time .Now (). Local ()
223
+ now := time .Now ()
225
224
spec := fmt .Sprintf ("%d,%d %d %d %d %d ?" ,
226
225
now .Second ()+ 1 , now .Second ()+ 2 , now .Minute (), now .Hour (), now .Day (), now .Month ())
227
226
@@ -231,8 +230,8 @@ func TestLocalTimezone(t *testing.T) {
231
230
defer cron .Stop ()
232
231
233
232
select {
234
- case <- time .After (ONE_SECOND * 2 ):
235
- t .FailNow ( )
233
+ case <- time .After (OneSecond * 2 ):
234
+ t .Error ( "expected job fires 2 times" )
236
235
case <- wait (wg ):
237
236
}
238
237
}
@@ -258,8 +257,8 @@ func TestNonLocalTimezone(t *testing.T) {
258
257
defer cron .Stop ()
259
258
260
259
select {
261
- case <- time .After (ONE_SECOND * 2 ):
262
- t .FailNow ( )
260
+ case <- time .After (OneSecond * 2 ):
261
+ t .Error ( "expected job fires 2 times" )
263
262
case <- wait (wg ):
264
263
}
265
264
}
@@ -280,6 +279,67 @@ func (t testJob) Run() {
280
279
t .wg .Done ()
281
280
}
282
281
282
+ // Test that adding an invalid job spec returns an error
283
+ func TestInvalidJobSpec (t * testing.T ) {
284
+ cron := New ()
285
+ _ , err := cron .AddJob ("" , "this will not parse" , nil )
286
+ if err == nil {
287
+ t .Errorf ("expected an error with invalid spec, got nil" )
288
+ }
289
+ }
290
+
291
+ // Test blocking run method behaves as Start()
292
+ func TestBlockingRun (t * testing.T ) {
293
+ wg := & sync.WaitGroup {}
294
+ wg .Add (1 )
295
+
296
+ cron := New ()
297
+ cron .AddFunc ("" , "* * * * * ?" , func () { wg .Done () })
298
+
299
+ var unblockChan = make (chan struct {})
300
+
301
+ go func () {
302
+ cron .Run ()
303
+ close (unblockChan )
304
+ }()
305
+ defer cron .Stop ()
306
+
307
+ select {
308
+ case <- time .After (OneSecond ):
309
+ t .Error ("expected job fires" )
310
+ case <- unblockChan :
311
+ t .Error ("expected that Run() blocks" )
312
+ case <- wait (wg ):
313
+ }
314
+ }
315
+
316
+ // Test that double-running is a no-op
317
+ func TestStartNoop (t * testing.T ) {
318
+ var tickChan = make (chan struct {}, 2 )
319
+
320
+ cron := New ()
321
+ cron .AddFunc ("" , "* * * * * ?" , func () {
322
+ tickChan <- struct {}{}
323
+ })
324
+
325
+ cron .Start ()
326
+ defer cron .Stop ()
327
+
328
+ // Wait for the first firing to ensure the runner is going
329
+ <- tickChan
330
+
331
+ cron .Start ()
332
+
333
+ <- tickChan
334
+
335
+ // Fail if this job fires again in a short period, indicating a double-run
336
+ select {
337
+ case <- time .After (time .Millisecond ):
338
+ case <- tickChan :
339
+ t .Error ("expected job fires exactly twice" )
340
+ }
341
+ }
342
+
283
343
// Simple test using Runnables.
284
344
func TestJob (t * testing.T ) {
285
345
wg := & sync.WaitGroup {}
@@ -297,7 +357,7 @@ func TestJob(t *testing.T) {
297
357
defer cron .Stop ()
298
358
299
359
select {
300
- case <- time .After (ONE_SECOND ):
360
+ case <- time .After (OneSecond ):
301
361
t .FailNow ()
302
362
case <- wait (wg ):
303
363
}
@@ -312,12 +372,31 @@ func TestJob(t *testing.T) {
312
372
313
373
for i , expected := range expecteds {
314
374
if actuals [i ] != expected {
315
- t .Errorf ("Jobs not in the right order. (expected) %s != %s (actual)" , expecteds , actuals )
316
- t .FailNow ()
375
+ t .Fatalf ("Jobs not in the right order. (expected) %s != %s (actual)" , expecteds , actuals )
317
376
}
318
377
}
319
378
}
320
379
380
+ type ZeroSchedule struct {}
381
+
382
+ func (* ZeroSchedule ) Next (time.Time ) time.Time {
383
+ return time.Time {}
384
+ }
385
+
386
+ // Tests that job without time does not run
387
+ func TestJobWithZeroTimeDoesNotRun (t * testing.T ) {
388
+ cron := New ()
389
+ calls := 0
390
+ cron .AddFunc ("" , "* * * * * *" , func () { calls += 1 })
391
+ cron .Schedule ("" , "" , new (ZeroSchedule ), FuncJob (func () { t .Error ("expected zero task will not run" ) }))
392
+ cron .Start ()
393
+ defer cron .Stop ()
394
+ <- time .After (OneSecond )
395
+ if calls != 1 {
396
+ t .Errorf ("called %d times, expected 1\n " , calls )
397
+ }
398
+ }
399
+
321
400
func wait (wg * sync.WaitGroup ) chan bool {
322
401
ch := make (chan bool )
323
402
go func () {
0 commit comments