Skip to content

Commit 97db47e

Browse files
committed
test: fix TestCacheConcurrentWrites race detector flakiness
1 parent 2d6273e commit 97db47e

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

pkg/remoteresolution/resolver/framework/cache/cache_test.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -508,35 +508,44 @@ func TestCacheConcurrentWrites(t *testing.T) {
508508
<-done
509509
}
510510

511-
// Verify that entries are in the cache and accessible
512-
// Cache size is 1000, and we wrote 500 writers × 10 entries = 5000 entries
513-
// Due to LRU eviction, only the most recent ~1000 entries should remain
514-
// Check a sample of entries from the last 10 writers (which should still be in cache)
511+
// After all concurrent writes, add some entries synchronously
512+
// These are guaranteed to be the most recent entries in the cache
513+
numSyncEntries := 20
514+
syncEntries := make([][]pipelinev1.Param, numSyncEntries)
515+
for i := range numSyncEntries {
516+
params := []pipelinev1.Param{
517+
{Name: "bundle", Value: pipelinev1.ParamValue{
518+
Type: pipelinev1.ParamTypeString,
519+
StringVal: fmt.Sprintf("registry.io/sync-entry%d@sha256:%064d", i, 999000+i),
520+
}},
521+
}
522+
syncEntries[i] = params
523+
mockResource := &mockResolvedResource{
524+
data: []byte(fmt.Sprintf("sync-data-%d", i)),
525+
}
526+
cache.Add(resolverType, params, mockResource)
527+
}
528+
529+
// Verify all synchronous entries are retrievable
530+
// Since they were written most recently, they should all be in cache
515531
cachedCount := 0
516-
for writerID := numWriters - 10; writerID < numWriters; writerID++ {
517-
for j := range 10 { // Check all entries from these writers
518-
params := []pipelinev1.Param{
519-
{Name: "bundle", Value: pipelinev1.ParamValue{
520-
Type: pipelinev1.ParamTypeString,
521-
StringVal: fmt.Sprintf("registry.io/writer%d-entry%d@sha256:%064d", writerID, j, writerID*100+j),
522-
}},
523-
}
524-
cached, ok := cache.Get(resolverType, params)
525-
if ok && cached != nil {
526-
cachedCount++
527-
// Verify the data is correct using Data() method
528-
expectedData := fmt.Sprintf("writer-%d-data-%d", writerID, j)
529-
if string(cached.Data()) != expectedData {
530-
t.Errorf("Expected data '%s' for writer %d entry %d, got '%s'", expectedData, writerID, j, string(cached.Data()))
531-
}
532+
for i, params := range syncEntries {
533+
cached, ok := cache.Get(resolverType, params)
534+
if !ok || cached == nil {
535+
t.Errorf("Expected cache hit for sync entry %d, but got miss", i)
536+
} else {
537+
cachedCount++
538+
// Verify the data is correct
539+
expectedData := fmt.Sprintf("sync-data-%d", i)
540+
if string(cached.Data()) != expectedData {
541+
t.Errorf("Expected data '%s' for sync entry %d, got '%s'", expectedData, i, string(cached.Data()))
532542
}
533543
}
534544
}
535545

536-
// We expect at least some recent entries to be cached
537-
// Due to concurrent access and LRU, we use a conservative threshold (at least 20% of the 100 we checked)
538-
if cachedCount < 20 {
539-
t.Errorf("Expected at least 20 recent entries to be cached, but only found %d", cachedCount)
546+
// All synchronous entries should be in cache since they were written most recently
547+
if cachedCount != numSyncEntries {
548+
t.Errorf("Expected all %d synchronous entries to be cached, but only found %d", numSyncEntries, cachedCount)
540549
}
541550
}
542551

0 commit comments

Comments
 (0)