fix data race in TestAutomationsSchedule (#7952)

This commit is contained in:
Roberto Dip 2022-09-26 17:19:38 -03:00 committed by GitHub
parent 1fd6844bb0
commit 3a349bb07b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -23,6 +23,23 @@ import (
"github.com/stretchr/testify/require"
)
// safeStore is a wrapper around mock.Store to allow for concurrent calling to
// AppConfig, in the past we have seen this test fail with a data race warning.
//
// TODO: if we see other tests failing for similar reasons, we should build a
// more robust pattern instead of doing this everywhere
type safeStore struct {
mock.Store
mu sync.Mutex
}
func (s *safeStore) AppConfig(ctx context.Context) (*fleet.AppConfig, error) {
s.mu.Lock()
s.AppConfigFuncInvoked = true
s.mu.Unlock()
return s.AppConfigFunc(ctx)
}
func TestMaybeSendStatistics(t *testing.T) {
ds := new(mock.Store)
@ -130,7 +147,7 @@ func TestMaybeSendStatisticsSkipsIfNotConfigured(t *testing.T) {
}
func TestAutomationsSchedule(t *testing.T) {
ds := new(mock.Store)
ds := new(safeStore)
endpointCalled := int32(0)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -313,7 +330,7 @@ func TestCronVulnerabilitiesSkipMkdirIfDisabled(t *testing.T) {
// for the current automation crons and that their duration is equal to the current
// schedule interval.
func TestAutomationsScheduleLockDuration(t *testing.T) {
ds := new(mock.Store)
ds := new(safeStore)
expectedInterval := 1 * time.Second
intitalConfigLoaded := make(chan struct{}, 1)
@ -379,7 +396,7 @@ func TestAutomationsScheduleLockDuration(t *testing.T) {
}
func TestAutomationsScheduleIntervalChange(t *testing.T) {
ds := new(mock.Store)
ds := new(safeStore)
interval := struct {
sync.Mutex