From cbf563fb9b0a34353639784640d63fb4e27bba2d Mon Sep 17 00:00:00 2001 From: Ian Littman Date: Mon, 23 Sep 2024 04:58:13 -0500 Subject: [PATCH] Use sync.Map for stubbed key-value store to avoid data races in GitOps test (#22292) This override only happens in testing, so this isn't release-blocking, but this is the quickest way to clean up a test that will otherwise be flaky due to data races, at the cost of performance (vs. setting up a more complex solution with mutexes). # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Added/updated tests - [x] Manual QA for all new/changed functionality (via manually running test using the KV store) --- cmd/fleetctl/gitops_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/fleetctl/gitops_test.go b/cmd/fleetctl/gitops_test.go index b934961559..1a054482f6 100644 --- a/cmd/fleetctl/gitops_test.go +++ b/cmd/fleetctl/gitops_test.go @@ -10,6 +10,7 @@ import ( "path/filepath" "slices" "strings" + "sync" "testing" "time" @@ -2919,24 +2920,23 @@ software: } type memKeyValueStore struct { - m map[string]string + m sync.Map } func newMemKeyValueStore() *memKeyValueStore { - return &memKeyValueStore{ - m: make(map[string]string), - } + return &memKeyValueStore{} } func (m *memKeyValueStore) Set(ctx context.Context, key string, value string, expireTime time.Duration) error { - m.m[key] = value + m.m.Store(key, value) return nil } func (m *memKeyValueStore) Get(ctx context.Context, key string) (*string, error) { - v, ok := m.m[key] + v, ok := m.m.Load(key) if !ok { return nil, nil } - return &v, nil + vAsString := v.(string) + return &vAsString, nil }