Skip to content

Commit

Permalink
make the order of the returned jobs slice deterministic (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler authored Jan 2, 2024
1 parent 29a2f29 commit ae366d9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ func (s *scheduler) selectAllJobsOutRequest(out allJobsOutRequest) {
outJobs[counter] = s.jobFromInternalJob(j)
counter++
}
slices.SortFunc(outJobs, func(a, b Job) int {
aID, bID := a.ID().String(), b.ID().String()
switch {
case aID < bID:
return -1
case aID > bID:
return 1
default:
return 0
}
})
select {
case <-s.shutdownCtx.Done():
case out.outChan <- outJobs:
Expand Down
29 changes: 29 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1635,3 +1635,32 @@ func TestScheduler_OneTimeJob(t *testing.T) {
})
}
}

func TestScheduler_Jobs(t *testing.T) {
tests := []struct {
name string
}{
{
"order is equal",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := newTestScheduler(t)

for i := 0; i <= 20; i++ {
_, err := s.NewJob(
DurationJob(time.Second),
NewTask(func() {}),
)
require.NoError(t, err)
}

jobsFirst := s.Jobs()
jobsSecond := s.Jobs()

assert.Equal(t, jobsFirst, jobsSecond)
})
}
}

0 comments on commit ae366d9

Please sign in to comment.