Skip to content

Commit b0bb444

Browse files
committed
edit tests, add comments
1 parent 134a96a commit b0bb444

File tree

2 files changed

+77
-34
lines changed

2 files changed

+77
-34
lines changed

kpq/keyed_priority_queue.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func newKeyNotFoundError[K comparable](k K) error {
6060
// CmpFunc is a generic function type used for ordering the priority queue.
6161
type CmpFunc[V any] func(x, y V) bool
6262

63-
// TODO comment
63+
// pair is a pair of key and value that might be put or read from the queue.
6464
type pair[K comparable, V any] struct {
6565
k K
6666
v V
@@ -142,9 +142,8 @@ func (pq *KeyedPriorityQueue[K, V]) Pop() (K, V, bool) {
142142
return k, v, true
143143
}
144144

145-
// TODO BlockingPop
146-
// Pop removes and returns the highest priority key and value from the priority queue.
147-
// It returns false as its last return value if the priority queue is empty; otherwise, true.
145+
// BlockingPop removes and returns the highest priority key and value from the priority queue.
146+
// In case queue is empty, it blocks until next Push happens.
148147
func (pq *KeyedPriorityQueue[K, V]) BlockingPop() (K, V) {
149148
pq.mu.Lock()
150149

kpq/keyed_priority_queue_test.go

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func TestNewKeyedPriorityQueue_NilCmp(t *testing.T) {
1111
defer func() {
1212
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")
1414
}
1515
}()
1616

@@ -218,7 +218,7 @@ func TestKeyedPriorityQueue_Pop(t *testing.T) {
218218
t.Run(fmt.Sprintf("%s_%d", tc.wantKey, tc.wantValue), func(t *testing.T) {
219219
gotKey, gotValue, ok := pq.Pop()
220220
if !ok {
221-
t.Fatal("pq.Pop(): got unexpected empty prioriy queue")
221+
t.Fatal("pq.Pop(): got unexpected empty priority queue")
222222
}
223223

224224
if gotKey != tc.wantKey {
@@ -256,13 +256,13 @@ func TestKeyedPriorityQueue_Pop(t *testing.T) {
256256

257257
_, _, ok := pq.Pop()
258258
if ok {
259-
t.Errorf("pq.Pop(): got unexpected non-empty priorit queue")
259+
t.Errorf("pq.Pop(): got unexpected non-empty priority queue")
260260
}
261261
})
262262
}
263263

264264
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) {
266266
pq := NewKeyedPriorityQueue[string](func(x, y int) bool {
267267
return x < y
268268
})
@@ -279,7 +279,8 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
279279
}
280280

281281
go func() {
282-
time.Sleep(10 * time.Millisecond)
282+
time.Sleep(10 * time.Millisecond) // let reader block via BlockingPop()
283+
283284
for _, item := range items {
284285
err := pq.Push(item.key, item.val)
285286
if err != nil {
@@ -296,17 +297,17 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
296297
wantLen int
297298
}{
298299
{
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,
303304
wantLen: 4,
304305
},
305306
{
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,
310311
wantLen: 3,
311312
},
312313
}
@@ -323,7 +324,7 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
323324
t.Errorf("pq.BlockingPop(): got value %d; want %d", gotValue, tc.wantValue)
324325
}
325326

326-
time.Sleep(10 * time.Millisecond)
327+
time.Sleep(10 * time.Millisecond) // give queue a chance to process all the rest Push'es
327328

328329
gotPeekKey, gotPeekValue, ok := pq.Peek()
329330
if !ok {
@@ -345,7 +346,7 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
345346
}
346347
})
347348

348-
/*t.Run("EmptyPQ", func(t *testing.T) {
349+
t.Run("Do not actually block as queue is not empty", func(t *testing.T) {
349350
pq := NewKeyedPriorityQueue[string](func(x, y int) bool {
350351
return x < y
351352
})
@@ -361,24 +362,67 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
361362
{key: "last", val: 20},
362363
}
363364

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))
371369
}
372-
}()
370+
}
373371

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+
},
377393
}
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+
})
380424
}
381-
})*/
425+
})
382426
}
383427

384428
func TestKeyedPriorityQueue_Remove(t *testing.T) {
@@ -506,7 +550,7 @@ func TestKeyedPriorityQueue_PeekKey_EmptyQueue(t *testing.T) {
506550
}
507551
}
508552

509-
func TestKeyedPriorityQeue_Contains(t *testing.T) {
553+
func TestKeyedPriorityQueue_Contains(t *testing.T) {
510554
pq := NewKeyedPriorityQueue[string](func(x, y int) bool { return x < y })
511555

512556
k := "user"

0 commit comments

Comments
 (0)