Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the order of the returned jobs slice deterministic #652

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}
Loading