Skip to content

The agent that grows with you

test(telegram): regression tests for concurrent update processing

Yuan Li7766d4627fb4
diff --git a/tests/plugins/test_telegram_update_admission.py b/tests/plugins/test_telegram_update_admission.py
index da8e5a254d8..c7a00b67f30 100644
--- a/tests/plugins/test_telegram_update_admission.py
+++ b/tests/plugins/test_telegram_update_admission.py
@@ -791,3 +791,66 @@ async def test_redelivery_to_rebuilt_adapter_is_dropped(monkeypatch, tmp_path):
await app.process_update(update(app.bot, 30))
await asyncio.gather(*adapter._pending_text_batch_tasks.values())
assert len(delivered) == 1
+
+
+async def test_connect_builds_concurrent_update_processor(monkeypatch):
+ """The PTB Application must not run with the library default max_concurrent_updates=1:
+ the update fetcher then awaits each update's full handler chain inline, so one slow
+ update deafens every other chat (and local commands) until it finishes. The adapter
+ must enable a bounded concurrent processor, overridable via config extra (#125098)."""
+ async with connected(monkeypatch) as (adapter, app, delivered):
+ assert app.concurrent_updates > 1
+ assert app._update_processor.max_concurrent_updates == app.concurrent_updates
+
+ async with connected(monkeypatch, extra={"max_concurrent_updates": 7}) as (adapter, app, delivered):
+ assert app.concurrent_updates == 7
+
+
+async def test_slow_update_does_not_block_other_chats(monkeypatch):
+ """End-to-end through the real PTB fetcher path (update_queue → __update_fetcher):
+ while one chat's update is parked inside its handler chain, a different chat's
+ update must still be dequeued, dispatched and delivered (#125098). Under the PTB
+ default (max_concurrent_updates=1) the fetcher awaits each update inline, so the
+ second update is never dequeued while the first is parked."""
+ gate = asyncio.Event()
+
+ async with connected(monkeypatch) as (adapter, app, delivered):
+ async def gate_keeper(update, context):
+ if update.effective_chat and update.effective_chat.id == 42:
+ await gate.wait()
+
+ # Group -1 runs before every core handler group inside PTB's real dispatch.
+ app.add_handler(TypeHandler(Update, gate_keeper), group=-1)
+
+ app.update_queue.put_nowait(update(app.bot, uid=10, chat=42))
+ for _ in range(200): # event-based: u10 has been dequeued and parked in the gate
+ if adapter._inflight_update_ids:
+ break
+ await asyncio.sleep(0.01)
+ assert adapter._inflight_update_ids, "update 10 never started processing"
+
+ app.update_queue.put_nowait(update(app.bot, uid=11, chat=43))
+ dequeued = False
+ for _ in range(500):
+ if app.update_queue.qsize() == 0:
+ dequeued = True
+ break
+ await asyncio.sleep(0.01)
+ # THE regression discriminator: the fetcher must pull update 11 while update 10
+ # is still parked. Serial processing leaves it sitting in the queue.
+ assert dequeued, "fetcher never dequeued update 11 while update 10 was parked"
+
+ for _ in range(500): # u11 flows through batching to the session handler
+ if delivered:
+ break
+ await asyncio.sleep(0.01)
+ await asyncio.gather(*adapter._pending_text_batch_tasks.values())
+ assert [e.source.chat_id for e in delivered] == ["43"]
+ gate.set()
+ for _ in range(500): # released u10 finishes its chain and delivers too
+ if len(delivered) >= 2:
+ break
+ await asyncio.sleep(0.02)
+ assert [e.source.chat_id for e in delivered] == ["43", "42"]