From e7187ea51f3d8c00140d35b540851fec8558fdec Mon Sep 17 00:00:00 2001 From: DarthSim Date: Thu, 7 Aug 2025 12:35:24 +0300 Subject: [PATCH] Stabilize asyncbuffer.Cond tests --- asyncbuffer/cond_test.go | 41 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/asyncbuffer/cond_test.go b/asyncbuffer/cond_test.go index b7ce7bdd..ee25bd86 100644 --- a/asyncbuffer/cond_test.go +++ b/asyncbuffer/cond_test.go @@ -2,6 +2,7 @@ package asyncbuffer import ( "sync" + "sync/atomic" "testing" "time" @@ -25,8 +26,6 @@ func (s *TestCondSuite) TeardownTest() { // TestBasicWaitAndTick tests the basic functionality of the Cond func (s *TestCondSuite) TestBasicWaitAndTick() { - done := make(chan struct{}) - ch := s.cond.ch // Start a goroutine that will tick after a short delay @@ -36,19 +35,13 @@ func (s *TestCondSuite) TestBasicWaitAndTick() { }() // Start a goroutine that will wait for the tick + var done atomic.Bool go func() { s.cond.Wait() - close(done) + done.Store(true) }() - s.Require().Eventually(func() bool { - select { - case <-done: - return true - default: - return false - } - }, 100*time.Millisecond, 10*time.Millisecond) + s.Require().Eventually(done.Load, 200*time.Millisecond, 10*time.Millisecond) // Means that and old channel was closed and a new one has been created s.Require().NotEqual(ch, s.cond.ch) @@ -78,21 +71,14 @@ func (s *TestCondSuite) TestWaitMultipleWaiters() { startWg.Wait() // Wait for all waiters to complete - done := make(chan struct{}) + var done atomic.Bool go func() { s.cond.Tick() // Signal that execution can proceed wg.Wait() - close(done) + done.Store(true) }() - s.Require().Eventually(func() bool { - select { - case <-done: - return true - default: - return false - } - }, 100*time.Millisecond, 10*time.Millisecond) + s.Require().Eventually(done.Load, 200*time.Millisecond, 10*time.Millisecond) // Check that all waiters were unblocked for _, completed := range results { @@ -137,20 +123,13 @@ func (s *TestCondSuite) TestRapidTicksAndWaits() { }() } - done := make(chan struct{}) + var done atomic.Bool go func() { wg.Wait() - close(done) + done.Store(true) }() - s.Require().Eventually(func() bool { - select { - case <-done: - return true - default: - return false - } - }, 100*time.Millisecond, 10*time.Millisecond) + s.Require().Eventually(done.Load, 200*time.Millisecond, 10*time.Millisecond) } func TestCond(t *testing.T) {