fix(scheduler): fix a data race in a unit test BE-11084 (#12057)

pull/12071/head
andres-portainer 2024-08-01 10:58:08 -03:00 committed by GitHub
parent bedb7fb255
commit 03e8d05f18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -70,12 +71,10 @@ func Test_JobShouldNotStop_UponError(t *testing.T) {
s := NewScheduler(context.Background()) s := NewScheduler(context.Background())
defer s.Shutdown() defer s.Shutdown()
var acc int var acc atomic.Int64
ch := make(chan struct{}) ch := make(chan struct{})
s.StartJobEvery(jobInterval, func() error { s.StartJobEvery(jobInterval, func() error {
acc++ if acc.Add(1) == 2 {
if acc == 2 {
close(ch) close(ch)
return NewPermanentError(fmt.Errorf("failed")) return NewPermanentError(fmt.Errorf("failed"))
} }
@ -85,7 +84,7 @@ func Test_JobShouldNotStop_UponError(t *testing.T) {
<-time.After(3 * jobInterval) <-time.After(3 * jobInterval)
<-ch <-ch
assert.Equal(t, 2, acc) assert.Equal(t, int64(2), acc.Load())
} }
func Test_CanTerminateAllJobs_ByShuttingDownScheduler(t *testing.T) { func Test_CanTerminateAllJobs_ByShuttingDownScheduler(t *testing.T) {