8000 Fix: Handle job creation errors better by arjunmahishi · Pull Request #89 · go-co-op/gocron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix: Handle job creation errors better #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,16 @@ func (s *Scheduler) stopScheduler() {
func (s *Scheduler) Do(jobFun interface{}, params ...interface{}) (*Job, error) {
j := s.getCurrentJob()
if j.err != nil {
// delete the job from the scheduler as this job
// cannot be executed
s.RemoveByReference(j)
return nil, j.err
}

typ := reflect.TypeOf(jobFun)
if typ.Kind() != reflect.Func {
// delete the job for the same reason as above
s.RemoveByReference(j)
return nil, ErrNotAFunction
}

Expand Down
35 changes: 35 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ func TestAtFuture(t *testing.T) {
nextRun = dayJob.ScheduledTime()
assert.Equal(t, expectedStartTime, nextRun)
assert.Equal(t, false, shouldBeFalse, "Day job was not expected to run as it was in the future")
s.RemoveByReference(dayJob)

// error due to bad time format
badTime := "0:0"
s.Every(1).Day().At(badTime).Do(func() {})
assert.Zero(t, len(s.jobs), "The job should be deleted if the time format is wrong")

}

func schedulerForNextOrPreviousWeekdayEveryNTimes(weekday time.Weekday, next bool, n uint64, s *Scheduler) *Scheduler {
Expand Down Expand Up @@ -846,3 +853,31 @@ func TestRunJobsWithLimit(t *testing.T) {
assert.Exactly(t, 1, j1Counter)
assert.Exactly(t, 1, j2Counter)
}

func TestDo(t *testing.T) {
var tests = []struct {
name string
evalFunc func(*Scheduler)
}{
{
name: "error due to the arg passed to Do() not being a function",
evalFunc: func(s *Scheduler) {
s.Every(1).Second().Do(1)
assert.Zero(t, len(s.jobs), "The job should be deleted if the arg passed to Do() is not a function")
},
},
{
name: "positive case",
evalFunc: func(s *Scheduler) {
s.Every(1).Day().Do(func() {})
assert.Equal(t, 1, len(s.jobs))
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewScheduler(time.Local)
tt.evalFunc(s)
})
}
}
0