From ae366d91ea618d1c19433bc2cd757f250c99a983 Mon Sep 17 00:00:00 2001 From: John Roesler Date: Tue, 2 Jan 2024 10:47:01 -0600 Subject: [PATCH] make the order of the returned jobs slice deterministic (#652) --- scheduler.go | 11 +++++++++++ scheduler_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/scheduler.go b/scheduler.go index 4477a9bb..eebb221e 100644 --- a/scheduler.go +++ b/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: diff --git a/scheduler_test.go b/scheduler_test.go index a2a0fb1f..3df33bb3 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -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) + }) + } +}