Skip to content

Commit

Permalink
Remove overhead from shelling out to execute a job, for purpose of sc…
Browse files Browse the repository at this point in the history
…heduling tests.
  • Loading branch information
tooolbox committed May 26, 2020
1 parent 5c8c3dd commit 711d86e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 3 additions & 0 deletions job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ type Job struct {
// when it's done rescheduling rather than when the job is done running.
// That's most useful for testing the scheduling aspect of jobs.
ranChan chan struct{}

// Used for testing schedules.
succeedInstantly bool
}

type jobType int
Expand Down
7 changes: 4 additions & 3 deletions job/job_recurring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func TestRecur(t *testing.T) {
j := GetMockRecurringJobWithSchedule(start, testStruct.Interval)
j.clk.SetClock(clk)
j.ResumeAtNextScheduledTime = true // This is important to have on so that there's no drift.
j.succeedInstantly = true // Eliminate any potential variance due to the overhead of shelling out.

cache := NewMockCache()
j.Init(cache)
Expand All @@ -144,7 +145,7 @@ func TestRecur(t *testing.T) {
select {
case <-j.ranChan:
t.Fatalf("Expected job not run on checkpoint %d of test %s.", i, testStruct.Name)
case <-time.After(time.Second * 2):
case <-time.After(time.Second * 1):
}

j.lock.RLock()
Expand All @@ -155,15 +156,15 @@ func TestRecur(t *testing.T) {

select {
case <-j.ranChan:
case <-time.After(time.Second * 2):
case <-time.After(time.Second * 5):
t.Fatalf("Expected job to have run on checkpoint %d of test %s.", i, testStruct.Name)
}

j.lock.RLock()
assert.Equal(t, i+1, int(j.Metadata.SuccessCount), fmt.Sprintf("2nd Test of %s index %d", testStruct.Name, i))
j.lock.RUnlock()

briefPause()
time.Sleep(time.Millisecond * 500)
}

}()
Expand Down
4 changes: 3 additions & 1 deletion job/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func (j *JobRunner) Run(cache JobCache) (*JobStat, Metadata, error) {
var out string
for {
var err error
if j.job.JobType == LocalJob {
if j.job.succeedInstantly {
out = "Job succeeded instantly for test purposes."
} else if j.job.JobType == LocalJob {
out, err = j.LocalRun()
} else if j.job.JobType == RemoteJob {
out, err = j.RemoteRun()
Expand Down

0 comments on commit 711d86e

Please sign in to comment.