From 83f17045bab05dd9927edb999f9c9f8a893b93f7 Mon Sep 17 00:00:00 2001 From: SungJin1212 Date: Fri, 3 Jul 2026 12:01:34 +0900 Subject: [PATCH] fix flakiness in shard cache TTL tests Signed-off-by: SungJin1212 --- pkg/util/parquetutil/shard_cache_test.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pkg/util/parquetutil/shard_cache_test.go b/pkg/util/parquetutil/shard_cache_test.go index d8b4e8c531d..d0eb8c67e5a 100644 --- a/pkg/util/parquetutil/shard_cache_test.go +++ b/pkg/util/parquetutil/shard_cache_test.go @@ -107,16 +107,17 @@ func Test_Cache_TTLEvictionByLoop(t *testing.T) { val := cache.Get("key1") require.Equal(t, "value1", val) - // sleep longer than TTL - time.Sleep(150 * time.Millisecond) + c, ok := cache.(*ParquetShardCache[string]) + require.True(t, ok) - if c, ok := cache.(*ParquetShardCache[string]); ok { - // should delete by maintenance loop - _, ok := c.cache.Peek("key1") - require.False(t, ok) - } + // should still exist before TTL expires + _, ok = c.cache.Peek("key1") + require.True(t, ok) - require.NoError(t, testutil.GatherAndCompare(reg, bytes.NewBufferString(` + // Poll on the final metric state (not Peek) because the LRU eviction + // callback that updates the metrics runs after the entry is already + // removed from the map. + expected := ` # HELP cortex_parquet_cache_evictions_total Total number of parquet cache evictions # TYPE cortex_parquet_cache_evictions_total counter cortex_parquet_cache_evictions_total{name="test"} 1 @@ -126,5 +127,9 @@ func Test_Cache_TTLEvictionByLoop(t *testing.T) { # HELP cortex_parquet_cache_item_count Current number of cached parquet items # TYPE cortex_parquet_cache_item_count gauge cortex_parquet_cache_item_count{name="test"} 0 - `))) + ` + require.Eventually(t, func() bool { + _, ok := c.cache.Peek("key1") + return !ok && testutil.GatherAndCompare(reg, bytes.NewBufferString(expected)) == nil + }, 2*time.Second, 10*time.Millisecond) }