mirror of https://github.com/go-co-op/gocron.git
make the order of the returned jobs slice deterministic (#652)
This commit is contained in:
parent
29a2f29e3c
commit
ae366d91ea
11
scheduler.go
11
scheduler.go
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue