Skip to content

Commit e281397

Browse files
committed
Validate correct usage of btree and tick
1 parent 7122ca0 commit e281397

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

pkg/controller/priorityqueue/priorityqueue.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type priorityqueue[T comparable] struct {
8686
// or metrics.
8787
lock sync.Mutex
8888
items map[T]*item[T]
89-
queue *btree.BTreeG[*item[T]]
89+
queue bTree[*item[T]]
9090
metrics queueMetrics[T]
9191

9292
// addedCounter is a counter of elements added, we need it
@@ -325,3 +325,10 @@ func (w *priorityqueue[T]) updateUnfinishedWorkLoop() {
325325
w.metrics.updateUnfinishedWork()
326326
}
327327
}
328+
329+
type bTree[T any] interface {
330+
ReplaceOrInsert(item T) (_ T, _ bool)
331+
Delete(item T) (T, bool)
332+
Ascend(iterator btree.ItemIteratorG[T])
333+
Len() int
334+
}

pkg/controller/priorityqueue/priorityqueue_test.go

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

33
import (
4+
"fmt"
45
"sync"
56
"testing"
67
"time"
@@ -377,5 +378,38 @@ func newQueue() (PriorityQueue[string], *fakeMetricsProvider) {
377378
q := New("test", func(o *Opts[string]) {
378379
o.MetricProvider = metrics
379380
})
381+
q.(*priorityqueue[string]).queue = &btreeInteractionValidator{
382+
bTree: q.(*priorityqueue[string]).queue,
383+
}
384+
385+
upstreamTick := q.(*priorityqueue[string]).tick
386+
q.(*priorityqueue[string]).tick = func(d time.Duration) <-chan time.Time {
387+
if d <= 0 {
388+
panic(fmt.Sprintf("got non-positive tick: %v", d))
389+
}
390+
return upstreamTick(d)
391+
}
380392
return q, metrics
381393
}
394+
395+
type btreeInteractionValidator struct {
396+
bTree[*item[string]]
397+
}
398+
399+
func (b *btreeInteractionValidator) ReplaceOrInsert(item *item[string]) (*item[string], bool) {
400+
// There is no codepath that updates an item
401+
item, alreadyExist := b.bTree.ReplaceOrInsert(item)
402+
if alreadyExist {
403+
panic(fmt.Sprintf("ReplaceOrInsert: item %v already existed", item))
404+
}
405+
return item, alreadyExist
406+
}
407+
408+
func (b *btreeInteractionValidator) Delete(item *item[string]) (*item[string], bool) {
409+
// There is node codepath that deletes an item that doesn't exist
410+
old, existed := b.bTree.Delete(item)
411+
if !existed {
412+
panic(fmt.Sprintf("Delete: item %v not found", item))
413+
}
414+
return old, existed
415+
}

0 commit comments

Comments
 (0)