Skip to content

Commit 7b8b24d

Browse files
committed
limiter: fix the blocking limiter busy loop
Signed-off-by: nolouch <[email protected]>
1 parent 493a30d commit 7b8b24d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

limiter/blocking.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func blockUntilSignaled(ctx context.Context, c *sync.Cond, timeout time.Duration
1818
go func() {
1919
c.L.Lock()
2020
defer c.L.Unlock()
21+
c.Wait()
2122
close(ready)
2223
}()
2324

limiter/blocking_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,31 @@ func TestBlockingLimiter(t *testing.T) {
268268
asrt.InDelta(4, sumReleased, 1.0, "expected roughly half to succeed")
269269
})
270270
}
271+
272+
func TestBlockUntilSignaled(t *testing.T) {
273+
asrt := assert.New(t)
274+
var mu sync.Mutex
275+
cond := sync.NewCond(&mu)
276+
277+
// Use a channel to control test completion
278+
done := make(chan struct{})
279+
280+
// Create a context for testing cancellation
281+
ctx, cancel := context.WithCancel(context.Background())
282+
283+
const waitTime = 50 * time.Millisecond
284+
go func() {
285+
time.Sleep(waitTime * 2)
286+
cond.Signal()
287+
close(done)
288+
}()
289+
290+
timeout := 500 * time.Millisecond
291+
now := time.Now()
292+
signalled := blockUntilSignaled(ctx, cond, timeout)
293+
asrt.True(signalled)
294+
asrt.True(time.Since(now) >= waitTime, "expected to wait at least 50ms")
295+
296+
<-done
297+
cancel()
298+
}

0 commit comments

Comments
 (0)