Skip to content

Commit

Permalink
Fix race in accessstore test (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
aruiz14 authored Jan 29, 2025
1 parent 5cdbd29 commit 8ae7449
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/accesscontrol/access_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 8ae7449

Please sign in to comment.