Skip to content

Commit

Permalink
priority queue: add unit test to reproduce panic
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Jan 3, 2025
1 parent 2c34bd6 commit 241602f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/controller/priorityqueue/priorityqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package priorityqueue

import (
"fmt"
"math/rand/v2"
"sync"
"testing"
"time"
Expand All @@ -13,6 +14,42 @@ import (
)

var _ = Describe("Controllerworkqueue", func() {

It("returns many items", func() {
// This test ensures the queue is able to drain a large queue without panic'ing.
// In a previous version of the code we were calling queue.Delete within q.Ascend
// which led to a panic in queue.Ascend > iterate:
// "panic: runtime error: index out of range [0] with length 0"

q, _ := newQueue()
defer q.ShutDown()

for range 20 {
for i := range 1000 {
rn := rand.N(100)

Check failure on line 29 in pkg/controller/priorityqueue/priorityqueue_test.go

View workflow job for this annotation

GitHub Actions / lint

G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand) (gosec)

Check failure on line 29 in pkg/controller/priorityqueue/priorityqueue_test.go

View workflow job for this annotation

GitHub Actions / lint

G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand) (gosec)
if rn < 10 {
q.AddWithOpts(AddOpts{After: time.Duration(rn) * time.Millisecond}, fmt.Sprintf("foo%d", i))
} else {
q.AddWithOpts(AddOpts{Priority: rn}, fmt.Sprintf("foo%d", i))
}
}

wg := sync.WaitGroup{}
for range 100 { // The panic only occurred relatively frequently with a high number of go routines.
wg.Add(1)
go func() {
defer wg.Done()
for range 10 {
obj, _, _ := q.GetWithPriority()
q.Done(obj)
}
}()
}

wg.Wait()
}
})

It("returns an item", func() {
q, metrics := newQueue()
defer q.ShutDown()
Expand Down

0 comments on commit 241602f

Please sign in to comment.