Skip to content

Commit

Permalink
test: improve code coverage (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn authored Nov 10, 2023
1 parent 8bead9e commit d0360f0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
8 changes: 5 additions & 3 deletions quartz/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions quartz/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 10 additions & 6 deletions quartz/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions quartz/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit d0360f0

Please sign in to comment.