Skip to content

Commit 0f466ba

Browse files
authored
Merge pull request kubernetes#94537 from knight42/fix/TestCacheNoConcurrentGet
test(azure): Deflake TestCacheNoConcurrentGet
2 parents a84419f + 2b1395a commit 0f466ba

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/cache/azure_cache.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ func (t *TimedCache) getInternal(key string) (*AzureCacheEntry, error) {
103103
t.Lock.Lock()
104104
defer t.Lock.Unlock()
105105

106+
// Another goroutine might have written the same key.
107+
entry, exists, err = t.Store.GetByKey(key)
108+
if err != nil {
109+
return nil, err
110+
}
111+
if exists {
112+
return entry.(*AzureCacheEntry), nil
113+
}
114+
106115
// Still not found, add new entry with nil data.
107116
// Note the data will be filled later by getter.
108117
newEntry := &AzureCacheEntry{

staging/src/k8s.io/legacy-cloud-providers/azure/cache/azure_cache_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ func TestCacheNoConcurrentGet(t *testing.T) {
195195
var wg sync.WaitGroup
196196
for i := 0; i < 5; i++ {
197197
wg.Add(1)
198-
go cache.Get(key, CacheReadTypeDefault)
199-
wg.Done()
198+
go func() {
199+
defer wg.Done()
200+
_, _ = cache.Get(key, CacheReadTypeDefault)
201+
}()
200202
}
201203
v, err := cache.Get(key, CacheReadTypeDefault)
202204
wg.Wait()

0 commit comments

Comments
 (0)