mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-10 12:32:03 +00:00
Don't panic when popping from empty scheduling queue
This commit is contained in:
parent
e90364f45d
commit
1f157bcb90
@ -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
|
// Swap implements swapping of two elements in the heap. This is a part of standard
|
||||||
// heap interface and should never be called directly.
|
// heap interface and should never be called directly.
|
||||||
func (h *data[T]) Swap(i, j int) {
|
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]
|
h.queue[i], h.queue[j] = h.queue[j], h.queue[i]
|
||||||
item := h.items[h.queue[i]]
|
item := h.items[h.queue[i]]
|
||||||
item.index = 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.
|
// Pop is supposed to be called by container/heap.Pop only.
|
||||||
func (h *data[T]) Pop() interface{} {
|
func (h *data[T]) Pop() interface{} {
|
||||||
|
if len(h.queue) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
key := h.queue[len(h.queue)-1]
|
key := h.queue[len(h.queue)-1]
|
||||||
h.queue = h.queue[0 : len(h.queue)-1]
|
h.queue = h.queue[0 : len(h.queue)-1]
|
||||||
item, ok := h.items[key]
|
item, ok := h.items[key]
|
||||||
|
@ -94,6 +94,11 @@ func TestHeapBasic(t *testing.T) {
|
|||||||
}
|
}
|
||||||
prevNum = num
|
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
|
// TestHeap_AddOrUpdate_Add tests add capabilities of Heap.AddOrUpdate
|
||||||
|
Loading…
Reference in New Issue
Block a user