diff --git a/tests/test_tui_gateway_server.py b/tests/test_tui_gateway_server.py index afb38a1b9365..6122314eebd9 100644 --- a/tests/test_tui_gateway_server.py +++ b/tests/test_tui_gateway_server.py @@ -2765,13 +2765,26 @@ def test_run_prompt_submit_requeues_all_unstarted_notifications_with_real_thread assert nested_started.wait(timeout=5) threads[0].join(timeout=5) assert not threads[0].is_alive() - queued = [] - while not isolated_queue.empty(): - queued.append(isolated_queue.get_nowait()) - assert [event["session_id"] for event in queued] == [ + # Membership, not order: the completion_queue is process-global, and + # notification pollers leaked by earlier session.init tests in this + # file legitimately steal-and-requeue foreign-session events (see + # _notification_poller_loop's belongs-elsewhere branch), rotating the + # queue. The requeue contract is that batch_2 and batch_3 both remain + # queued (never consumed) while batch_1's turn is in flight — so drain + # with a deadline (an event may be transiently held by a poller + # mid-cycle) and assert exactly {batch_2, batch_3} come back. + queued: dict = {} + deadline = time.time() + 5.0 + while time.time() < deadline and set(queued) != { "proc_batch_2", "proc_batch_3", - ] + }: + try: + evt = isolated_queue.get(timeout=0.1) + except _queue_mod.Empty: + continue + queued[evt["session_id"]] = evt + assert set(queued) == {"proc_batch_2", "proc_batch_3"} finally: release_nested.set() for thread in threads: