From a96be32959ff1ac4157b3c80c19337a6b5c4d3c5 Mon Sep 17 00:00:00 2001 From: Julio Lopez <1953782+julio-lopez@users.noreply.github.com> Date: Tue, 23 Sep 2025 20:20:40 -0700 Subject: [PATCH] test(general): stop background workers when test fails (#4841) --- repo/content/content_manager_test.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/repo/content/content_manager_test.go b/repo/content/content_manager_test.go index 1910bfa08..96fed2563 100644 --- a/repo/content/content_manager_test.go +++ b/repo/content/content_manager_test.go @@ -1027,12 +1027,18 @@ func (s *contentManagerSuite) TestParallelWrites(t *testing.T) { defer bm.CloseShared(ctx) numWorkers := 8 - closeWorkers := make(chan bool) // workerLock allows workers to append to their own list of IDs (when R-locked) in parallel. // W-lock allows flusher to capture the state without any worker being able to modify it. workerWritten := make([][]ID, numWorkers) + var stopWorker atomic.Bool + + // ensure the worker routines are stopped even if the test fails early + t.Cleanup(func() { + stopWorker.Store(true) + }) + // start numWorkers, each writing random block and recording it for workerID := range numWorkers { workersWG.Add(1) @@ -1040,17 +1046,12 @@ func (s *contentManagerSuite) TestParallelWrites(t *testing.T) { go func() { defer workersWG.Done() - for { - select { - case <-closeWorkers: - return - case <-time.After(1 * time.Nanosecond): - id := writeContentAndVerify(ctx, t, bm, seededRandomData(rand.Int(), 100)) + for !stopWorker.Load() { + id := writeContentAndVerify(ctx, t, bm, seededRandomData(rand.Int(), 100)) - workerLock.RLock() - workerWritten[workerID] = append(workerWritten[workerID], id) - workerLock.RUnlock() - } + workerLock.RLock() + workerWritten[workerID] = append(workerWritten[workerID], id) + workerLock.RUnlock() } }() } @@ -1087,7 +1088,7 @@ func (s *contentManagerSuite) TestParallelWrites(t *testing.T) { } // shut down workers and wait for them - close(closeWorkers) + stopWorker.Store(true) workersWG.Wait() // flush and check once more