Merge pull request #127064 from macsko/dont_panic_when_scheduling_queue_empty

Don't panic when popping from empty scheduling queue
This commit is contained in:
Kubernetes Prow Robot 2024-09-04 15:05:46 +01:00 committed by GitHub
commit 2a8408811a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 0 deletions

View File

@ -85,6 +85,9 @@ func (h *data[T]) Len() int { return len(h.queue) }
// Swap implements swapping of two elements in the heap. This is a part of standard
// heap interface and should never be called directly.
func (h *data[T]) Swap(i, j int) {
if i < 0 || j < 0 {
return
}
h.queue[i], h.queue[j] = h.queue[j], h.queue[i]
item := h.items[h.queue[i]]
item.index = i
@ -102,6 +105,9 @@ func (h *data[T]) Push(kv interface{}) {
// Pop is supposed to be called by container/heap.Pop only.
func (h *data[T]) Pop() interface{} {
if len(h.queue) == 0 {
return nil
}
key := h.queue[len(h.queue)-1]
h.queue = h.queue[0 : len(h.queue)-1]
item, ok := h.items[key]

View File

@ -94,6 +94,11 @@ func TestHeapBasic(t *testing.T) {
}
prevNum = num
}
_, err := h.Pop()
if err == nil {
t.Errorf("expected Pop() to error on empty heap")
}
}
// TestHeap_AddOrUpdate_Add tests add capabilities of Heap.AddOrUpdate