Skip to content

Commit a1f3f66

Browse files
committed
add boolean return value
1 parent 1851bdc commit a1f3f66

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

kpq/keyed_priority_queue.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (pq *KeyedPriorityQueue[K, V]) Pop() (K, V, bool) {
145145

146146
// BlockingPop removes and returns the highest priority key and value from the priority queue.
147147
// In case queue is empty, it blocks until next Push happens or ctx is closed.
148-
func (pq *KeyedPriorityQueue[K, V]) BlockingPop(ctx context.Context) (K, V) {
148+
func (pq *KeyedPriorityQueue[K, V]) BlockingPop(ctx context.Context) (K, V, bool) {
149149
pq.mu.Lock()
150150

151151
if len(pq.pm) == 0 {
@@ -155,14 +155,15 @@ func (pq *KeyedPriorityQueue[K, V]) BlockingPop(ctx context.Context) (K, V) {
155155
case <-ctx.Done():
156156
var k K
157157
var v V
158-
return k, v
158+
return k, v, false
159159
case pair := <-pq.fastTrack:
160-
return pair.k, pair.v
160+
return pair.k, pair.v, true
161161
}
162162
}
163163

164164
defer pq.mu.Unlock()
165-
return pq.pop()
165+
k, v := pq.pop()
166+
return k, v, true
166167
}
167168

168169
func (pq *KeyedPriorityQueue[K, V]) pop() (K, V) {

kpq/keyed_priority_queue_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kpq
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"testing"
@@ -314,7 +315,7 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
314315

315316
for _, tc := range testCases {
316317
t.Run(fmt.Sprintf("%s_%d", tc.wantKey, tc.wantValue), func(t *testing.T) {
317-
gotKey, gotValue := pq.BlockingPop()
318+
gotKey, gotValue, _ := pq.BlockingPop(context.Background())
318319

319320
if gotKey != tc.wantKey {
320321
t.Errorf("pq.BlockingPop(): got key %q; want %q", gotKey, tc.wantKey)
@@ -394,7 +395,7 @@ func TestKeyedPriorityQueue_BlockingPop(t *testing.T) {
394395

395396
for _, tc := range testCases {
396397
t.Run(fmt.Sprintf("%s_%d", tc.wantKey, tc.wantValue), func(t *testing.T) {
397-
gotKey, gotValue := pq.BlockingPop()
398+
gotKey, gotValue, _ := pq.BlockingPop(context.Background())
398399

399400
if gotKey != tc.wantKey {
400401
t.Errorf("pq.Pop(): got key %q; want %q", gotKey, tc.wantKey)

0 commit comments

Comments
 (0)