Files
openbot fbb01c4c83 feat: 重构为 agent / channels / flows / db 分层架构
- agent: 新增 base.py 执行基类、execution.py 执行器、web.py 网络工具,
  middlewares/steering.py 中间件;移除旧的 agent.py
- channels: 新增 ChannelManager、registry、models 与 IM 渠道(企业微信),
  扩展 base.py
- flows: 新增 agent_task / bus / cron / scheduler / service / stream / rpc /
  processor / tasks / model_settings / builtin;移除旧的 task.py
- db: 新增 sqlite schema 与 repo 层
- docs: 新增架构说明、Channel × EventBus 设计、任务编写教程
- 依赖新增 aiosqlite / httpx / langgraph / primp / pydantic / ripgrep /
  wecom-aibot-sdk;新增 bot 命令入口
- 测试:232 passed
2026-11-09 20:32:25 +08:00

174 lines
4.4 KiB
Python

"""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())