@@ -279,6 +279,67 @@ func (t testJob) Run() {
279
279
t .wg .Done ()
280
280
}
281
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
+
282
343
// Simple test using Runnables.
283
344
func TestJob (t * testing.T ) {
284
345
wg := & sync.WaitGroup {}
0 commit comments