@@ -197,6 +197,66 @@ var _ = Describe("Controllerworkqueue", func() {
197
197
Expect (metrics .retries ["test" ]).To (Equal (1 ))
198
198
})
199
199
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
+
200
260
It ("returns an item to a waiter as soon as it has one" , func () {
201
261
q , metrics := newQueue ()
202
262
defer q .ShutDown ()
0 commit comments