@@ -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
}
@@ -297,7 +296,7 @@ func TestJob(t *testing.T) {
297
296
defer cron .Stop ()
298
297
299
298
select {
300
- case <- time .After (ONE_SECOND ):
299
+ case <- time .After (OneSecond ):
301
300
t .FailNow ()
302
301
case <- wait (wg ):
303
302
}
@@ -312,12 +311,31 @@ func TestJob(t *testing.T) {
312
311
313
312
for i , expected := range expecteds {
314
313
if actuals [i ] != expected {
315
- t .Errorf ("Jobs not in the right order. (expected) %s != %s (actual)" , expecteds , actuals )
316
- t .FailNow ()
314
+ t .Fatalf ("Jobs not in the right order. (expected) %s != %s (actual)" , expecteds , actuals )
317
315
}
318
316
}
319
317
}
320
318
319
+ type ZeroSchedule struct {}
320
+
321
+ func (* ZeroSchedule ) Next (time.Time ) time.Time {
322
+ return time.Time {}
323
+ }
324
+
325
+ // Tests that job without time does not run
326
+ func TestJobWithZeroTimeDoesNotRun (t * testing.T ) {
327
+ cron := New ()
328
+ calls := 0
329
+ cron .AddFunc ("* * * * * *" , func () { calls += 1 })
330
+ cron .Schedule (new (ZeroSchedule ), FuncJob (func () { t .Error ("expected zero task will not run" ) }))
331
+ cron .Start ()
332
+ defer cron .Stop ()
333
+ <- time .After (OneSecond )
334
+ if calls != 1 {
335
+ t .Errorf ("called %d times, expected 1\n " , calls )
336
+ }
337
+ }
338
+
321
339
func wait (wg * sync.WaitGroup ) chan bool {
322
340
ch := make (chan bool )
323
341
go func () {
0 commit comments