Skip to content

Commit

Permalink
Fix simple scheduling (distribworks#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Castell committed Jun 4, 2016
1 parent 3e460e7 commit fa71979
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cron/simple.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cron

import "time"
import (
"time"
)

// SimpleDelaySchedule represents a simple non recurring duration.
type SimpleSchedule struct {
Expand All @@ -17,5 +19,11 @@ func At(date time.Time) SimpleSchedule {
// Next conforms to the Schedule interface but this kind of jobs
// doesn't need to be run more than once, so it doesn't return a new date but the existing one.
func (schedule SimpleSchedule) Next(t time.Time) time.Time {
return schedule.Date
// If the date set is after the reference time return it
// if it's before, return a virtually infinite sleep date
// so do nothing.
if schedule.Date.After(t) {
return schedule.Date
}
return t.AddDate(10, 0, 0)
}
33 changes: 33 additions & 0 deletions cron/simple_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cron

import (
"testing"
"time"
)

func TestSimpleNext(t *testing.T) {
tests := []struct {
time string
date string
expected string
}{
// Simple cases
{"2012-07-09T14:45:00Z", "2012-07-09T15:00:00Z", "2012-07-09T15:00:00Z"},
{"2012-07-09T14:45:00Z", "2012-07-05T13:00:00Z", "2022-07-09T14:45:00Z"},
}

for _, c := range tests {
now, _ := time.Parse(time.RFC3339, c.time)
date, _ := time.Parse(time.RFC3339, c.date)
actual := At(date).Next(now)
expected, _ := time.Parse(time.RFC3339, c.expected)

if !actual.After(now) {
t.Errorf("%s, \"%s\": (expected) %v after %v (actual)", c.time, c.date, expected, actual)
}

if actual != expected {
t.Errorf("%s, \"%s\": (expected) %v != %v (actual)", c.time, c.date, expected, actual)
}
}
}

0 comments on commit fa71979

Please sign in to comment.