"""事件总线测试:纯异步 handler 的调度、once 与 error 路由。""" from __future__ import annotations import asyncio import pytest from bot042.flows.bus import BusError, EventBus def test_emit_schedules_async_handlers() -> None: async def main() -> None: bus = EventBus() got: list[tuple[str, int]] = [] async def handler(name: str, n: int) -> None: await asyncio.sleep(0) got.append((name, n)) bus.on("evt", handler) assert bus.emit("evt", "a", 1) is True assert bus.complete is False await bus.wait_for_complete() assert got == [("a", 1)] assert bus.complete is True asyncio.run(main()) def test_once_handler_runs_single_time() -> None: async def main() -> None: bus = EventBus() calls: list[int] = [] async def handler(n: int) -> None: calls.append(n) bus.once("evt", handler) bus.emit("evt", 1) bus.emit("evt", 2) await bus.wait_for_complete() assert calls == [1] asyncio.run(main()) def test_handler_error_routed_to_error_event() -> None: async def main() -> None: bus = EventBus() errors: list[BaseException] = [] async def on_error(exc: BaseException) -> None: errors.append(exc) async def boom() -> None: raise ValueError("boom") bus.on("error", on_error) bus.on("go", boom) bus.emit("go") await bus.wait_for_complete() assert len(errors) == 1 assert isinstance(errors[0], ValueError) asyncio.run(main()) def test_sync_handler_rejected_at_registration() -> None: bus = EventBus() with pytest.raises(BusError): bus.on("ping", lambda: None) def test_unhandled_error_event_raises() -> None: bus = EventBus() with pytest.raises(ValueError): bus.emit("error", ValueError("x")) with pytest.raises(BusError): bus.emit("error", "not an exception") def test_handler_sync_failure_never_escapes_emit() -> None: """handler 调用时元数不匹配(同步 TypeError)不能从 emit 冒出来。""" async def main() -> None: bus = EventBus() async def no_args() -> None: pass bus.on("evt", no_args) assert bus.emit("evt", 1) is True # 没有 error 监听者也不抛 await bus.wait_for_complete() errors: list[BaseException] = [] async def on_error(exc: BaseException) -> None: errors.append(exc) bus.on("error", on_error) assert bus.emit("evt", 2) is True await bus.wait_for_complete() assert len(errors) == 1 assert isinstance(errors[0], TypeError) asyncio.run(main()) def test_publish_subscribe_aliases() -> None: async def main() -> None: bus = EventBus() got: list[int] = [] async def handler(n: int) -> None: got.append(n) bus.subscribe("evt", handler) assert bus.publish("evt", 1) is True await bus.wait_for_complete() assert got == [1] async def once_handler(n: int) -> None: got.append(n * 10) bus.subscribe_once("evt2", once_handler) bus.publish("evt2", 2) bus.publish("evt2", 3) await bus.wait_for_complete() assert got == [1, 20] asyncio.run(main()) def test_publish_threadsafe_same_thread() -> None: async def main() -> None: bus = EventBus() bus.bind_loop() got: list[int] = [] async def handler(n: int) -> None: got.append(n) bus.on("evt", handler) bus.publish_threadsafe("evt", 7) await bus.wait_for_complete() assert got == [7] asyncio.run(main()) def test_publish_threadsafe_cross_thread() -> None: import threading async def main() -> None: bus = EventBus() bus.bind_loop() got: list[int] = [] async def handler(n: int) -> None: got.append(n) bus.on("evt", handler) thread = threading.Thread(target=lambda: bus.publish_threadsafe("evt", 42)) thread.start() await asyncio.sleep(0.05) thread.join() await bus.wait_for_complete() assert got == [42] asyncio.run(main()) def test_publish_threadsafe_without_loop_raises() -> None: bus = EventBus() with pytest.raises(BusError, match="bind_loop"): bus.publish_threadsafe("evt", 1) def test_duplicate_handler_registration_rejected() -> None: """同一事件重复注册同一 handler 会静默覆盖并让 once 失效,注册时应报错。""" async def main() -> None: bus = EventBus() async def handler() -> None: pass bus.once("evt", handler) with pytest.raises(BusError): bus.on("evt", handler) asyncio.run(main())