mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-06 18:03:29 +02:00
Stabilize asyncbuffer.Cond tests
This commit is contained in:
@@ -2,6 +2,7 @@ package asyncbuffer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -25,8 +26,6 @@ func (s *TestCondSuite) TeardownTest() {
|
|||||||
|
|
||||||
// TestBasicWaitAndTick tests the basic functionality of the Cond
|
// TestBasicWaitAndTick tests the basic functionality of the Cond
|
||||||
func (s *TestCondSuite) TestBasicWaitAndTick() {
|
func (s *TestCondSuite) TestBasicWaitAndTick() {
|
||||||
done := make(chan struct{})
|
|
||||||
|
|
||||||
ch := s.cond.ch
|
ch := s.cond.ch
|
||||||
|
|
||||||
// Start a goroutine that will tick after a short delay
|
// 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
|
// Start a goroutine that will wait for the tick
|
||||||
|
var done atomic.Bool
|
||||||
go func() {
|
go func() {
|
||||||
s.cond.Wait()
|
s.cond.Wait()
|
||||||
close(done)
|
done.Store(true)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s.Require().Eventually(func() bool {
|
s.Require().Eventually(done.Load, 200*time.Millisecond, 10*time.Millisecond)
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}, 100*time.Millisecond, 10*time.Millisecond)
|
|
||||||
|
|
||||||
// Means that and old channel was closed and a new one has been created
|
// Means that and old channel was closed and a new one has been created
|
||||||
s.Require().NotEqual(ch, s.cond.ch)
|
s.Require().NotEqual(ch, s.cond.ch)
|
||||||
@@ -78,21 +71,14 @@ func (s *TestCondSuite) TestWaitMultipleWaiters() {
|
|||||||
startWg.Wait()
|
startWg.Wait()
|
||||||
|
|
||||||
// Wait for all waiters to complete
|
// Wait for all waiters to complete
|
||||||
done := make(chan struct{})
|
var done atomic.Bool
|
||||||
go func() {
|
go func() {
|
||||||
s.cond.Tick() // Signal that execution can proceed
|
s.cond.Tick() // Signal that execution can proceed
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(done)
|
done.Store(true)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s.Require().Eventually(func() bool {
|
s.Require().Eventually(done.Load, 200*time.Millisecond, 10*time.Millisecond)
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}, 100*time.Millisecond, 10*time.Millisecond)
|
|
||||||
|
|
||||||
// Check that all waiters were unblocked
|
// Check that all waiters were unblocked
|
||||||
for _, completed := range results {
|
for _, completed := range results {
|
||||||
@@ -137,20 +123,13 @@ func (s *TestCondSuite) TestRapidTicksAndWaits() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
done := make(chan struct{})
|
var done atomic.Bool
|
||||||
go func() {
|
go func() {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(done)
|
done.Store(true)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s.Require().Eventually(func() bool {
|
s.Require().Eventually(done.Load, 200*time.Millisecond, 10*time.Millisecond)
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}, 100*time.Millisecond, 10*time.Millisecond)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCond(t *testing.T) {
|
func TestCond(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user