From dc76fa869f9ae66a064b6fa4b76f229ac09fbd2f Mon Sep 17 00:00:00 2001 From: SungJin1212 Date: Tue, 17 Mar 2026 17:35:54 +0900 Subject: [PATCH] Fix flaky TestLoader_ShouldCacheIndexNotFoundOnBackgroundUpdates Signed-off-by: SungJin1212 --- pkg/storage/tsdb/bucketindex/loader_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/storage/tsdb/bucketindex/loader_test.go b/pkg/storage/tsdb/bucketindex/loader_test.go index 111819cb17f..8bd88559c16 100644 --- a/pkg/storage/tsdb/bucketindex/loader_test.go +++ b/pkg/storage/tsdb/bucketindex/loader_test.go @@ -475,14 +475,17 @@ func TestLoader_ShouldCacheIndexNotFoundOnBackgroundUpdates(t *testing.T) { return testutil.ToFloat64(loader.loadAttempts) > prevLoads }) + // Wait until the index is offloaded (metrics becomes 0). // We expect the bucket index is not considered loaded because of the error. - assert.NoError(t, testutil.GatherAndCompare(reg, bytes.NewBufferString(` + test.Poll(t, 3*time.Second, true, func() any { + err := testutil.GatherAndCompare(reg, bytes.NewBufferString(` # HELP cortex_bucket_index_loaded Number of bucket indexes currently loaded in-memory. # TYPE cortex_bucket_index_loaded gauge cortex_bucket_index_loaded 0 - `), - "cortex_bucket_index_loaded", - )) + `), "cortex_bucket_index_loaded") + + return err == nil + }) // Try to get the index again. We expect no load attempt because the error has been cached. prevLoads = testutil.ToFloat64(loader.loadAttempts)