diff --git a/quartz/job.go b/quartz/job.go index 2ec2dcc..d6cce0e 100644 --- a/quartz/job.go +++ b/quartz/job.go @@ -119,11 +119,11 @@ func (sh *ShellJob) Execute(ctx context.Context) { } else { sh.jobStatus = OK } + sh.Unlock() if sh.callback != nil { sh.callback(ctx, sh) } - sh.Unlock() } // ExitCode returns the exit code of the ShellJob. @@ -187,6 +187,9 @@ func NewCurlJob(request *http.Request) *CurlJob { // NewCurlJobWithOptions returns a new CurlJob configured with CurlJobOptions. func NewCurlJobWithOptions(request *http.Request, opts CurlJobOptions) *CurlJob { + if opts.HTTPClient == nil { + opts.HTTPClient = http.DefaultClient + } return &CurlJob{ httpClient: opts.HTTPClient, request: request, @@ -240,8 +243,6 @@ func formatRequest(r *http.Request) string { // Execute is called by a Scheduler when the Trigger associated with this job fires. func (cu *CurlJob) Execute(ctx context.Context) { cu.Lock() - defer cu.Unlock() - cu.request = cu.request.WithContext(ctx) var err error cu.response, err = cu.httpClient.Do(cu.request) @@ -251,6 +252,7 @@ func (cu *CurlJob) Execute(ctx context.Context) { } else { cu.jobStatus = FAILURE } + cu.Unlock() if cu.callback != nil { cu.callback(ctx, cu) diff --git a/quartz/job_test.go b/quartz/job_test.go index 68a0c70..b4efd4b 100644 --- a/quartz/job_test.go +++ b/quartz/job_test.go @@ -254,3 +254,37 @@ func TestShellJob_Execute(t *testing.T) { assertEqual(t, 127, sh.ExitCode()) // the return value is different under different platforms. } + +func TestShellJob_WithCallback(t *testing.T) { + stdoutShell := "echo -n ok" + resultChan := make(chan string, 1) + shJob := quartz.NewShellJobWithCallback( + stdoutShell, + func(_ context.Context, job *quartz.ShellJob) { + resultChan <- job.Stdout() + }, + ) + shJob.Execute(context.Background()) + + assertEqual(t, "", shJob.Stderr()) + assertEqual(t, "ok", shJob.Stdout()) + assertEqual(t, "ok", <-resultChan) +} + +func TestCurlJob_WithCallback(t *testing.T) { + request, err := http.NewRequest(http.MethodGet, worldtimeapiURL, nil) + if err != nil { + t.Fatal(err) + } + resultChan := make(chan quartz.JobStatus, 1) + opts := quartz.CurlJobOptions{ + Callback: func(_ context.Context, job *quartz.CurlJob) { + resultChan <- job.JobStatus() + }, + } + curlJob := quartz.NewCurlJobWithOptions(request, opts) + curlJob.Execute(context.Background()) + + assertEqual(t, 466866822, curlJob.Key()) + assertEqual(t, quartz.OK, <-resultChan) +} diff --git a/quartz/scheduler_test.go b/quartz/scheduler_test.go index 86f5c51..34fd997 100644 --- a/quartz/scheduler_test.go +++ b/quartz/scheduler_test.go @@ -37,6 +37,7 @@ func TestScheduler(t *testing.T) { jobKeys[3] = errCurlJob.Key() sched.Start(ctx) + assertEqual(t, sched.IsStarted(), true) sched.ScheduleJob(ctx, shellJob, quartz.NewSimpleTrigger(time.Millisecond*800)) sched.ScheduleJob(ctx, curlJob, quartz.NewRunOnceTrigger(time.Millisecond)) sched.ScheduleJob(ctx, errShellJob, quartz.NewRunOnceTrigger(time.Millisecond)) @@ -47,14 +48,17 @@ func TestScheduler(t *testing.T) { assertEqual(t, scheduledJobKeys, []int{3668896347, 2787962474}) _, err = sched.GetScheduledJob(jobKeys[0]) - if err != nil { - t.Fail() - } + assertEqual(t, err, nil) err = sched.DeleteJob(shellJob.Key()) - if err != nil { - t.Fail() - } + assertEqual(t, err, nil) + + nonExistentJobKey := 1111 + _, err = sched.GetScheduledJob(nonExistentJobKey) + assertNotEqual(t, err, nil) + + err = sched.DeleteJob(nonExistentJobKey) + assertNotEqual(t, err, nil) scheduledJobKeys = sched.GetJobKeys() assertEqual(t, len(scheduledJobKeys), 1) diff --git a/quartz/trigger_test.go b/quartz/trigger_test.go index 7955c47..857ac75 100644 --- a/quartz/trigger_test.go +++ b/quartz/trigger_test.go @@ -35,6 +35,7 @@ func TestRunOnceTrigger(t *testing.T) { assertEqual(t, err, nil) next, err = trigger.NextFireTime(next) + trigger.Description() assertEqual(t, next, 0) assertNotEqual(t, err, nil) }