From 8ae74491e757ee48c64934baa24fa33ad118da4b Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz <4057165+aruiz14@users.noreply.github.com> Date: Wed, 29 Jan 2025 02:35:24 +0100 Subject: [PATCH] Fix race in accessstore test (#466) --- pkg/accesscontrol/access_store_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/accesscontrol/access_store_test.go b/pkg/accesscontrol/access_store_test.go index 97e2243c..e25cc7e8 100644 --- a/pkg/accesscontrol/access_store_test.go +++ b/pkg/accesscontrol/access_store_test.go @@ -315,24 +315,27 @@ func TestAccessStore_AccessFor_concurrent(t *testing.T) { const n = 5 // observation showed cases with up to 5 (or more) concurrent queries for the same user + ids := make([]string, n) wait := make(chan struct{}) var wg sync.WaitGroup - var id string - for range n { + for i := range n { wg.Add(1) go func() { <-wait - id = store.AccessFor(testUser).ID + ids[i] = store.AccessFor(testUser).ID wg.Done() }() } close(wait) wg.Wait() + if got, want := len(slices.Compact(ids)), 1; got != want { + t.Errorf("Unexpected number of ids: got %d, want %d", got, want) + } if got, want := len(asCache.setCalls), 1; got != want { t.Errorf("Unexpected number of cache entries: got %d, want %d", got, want) } - if got, want := asCache.setCalls[id], 1; got != want { + if got, want := asCache.setCalls[ids[0]], 1; got != want { t.Errorf("Unexpected number of calls to cache.Set(): got %d, want %d", got, want) } }