Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion astrbot/core/platform/sources/weixin_oc/weixin_oc_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,13 @@ async def run(self) -> None:
await asyncio.sleep(self.qr_poll_interval)
continue

await self._poll_inbound_updates()
try:
await self._poll_inbound_updates()
except asyncio.TimeoutError:
logger.debug(
"weixin_oc(%s): inbound long-poll timeout",
self.meta().id,
)
except asyncio.CancelledError:
raise
except Exception as e:
Expand Down
48 changes: 48 additions & 0 deletions tests/test_weixin_oc_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import asyncio
from unittest.mock import AsyncMock, patch

import pytest

from astrbot.core.platform.sources.weixin_oc.weixin_oc_adapter import WeixinOCAdapter


def _make_adapter() -> WeixinOCAdapter:
return WeixinOCAdapter(
platform_config={
"id": "weixin_main",
"type": "weixin_oc",
"weixin_oc_token": "test-token",
},
platform_settings={},
event_queue=asyncio.Queue(),
)


@pytest.mark.asyncio
async def test_run_keeps_polling_after_inbound_timeout():
adapter = _make_adapter()
adapter.client.close = AsyncMock()

calls = 0

async def fake_poll_inbound_updates():
nonlocal calls
calls += 1
if calls == 1:
raise asyncio.TimeoutError()
adapter._shutdown_event.set()

adapter._poll_inbound_updates = fake_poll_inbound_updates # type: ignore[method-assign]

with patch(
"astrbot.core.platform.sources.weixin_oc.weixin_oc_adapter.logger"
) as mock_logger:
await adapter.run()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve test robustness and clarity, consider using patch.object as a context manager for monkey-patching. This practice ensures that the original method is restored automatically after the test, even in case of errors, and it eliminates the need for a type: ignore comment.

    with patch.object(adapter, '_poll_inbound_updates', new=fake_poll_inbound_updates):
        with patch('astrbot.core.platform.sources.weixin_oc.weixin_oc_adapter.logger') as mock_logger:
            await adapter.run()


assert calls == 2
mock_logger.debug.assert_any_call(
"weixin_oc(%s): inbound long-poll timeout",
"weixin_main",
)
mock_logger.exception.assert_not_called()
adapter.client.close.assert_awaited_once()