Fix flaky TestScheduleOnceSequential (#36805)

The subtest used a fixed sleep before asserting callback counts, but
job.run waits until runAt plus up to scheduleOnceJitter. Under the race
detector or loaded CI that window can elapse after the sleep, so
newCount3 is still 0 and the assertion flakes.

Poll with require.Eventually (same approach as the paging subtest in
#35891) so the test waits for the scheduled callback without weakening
assertions.

Tests-only change. Verified with:
go test -run '^TestScheduleOnceSequential$/adding_two_callback_works' \
  -race -count=100 ./pluginapi/cluster/... (from server/public)

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: mattermost-code <matty-code@mattermost.com>
This commit is contained in:
cursor[bot]
2026-05-29 12:39:07 +02:00
committed by GitHub
parent 495fbc8437
commit e3823a51ce
@@ -300,9 +300,13 @@ func TestScheduleOnceSequential(t *testing.T) {
_, err = s.ScheduleOnce("anything", time.Now().Add(50*time.Millisecond), nil)
require.NoError(t, err)
time.Sleep(70*time.Millisecond + scheduleOnceJitter)
assert.Equal(t, int32(0), atomic.LoadInt32(newCount2))
assert.Equal(t, int32(1), atomic.LoadInt32(newCount3))
// Poll for the scheduled callback. A fixed sleep is flaky under the race
// detector and on loaded CI because job.run adds up to scheduleOnceJitter
// on top of the scheduled delay.
require.Eventually(t, func() bool {
return atomic.LoadInt32(newCount2) == int32(0) && atomic.LoadInt32(newCount3) == int32(1)
}, 5*time.Second, 50*time.Millisecond, "timed out waiting for scheduled callback")
})
t.Run("test paging keys from the db by inserting 3 pages of jobs and starting scheduler", func(t *testing.T) {