From df47637d52cd3d59e10a443f15833568d500dd7b Mon Sep 17 00:00:00 2001 From: bwplotka Date: Tue, 16 Dec 2025 14:04:28 +0000 Subject: [PATCH] fix TestScrapeLoopRun flakiness Signed-off-by: bwplotka --- scrape/scrape_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index b31e8e3e40..9b96c378f9 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -1041,10 +1041,11 @@ func TestScrapeLoopRun(t *testing.T) { // scraper. The scraper has to respect the context. scraper.offsetDur = 0 - block := make(chan struct{}) + blockCtx, blockCancel := context.WithCancel(t.Context()) scraper.scrapeFunc = func(ctx context.Context, _ io.Writer) error { select { - case <-block: + case <-blockCtx.Done(): + cancel() case <-ctx.Done(): return ctx.Err() } @@ -1066,9 +1067,7 @@ func TestScrapeLoopRun(t *testing.T) { // We already caught the timeout error and are certainly in the loop. // Let the scrapes returns immediately to cause no further timeout errors // and check whether canceling the parent context terminates the loop. - close(block) - cancel() - + blockCancel() select { case <-signal: // Loop terminated as expected.