|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Unit tests for `telebot.async_telebot.AsyncTeleBot`. |
| 3 | +
|
| 4 | +These tests are self-contained (no TOKEN/CHAT_ID required) and stub out all |
| 5 | +network I/O. |
| 6 | +""" |
| 7 | +import asyncio |
| 8 | + |
| 9 | +from telebot import types |
| 10 | +from telebot.async_telebot import AsyncTeleBot |
| 11 | + |
| 12 | + |
| 13 | +def _make_fake_me() -> types.User: |
| 14 | + return types.User.de_json({ |
| 15 | + "id": 1, |
| 16 | + "is_bot": True, |
| 17 | + "first_name": "Test", |
| 18 | + "username": "test_bot", |
| 19 | + }) |
| 20 | + |
| 21 | + |
| 22 | +def test_process_polling_retains_update_processing_tasks(): |
| 23 | + """Regression test for issue #2572. |
| 24 | +
|
| 25 | + Tasks fired by `_process_polling` for `process_new_updates` must be held |
| 26 | + in `self._pending_tasks` while running and discarded on completion, so |
| 27 | + they cannot be garbage-collected mid-execution. |
| 28 | + """ |
| 29 | + bot = AsyncTeleBot("1:fake", validate_token=False) |
| 30 | + |
| 31 | + task_was_tracked_during_run: list[bool] = [] |
| 32 | + process_completed = asyncio.Event() |
| 33 | + |
| 34 | + async def fake_process_new_updates(updates): |
| 35 | + current = asyncio.current_task() |
| 36 | + task_was_tracked_during_run.append(current in bot._pending_tasks) |
| 37 | + process_completed.set() |
| 38 | + |
| 39 | + async def fake_get_me(): |
| 40 | + return _make_fake_me() |
| 41 | + |
| 42 | + # Deliver a single update batch, then stop polling on the next tick. |
| 43 | + fake_update = types.Update.de_json({"update_id": 1}) |
| 44 | + call_count = {"n": 0} |
| 45 | + |
| 46 | + async def fake_get_updates(*args, **kwargs): |
| 47 | + call_count["n"] += 1 |
| 48 | + if call_count["n"] == 1: |
| 49 | + return [fake_update] |
| 50 | + bot._polling = False |
| 51 | + return [] |
| 52 | + |
| 53 | + async def noop(): |
| 54 | + return None |
| 55 | + |
| 56 | + bot.get_me = fake_get_me |
| 57 | + bot.get_updates = fake_get_updates |
| 58 | + bot.process_new_updates = fake_process_new_updates |
| 59 | + bot.close_session = noop # stub: no real aiohttp session in tests |
| 60 | + |
| 61 | + async def driver(): |
| 62 | + await bot._process_polling(non_stop=True, interval=0, timeout=0) |
| 63 | + # Allow the fire-and-forget task to finish plus one yield for the |
| 64 | + # add_done_callback discard to run. A timeout guards against the |
| 65 | + # stub ever being rewired such that the processing task never runs. |
| 66 | + await asyncio.wait_for(process_completed.wait(), timeout=1) |
| 67 | + await asyncio.sleep(0) |
| 68 | + |
| 69 | + asyncio.run(driver()) |
| 70 | + |
| 71 | + assert task_was_tracked_during_run == [True], ( |
| 72 | + "In-flight processing task must be held by _pending_tasks" |
| 73 | + ) |
| 74 | + assert bot._pending_tasks == set(), ( |
| 75 | + "Completed processing tasks must be discarded from _pending_tasks" |
| 76 | + ) |
0 commit comments