Skip to content

Commit bd68c81

Browse files
moritzmoekstiehl
andcommitted
test: add test case
Co-authored-by: kstiehl <[email protected]>
1 parent 944ef63 commit bd68c81

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

pkg/controller/priorityqueue/priorityqueue_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,66 @@ var _ = Describe("Controllerworkqueue", func() {
197197
Expect(metrics.retries["test"]).To(Equal(1))
198198
})
199199

200+
It("returns high priority item that became ready before low priority items", func() {
201+
q, metrics := newQueue()
202+
defer q.ShutDown()
203+
204+
now := time.Now().Round(time.Second)
205+
nowLock := sync.Mutex{}
206+
tick := make(chan time.Time)
207+
208+
cwq := q.(*priorityqueue[string])
209+
cwq.now = func() time.Time {
210+
nowLock.Lock()
211+
defer nowLock.Unlock()
212+
return now
213+
}
214+
cwq.tick = func(d time.Duration) <-chan time.Time {
215+
Expect(d).To(Equal(time.Second))
216+
return tick
217+
}
218+
219+
retrievedItem := make(chan any)
220+
getNext := make(chan any)
221+
222+
go func() {
223+
defer GinkgoRecover()
224+
defer close(retrievedItem)
225+
226+
key, prio, _ := q.GetWithPriority()
227+
Expect(key).To(Equal("foo"))
228+
Expect(prio).To(Equal(-100))
229+
230+
retrievedItem <- nil
231+
<-getNext
232+
233+
key, prio, _ = q.GetWithPriority()
234+
Expect(key).To(Equal("prio"))
235+
Expect(prio).To(Equal(0))
236+
}()
237+
238+
lowPriority := -100
239+
highPriority := 0
240+
q.AddWithOpts(AddOpts{After: 0, Priority: &lowPriority}, "foo")
241+
q.AddWithOpts(AddOpts{After: 0, Priority: &lowPriority}, "bar")
242+
q.AddWithOpts(AddOpts{After: time.Second, Priority: &highPriority}, "prio")
243+
244+
<-retrievedItem
245+
246+
nowLock.Lock()
247+
now = now.Add(time.Second)
248+
nowLock.Unlock()
249+
tick <- now
250+
getNext <- nil
251+
252+
Eventually(retrievedItem).Should(BeClosed())
253+
close(getNext)
254+
255+
Expect(metrics.depth["test"]).To(Equal(map[int]int{-100: 1, 0: 0}))
256+
Expect(metrics.adds["test"]).To(Equal(3))
257+
Expect(metrics.retries["test"]).To(Equal(1))
258+
})
259+
200260
It("returns an item to a waiter as soon as it has one", func() {
201261
q, metrics := newQueue()
202262
defer q.ShutDown()

0 commit comments

Comments
 (0)