@@ -17,13 +17,40 @@ const (
1717 sleep = time .Microsecond * 10
1818)
1919
20- func TestLimit (t * testing.T ) {
20+ func TestLimitSilent (t * testing.T ) {
21+ testLimit (t , false )
22+ }
23+
24+ func TestLimitVerbose (t * testing.T ) {
25+ testLimit (t , true )
26+ }
27+
28+ func testLimit (t * testing.T , withStuck bool ) {
2129 t .Parallel ()
2230
2331 var running int32
2432 var wg sync.WaitGroup
2533 limit := simultaneous.New [any ](max )
2634
35+ var stuckCalled atomic.Int32
36+ var unstuckCalled atomic.Int32
37+ someUnstuck := make (chan struct {})
38+ noteStuckCalled := sync .OnceFunc (func () {
39+ close (someUnstuck )
40+ })
41+
42+ if withStuck {
43+ limit = limit .SetForeverMessaging (time .Millisecond ,
44+ func () {
45+ stuckCalled .Add (1 )
46+ noteStuckCalled ()
47+ },
48+ func () {
49+ unstuckCalled .Add (1 )
50+ },
51+ )
52+ }
53+
2754 for i := 0 ; i < threadCount ; i ++ {
2855 i := i
2956 wg .Add (1 )
@@ -42,8 +69,17 @@ func TestLimit(t *testing.T) {
4269 assert .LessOrEqual (t , atomic .AddInt32 (& running , 1 ), int32 (max ))
4370 time .Sleep (sleep )
4471 assert .GreaterOrEqual (t , atomic .AddInt32 (& running , - 1 ), int32 (0 ))
72+ if withStuck {
73+ <- someUnstuck
74+ }
4575 done .Done ()
4676 }()
4777 }
4878 wg .Wait ()
79+
80+ if withStuck {
81+ t .Logf ("stuck called %d unstuck called %d" , stuckCalled .Load (), unstuckCalled .Load ())
82+ assert .NotZero (t , stuckCalled .Load (), "stuck reported" )
83+ assert .Equal (t , stuckCalled .Load (), unstuckCalled .Load (), "stuck == unstuck" )
84+ }
4985}
0 commit comments