|
| 1 | +"""The two user-adjustable weixin_oc timeouts must not disable the HTTP timeout. |
| 2 | +
|
| 3 | +``weixin_oc_long_poll_timeout_ms`` and ``weixin_oc_api_timeout_ms`` are the only |
| 4 | +personal-WeChat fields the dashboard exposes for tuning (see |
| 5 | +``tests/test_weixin_oc_config_metadata.py``), and its numeric field writes ``0`` |
| 6 | +when the box is cleared (``toNumber`` maps ``parseFloat('')`` to ``0``). Aiohttp |
| 7 | +treats ``ClientTimeout(total=0)`` as "no timeout" -- measured here: a request that |
| 8 | +sleeps 0.4 s returns 200 with ``total=0`` and raises ``TimeoutError`` with |
| 9 | +``total=0.001`` -- so a cleared field leaves the receive loop waiting on a single |
| 10 | +stalled ``getupdates`` connection forever, which is exactly the case its |
| 11 | +``except asyncio.TimeoutError`` retry branch exists for. |
| 12 | +""" |
| 13 | + |
| 14 | +import asyncio |
| 15 | + |
| 16 | +from astrbot.core.platform.sources.weixin_oc.weixin_oc_adapter import ( |
| 17 | + WeixinOCAdapter, |
| 18 | +) |
| 19 | + |
| 20 | +DEFAULTS = { |
| 21 | + "weixin_oc_qr_poll_interval": 1, |
| 22 | + "weixin_oc_long_poll_timeout_ms": 35_000, |
| 23 | + "weixin_oc_api_timeout_ms": 120_000, |
| 24 | +} |
| 25 | +MINIMUM_TIMEOUT_MS = 1_000 |
| 26 | + |
| 27 | + |
| 28 | +def _adapter(**overrides) -> WeixinOCAdapter: |
| 29 | + config = {"id": "weixin_oc_test", **overrides} |
| 30 | + return WeixinOCAdapter(config, {"settings": {}}, asyncio.Queue()) |
| 31 | + |
| 32 | + |
| 33 | +def test_cleared_api_timeout_falls_back_to_a_positive_floor(): |
| 34 | + adapter = _adapter(weixin_oc_api_timeout_ms=0) |
| 35 | + assert adapter.api_timeout_ms == MINIMUM_TIMEOUT_MS |
| 36 | + # the value that actually reaches aiohttp.ClientTimeout(total=...) |
| 37 | + assert adapter.client.api_timeout_ms == MINIMUM_TIMEOUT_MS |
| 38 | + |
| 39 | + |
| 40 | +def test_cleared_long_poll_timeout_falls_back_to_a_positive_floor(): |
| 41 | + adapter = _adapter(weixin_oc_long_poll_timeout_ms=0) |
| 42 | + assert adapter.long_poll_timeout_ms == MINIMUM_TIMEOUT_MS |
| 43 | + |
| 44 | + |
| 45 | +def test_negative_timeouts_are_clamped_like_the_login_flow(): |
| 46 | + adapter = _adapter( |
| 47 | + weixin_oc_api_timeout_ms=-1, |
| 48 | + weixin_oc_long_poll_timeout_ms=-35_000, |
| 49 | + ) |
| 50 | + assert adapter.api_timeout_ms == MINIMUM_TIMEOUT_MS |
| 51 | + assert adapter.long_poll_timeout_ms == MINIMUM_TIMEOUT_MS |
| 52 | + |
| 53 | + |
| 54 | +def test_config_value_below_the_floor_still_clamped(): |
| 55 | + adapter = _adapter(weixin_oc_api_timeout_ms=200) |
| 56 | + assert adapter.api_timeout_ms == MINIMUM_TIMEOUT_MS |
| 57 | + |
| 58 | + |
| 59 | +def test_a_tuned_value_is_kept(): |
| 60 | + adapter = _adapter( |
| 61 | + weixin_oc_api_timeout_ms=5_000, |
| 62 | + weixin_oc_long_poll_timeout_ms=45_000, |
| 63 | + ) |
| 64 | + assert adapter.api_timeout_ms == 5_000 |
| 65 | + assert adapter.long_poll_timeout_ms == 45_000 |
| 66 | + |
| 67 | + |
| 68 | +def test_unset_fields_keep_the_documented_defaults(): |
| 69 | + adapter = _adapter() |
| 70 | + assert adapter.api_timeout_ms == DEFAULTS["weixin_oc_api_timeout_ms"] |
| 71 | + assert adapter.long_poll_timeout_ms == DEFAULTS["weixin_oc_long_poll_timeout_ms"] |
| 72 | + assert adapter.qr_poll_interval == DEFAULTS["weixin_oc_qr_poll_interval"] |
| 73 | + |
| 74 | + |
| 75 | +def test_a_non_numeric_field_does_not_kill_the_platform_adapter(): |
| 76 | + """PlatformManager.initialize only logs a bare `invalid literal ...`.""" |
| 77 | + adapter = _adapter( |
| 78 | + weixin_oc_api_timeout_ms="", |
| 79 | + weixin_oc_long_poll_timeout_ms=None, |
| 80 | + weixin_oc_qr_poll_interval="", |
| 81 | + ) |
| 82 | + assert adapter.api_timeout_ms == DEFAULTS["weixin_oc_api_timeout_ms"] |
| 83 | + assert adapter.long_poll_timeout_ms == DEFAULTS["weixin_oc_long_poll_timeout_ms"] |
| 84 | + assert adapter.qr_poll_interval == DEFAULTS["weixin_oc_qr_poll_interval"] |
0 commit comments