From 03e8d05f1859377678eab2a26e7a878bd0f49714 Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Thu, 1 Aug 2024 10:58:08 -0300 Subject: [PATCH] fix(scheduler): fix a data race in a unit test BE-11084 (#12057) --- api/scheduler/scheduler_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/api/scheduler/scheduler_test.go b/api/scheduler/scheduler_test.go index 126c00b6e..279c8d8b6 100644 --- a/api/scheduler/scheduler_test.go +++ b/api/scheduler/scheduler_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "sync/atomic" "testing" "time" @@ -70,12 +71,10 @@ func Test_JobShouldNotStop_UponError(t *testing.T) { s := NewScheduler(context.Background()) defer s.Shutdown() - var acc int + var acc atomic.Int64 ch := make(chan struct{}) s.StartJobEvery(jobInterval, func() error { - acc++ - - if acc == 2 { + if acc.Add(1) == 2 { close(ch) return NewPermanentError(fmt.Errorf("failed")) } @@ -85,7 +84,7 @@ func Test_JobShouldNotStop_UponError(t *testing.T) { <-time.After(3 * jobInterval) <-ch - assert.Equal(t, 2, acc) + assert.Equal(t, int64(2), acc.Load()) } func Test_CanTerminateAllJobs_ByShuttingDownScheduler(t *testing.T) {