@@ -10,7 +10,7 @@ import (
10
10
func TestNewKeyedPriorityQueue_NilCmp (t * testing.T ) {
11
11
defer func () {
12
12
if err := recover (); err == nil {
13
- t .Error ("want NewKeyedPriorityQueue to panic when receiving a nil comparison cunction " )
13
+ t .Error ("want NewKeyedPriorityQueue to panic when receiving a nil comparison function " )
14
14
}
15
15
}()
16
16
@@ -218,7 +218,7 @@ func TestKeyedPriorityQueue_Pop(t *testing.T) {
218
218
t .Run (fmt .Sprintf ("%s_%d" , tc .wantKey , tc .wantValue ), func (t * testing.T ) {
219
219
gotKey , gotValue , ok := pq .Pop ()
220
220
if ! ok {
221
- t .Fatal ("pq.Pop(): got unexpected empty prioriy queue" )
221
+ t .Fatal ("pq.Pop(): got unexpected empty priority queue" )
222
222
}
223
223
224
224
if gotKey != tc .wantKey {
@@ -256,13 +256,13 @@ func TestKeyedPriorityQueue_Pop(t *testing.T) {
256
256
257
257
_ , _ , ok := pq .Pop ()
258
258
if ok {
259
- t .Errorf ("pq.Pop(): got unexpected non-empty priorit queue" )
259
+ t .Errorf ("pq.Pop(): got unexpected non-empty priority queue" )
260
260
}
261
261
})
262
262
}
263
263
264
264
func TestKeyedPriorityQueue_BlockingPop (t * testing.T ) {
265
- t .Run ("Keys " , func (t * testing.T ) {
265
+ t .Run ("Block before pushing into queue " , func (t * testing.T ) {
266
266
pq := NewKeyedPriorityQueue [string ](func (x , y int ) bool {
267
267
return x < y
268
268
})
@@ -279,7 +279,8 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
279
279
}
280
280
281
281
go func () {
282
- time .Sleep (10 * time .Millisecond )
282
+ time .Sleep (10 * time .Millisecond ) // let reader block via BlockingPop()
283
+
283
284
for _ , item := range items {
284
285
err := pq .Push (item .key , item .val )
285
286
if err != nil {
@@ -296,17 +297,17 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
296
297
wantLen int
297
298
}{
298
299
{
299
- wantKey : "fourth" , //" first",
300
- wantValue : 10 , // 6,
301
- wantPeekKey : "first" , //"second",
302
- wantPeekValue : 6 , // 8,
300
+ wantKey : "fourth" , // this element was put first into queue, when reader was already blocked
301
+ wantValue : 10 ,
302
+ wantPeekKey : "first" , // this is an already sorted element in the queue
303
+ wantPeekValue : 6 ,
303
304
wantLen : 4 ,
304
305
},
305
306
{
306
- wantKey : "first" , //"second",
307
- wantValue : 6 , // 8,
308
- wantPeekKey : "second" , //"third",
309
- wantPeekValue : 8 , // 9,
307
+ wantKey : "first" ,
308
+ wantValue : 6 ,
309
+ wantPeekKey : "second" ,
310
+ wantPeekValue : 8 ,
310
311
wantLen : 3 ,
311
312
},
312
313
}
@@ -323,7 +324,7 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
323
324
t .Errorf ("pq.BlockingPop(): got value %d; want %d" , gotValue , tc .wantValue )
324
325
}
325
326
326
- time .Sleep (10 * time .Millisecond )
327
+ time .Sleep (10 * time .Millisecond ) // give queue a chance to process all the rest Push'es
327
328
328
329
gotPeekKey , gotPeekValue , ok := pq .Peek ()
329
330
if ! ok {
@@ -345,7 +346,7 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
345
346
}
346
347
})
347
348
348
- /* t.Run("EmptyPQ ", func(t *testing.T) {
349
+ t .Run ("Do not actually block as queue is not empty " , func (t * testing.T ) {
349
350
pq := NewKeyedPriorityQueue [string ](func (x , y int ) bool {
350
351
return x < y
351
352
})
@@ -361,24 +362,67 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
361
362
{key : "last" , val : 20 },
362
363
}
363
364
364
- go func() {
365
- time.Sleep(10 * time.Millisecond)
366
- for _, item := range items {
367
- err := pq.Push(item.key, item.val)
368
- if err != nil {
369
- panic(fmt.Sprintf("Push(%v, %v): got unexpected error %v", item.key, item.val, err))
370
- }
365
+ for _ , item := range items {
366
+ err := pq .Push (item .key , item .val )
367
+ if err != nil {
368
+ panic (fmt .Sprintf ("Push(%v, %v): got unexpected error %v" , item .key , item .val , err ))
371
369
}
372
- }()
370
+ }
373
371
374
- gotKey, gotValue := pq.BlockingPop()
375
- if gotKey != items[0].key {
376
- t.Errorf("pq.BlockingPop(): got key %q; want %q", gotKey, items[0].key)
372
+ testCases := []struct {
373
+ wantKey string
374
+ wantValue int
375
+ wantPeekKey string
376
+ wantPeekValue int
377
+ wantLen int
378
+ }{
379
+ {
380
+ wantKey : "first" ,
381
+ wantValue : 6 ,
382
+ wantPeekKey : "second" ,
383
+ wantPeekValue : 8 ,
384
+ wantLen : 4 ,
385
+ },
386
+ {
387
+ wantKey : "second" ,
388
+ wantValue : 8 ,
389
+ wantPeekKey : "third" ,
390
+ wantPeekValue : 9 ,
391
+ wantLen : 3 ,
392
+ },
377
393
}
378
- if gotValue != items[0].val {
379
- t.Errorf("pq.BlockingPop(): got value %d; want %d", gotValue, items[0].val)
394
+
395
+ for _ , tc := range testCases {
396
+ t .Run (fmt .Sprintf ("%s_%d" , tc .wantKey , tc .wantValue ), func (t * testing.T ) {
397
+ gotKey , gotValue := pq .BlockingPop ()
398
+
399
+ if gotKey != tc .wantKey {
400
+ t .Errorf ("pq.Pop(): got key %q; want %q" , gotKey , tc .wantKey )
401
+ }
402
+
403
+ if gotValue != tc .wantValue {
404
+ t .Errorf ("pq.Pop(): got value %d; want %d" , gotValue , tc .wantValue )
405
+ }
406
+
407
+ gotPeekKey , gotPeekValue , ok := pq .Peek ()
408
+ if ! ok {
409
+ t .Fatal ("got no min key and value in the priority queue" )
410
+ }
411
+
412
+ if gotPeekKey != tc .wantPeekKey {
413
+ t .Errorf ("pq.Peek(): got key %q; want %q" , gotPeekKey , tc .wantPeekKey )
414
+ }
415
+
416
+ if gotPeekValue != tc .wantPeekValue {
417
+ t .Errorf ("pq.Peek(): got value %d; want %d" , gotPeekValue , tc .wantPeekValue )
418
+ }
419
+
420
+ if got := pq .Len (); got != tc .wantLen {
421
+ t .Errorf ("pq.Len(): got %d; want %d" , got , tc .wantLen )
422
+ }
423
+ })
380
424
}
381
- })*/
425
+ })
382
426
}
383
427
384
428
func TestKeyedPriorityQueue_Remove (t * testing.T ) {
@@ -506,7 +550,7 @@ func TestKeyedPriorityQueue_PeekKey_EmptyQueue(t *testing.T) {
506
550
}
507
551
}
508
552
509
- func TestKeyedPriorityQeue_Contains (t * testing.T ) {
553
+ func TestKeyedPriorityQueue_Contains (t * testing.T ) {
510
554
pq := NewKeyedPriorityQueue [string ](func (x , y int ) bool { return x < y })
511
555
512
556
k := "user"
0 commit comments