diff --git a/amber/src/main/python/core/util/customized_queue/linked_blocking_multi_queue.py b/amber/src/main/python/core/util/customized_queue/linked_blocking_multi_queue.py index 561b1680cf5..f9c9c9dbfec 100644 --- a/amber/src/main/python/core/util/customized_queue/linked_blocking_multi_queue.py +++ b/amber/src/main/python/core/util/customized_queue/linked_blocking_multi_queue.py @@ -420,7 +420,8 @@ def add_sub_queue(self, key: K, priority: int) -> Optional[SubQueue]: elif pg.priority > priority: new_pg = self.PriorityGroup(priority) new_pg.add_queue(sub_queue) - self.priority_groups.append(new_pg) + # Insert before the higher-priority-value group to keep the list sorted. + self.priority_groups.insert(i, new_pg) added = True break diff --git a/amber/src/test/python/core/util/customized_queue/test_linked_blocking_multi_queue.py b/amber/src/test/python/core/util/customized_queue/test_linked_blocking_multi_queue.py index 8096972c1d4..238e5a23d56 100644 --- a/amber/src/test/python/core/util/customized_queue/test_linked_blocking_multi_queue.py +++ b/amber/src/test/python/core/util/customized_queue/test_linked_blocking_multi_queue.py @@ -520,3 +520,17 @@ def test_len_matches_size(self, queue): queue.put("data", 1) queue.put("control", "s") assert len(queue) == queue.size() == 2 + + def test_sub_queues_registered_out_of_priority_order_are_served_by_priority(self): + # Register the lower-priority (higher number) queue first; priority must still win. + lbmq = LinkedBlockingMultiQueue() + lbmq.add_sub_queue("data", 1) + lbmq.add_sub_queue("control", 0) + assert [pg.priority for pg in lbmq.sub_queue_selection.priority_groups] == [ + 0, + 1, + ] + lbmq.put("data", "d") + lbmq.put("control", "c") + assert lbmq.get() == "c" + assert lbmq.get() == "d"