Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,13 @@ def get_next_sub_queue(self) -> Optional[LinkedBlockingMultiQueue.SubQueue[T]]:
return None

def peek(self) -> Optional[T]:
start_idx = self.next_idx
idx = self.next_idx
while True:
child = self.queues[self.next_idx]
child = self.queues[idx]
if child.enabled and child.size() > 0:
return child.head.next.item
else:
self.next_idx += 1
if self.next_idx == len(self.queues):
self.next_idx = 0
if self.next_idx == start_idx:
idx = (idx + 1) % len(self.queues)
if idx == self.next_idx:
break
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def test_returns_none_when_the_only_non_empty_sub_queue_is_disabled(self, queue)
queue.disable("data")
assert queue.peek() is None

def test_priority_group_peek_perturbs_round_robin_state(self):
def test_priority_group_peek_preserves_round_robin_state(self):
lbmq = LinkedBlockingMultiQueue()
lbmq.add_sub_queue("first", 1)
lbmq.add_sub_queue("second", 1)
Expand All @@ -282,9 +282,10 @@ def test_priority_group_peek_perturbs_round_robin_state(self):

assert group.next_idx == 0
assert group.peek() == "s"
# Skipping the empty `first` queue advanced next_idx, so peek is not
# side-effect free on the group's round-robin cursor.
assert group.next_idx == 1
assert group.next_idx == 0

lbmq.put("first", "f")
assert lbmq.get() == "f"

def test_priority_group_peek_returns_none_when_all_queues_empty(self, queue):
group = queue.get_sub_queue("control").priority_group
Expand Down
Loading