Skip to content

Commit 241602f

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

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

pkg/controller/priorityqueue/priorityqueue_test.go

Lines changed: 37 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"
@@ -13,6 +14,42 @@ import (
1314
)
1415

1516
var _ = Describe("Controllerworkqueue", func() {
17+
18+
It("returns many items", func() {
19+
// This test ensures the queue is able to drain a large queue without panic'ing.
20+
// In a previous version of the code we were calling queue.Delete within q.Ascend
21+
// which led to a panic in queue.Ascend > iterate:
22+
// "panic: runtime error: index out of range [0] with length 0"
23+
24+
q, _ := newQueue()
25+
defer q.ShutDown()
26+
27+
for range 20 {
28+
for i := range 1000 {
29+
rn := rand.N(100)
30+
if rn < 10 {
31+
q.AddWithOpts(AddOpts{After: time.Duration(rn) * time.Millisecond}, fmt.Sprintf("foo%d", i))
32+
} else {
33+
q.AddWithOpts(AddOpts{Priority: rn}, fmt.Sprintf("foo%d", i))
34+
}
35+
}
36+
37+
wg := sync.WaitGroup{}
38+
for range 100 { // The panic only occurred relatively frequently with a high number of go routines.
39+
wg.Add(1)
40+
go func() {
41+
defer wg.Done()
42+
for range 10 {
43+
obj, _, _ := q.GetWithPriority()
44+
q.Done(obj)
45+
}
46+
}()
47+
}
48+
49+
wg.Wait()
50+
}
51+
})
52+
1653
It("returns an item", func() {
1754
q, metrics := newQueue()
1855
defer q.ShutDown()

0 commit comments

Comments
 (0)