Skip to content

Commit 2315d57

Browse files
authored
Merge pull request robfig#120 from theothertomelliott/master
Add additional unit tests to cron_test.go
2 parents 736158d + 7002cd6 commit 2315d57

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

cron_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,67 @@ func (t testJob) Run() {
279279
t.wg.Done()
280280
}
281281

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+
282343
// Simple test using Runnables.
283344
func TestJob(t *testing.T) {
284345
wg := &sync.WaitGroup{}

0 commit comments

Comments
 (0)