"""RpcHub 测试:往返、超时、远端异常、未注册方法、并发与中止。""" from __future__ import annotations import asyncio import pytest from bot042.flows.bus import EventBus from bot042.flows.rpc import ( RpcAborted, RpcError, RpcHub, RpcRemoteError, RpcTimeoutError, ) def test_rpc_roundtrip() -> None: async def main() -> None: bus = EventBus() hub = RpcHub(bus) async def add(payload: object) -> object: assert isinstance(payload, dict) return payload["a"] + payload["b"] hub.serve("math.add", add) assert hub.served() == ["math.add"] assert await hub.call("math.add", {"a": 1, "b": 2}) == 3 assert hub.pending == 0 hub.close() asyncio.run(main()) def test_rpc_remote_error_is_mapped() -> None: async def main() -> None: bus = EventBus() hub = RpcHub(bus) async def boom(payload: object) -> object: _ = payload raise ValueError("bad input") hub.serve("boom", boom) with pytest.raises(RpcRemoteError) as info: await hub.call("boom") assert info.value.remote_type == "ValueError" assert "bad input" in info.value.remote_message hub.close() asyncio.run(main()) def test_rpc_method_not_found() -> None: async def main() -> None: bus = EventBus() hub = RpcHub(bus) with pytest.raises(RpcError, match="未注册的 RPC 方法"): await hub.call("ghost") hub.close() asyncio.run(main()) def test_rpc_timeout_drops_late_reply() -> None: async def main() -> None: bus = EventBus() hub = RpcHub(bus) gate = asyncio.Event() async def slow(payload: object) -> object: await gate.wait() return payload hub.serve("slow", slow) with pytest.raises(RpcTimeoutError): await hub.call("slow", 1, timeout=0.01) assert hub.pending == 0 gate.set() # 迟到响应必须被丢弃,不能污染后续调用 await bus.wait_for_complete() assert hub.pending == 0 hub.close() asyncio.run(main()) def test_rpc_concurrent_calls() -> None: async def main() -> int: bus = EventBus() hub = RpcHub(bus) async def double(payload: object) -> object: await asyncio.sleep(0) assert isinstance(payload, int) return payload * 2 hub.serve("double", double) results = await asyncio.gather(*(hub.call("double", i) for i in range(50))) hub.close() return sum(1 for i, value in enumerate(results) if value == i * 2) assert asyncio.run(main()) == 50 def test_rpc_duplicate_serve_rejected() -> None: async def main() -> None: bus = EventBus() hub = RpcHub(bus) async def handler(payload: object) -> object: return payload hub.serve("m", handler) with pytest.raises(ValueError, match="已注册"): hub.serve("m", handler) hub.unserve("m") assert hub.served() == [] hub.close() asyncio.run(main()) def test_rpc_pending_limit() -> None: async def main() -> None: bus = EventBus() hub = RpcHub(bus, pending_limit=1) gate = asyncio.Event() async def slow(payload: object) -> object: await gate.wait() return payload hub.serve("slow", slow) first = asyncio.create_task(hub.call("slow", 1, timeout=None)) await asyncio.sleep(0) with pytest.raises(RpcError, match="上限"): await hub.call("slow", 2, timeout=None) gate.set() assert await first == 1 hub.close() await bus.wait_for_complete() asyncio.run(main()) def test_rpc_close_aborts_pending() -> None: async def main() -> None: bus = EventBus() hub = RpcHub(bus) gate = asyncio.Event() async def slow(payload: object) -> object: await gate.wait() return payload hub.serve("slow", slow) task = asyncio.create_task(hub.call("slow", 1, timeout=None)) await asyncio.sleep(0) hub.close() with pytest.raises(RpcAborted): await task gate.set() await bus.wait_for_complete() with pytest.raises(RpcAborted): await hub.call("slow", 2) asyncio.run(main())