Skip to content

Commit

Permalink
don't accept a *Job in Job for Update if the ptr address doesn't match (
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler authored Aug 23, 2023
1 parent 8454d19 commit e3b4f5f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
5 changes: 5 additions & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,11 @@ func (s *Scheduler) TagsUnique() {
// of making changes to the job with the scheduler chain
// and finalized by calling Update()
func (s *Scheduler) Job(j *Job) *Scheduler {
if job, ok := s.JobsMap()[j.id]; !ok {
return s
} else if job != j {
return s
}
s.inScheduleChain = &j.id
s.updateJob = true
return s
Expand Down
36 changes: 25 additions & 11 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1759,21 +1759,35 @@ func TestScheduler_DoParameterValidation(t *testing.T) {
}

func TestScheduler_Job(t *testing.T) {
s := NewScheduler(time.UTC)
t.Run("happy", func(t *testing.T) {
s := NewScheduler(time.UTC)

j1, err := s.Every("1s").Do(func() {})
require.NoError(t, err)
assert.Equal(t, j1, s.jobs[j1.id])
j1, err := s.Every("1s").Do(func() {})
require.NoError(t, err)
assert.Equal(t, j1, s.jobs[j1.id])

j2, err := s.Every("1s").Do(func() {})
require.NoError(t, err)
assert.Equal(t, j2, s.jobs[j2.id])
j2, err := s.Every("1s").Do(func() {})
require.NoError(t, err)
assert.Equal(t, j2, s.jobs[j2.id])

s.Job(j1)
assert.Equal(t, j1, s.jobs[j1.id])
s.Job(j1)
assert.Equal(t, j1, s.jobs[j1.id])

s.Job(j2)
assert.Equal(t, j2, s.jobs[j2.id])
s.Job(j2)
assert.Equal(t, j2, s.jobs[j2.id])
})
t.Run("job address doesn't match after de/re-referencing", func(t *testing.T) {
s := NewScheduler(time.UTC)
j1, err := s.Every("1s").Do(func() {})
require.NoError(t, err)
assert.Equal(t, j1, s.jobs[j1.id])

j1PtrDeReferenced := *j1
j1PtrReReferenced := &j1PtrDeReferenced

_, err = s.Job(j1PtrReReferenced).Every("2s").Update()
assert.EqualError(t, err, ErrUpdateCalledWithoutJob.Error())
})
}

func TestScheduler_Update(t *testing.T) {
Expand Down

0 comments on commit e3b4f5f

Please sign in to comment.