Skip to content

Commit 8aa1eb4

Browse files
committed
priority queue: add unit test to reproduce panic
1 parent 2c34bd6 commit 8aa1eb4

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pkg/controller/priorityqueue/priorityqueue_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package priorityqueue
22

33
import (
44
"fmt"
5+
"math/rand/v2"
56
"sync"
67
"testing"
78
"time"
@@ -283,6 +284,41 @@ var _ = Describe("Controllerworkqueue", func() {
283284
Expect(metrics.depth["test"]).To(Equal(0))
284285
Expect(metrics.adds["test"]).To(Equal(2))
285286
})
287+
288+
It("returns many items", func() {
289+
// This test ensures the queue is able to drain a large queue without panic'ing.
290+
// In a previous version of the code we were calling queue.Delete within q.Ascend
291+
// which led to a panic in queue.Ascend > iterate:
292+
// "panic: runtime error: index out of range [0] with length 0"
293+
294+
q, _ := newQueue()
295+
defer q.ShutDown()
296+
297+
for range 20 {
298+
for i := range 1000 {
299+
rn := rand.N(100)
300+
if rn < 10 {
301+
q.AddWithOpts(AddOpts{After: time.Duration(rn) * time.Millisecond}, fmt.Sprintf("foo%d", i))
302+
} else {
303+
q.AddWithOpts(AddOpts{Priority: rn}, fmt.Sprintf("foo%d", i))
304+
}
305+
}
306+
307+
wg := sync.WaitGroup{}
308+
for range 100 { // The panic only occurred relatively frequently with a high number of go routines.
309+
wg.Add(1)
310+
go func() {
311+
defer wg.Done()
312+
for range 10 {
313+
obj, _, _ := q.GetWithPriority()
314+
q.Done(obj)
315+
}
316+
}()
317+
}
318+
319+
wg.Wait()
320+
}
321+
})
286322
})
287323

288324
func BenchmarkAddGetDone(b *testing.B) {

0 commit comments

Comments
 (0)