make the order of the returned jobs slice deterministic (#652)

This commit is contained in:
John Roesler 2024-01-02 10:47:01 -06:00 committed by GitHub
parent 29a2f29e3c
commit ae366d91ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -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:

View File

@ -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)
})
}
}