modify: 统一插件风格
This commit is contained in:
@@ -44,18 +44,19 @@ check("set 后 require 返回同一对象", gv.require("db") is marker)
|
||||
check("模块属性 db 已被更新", gv.db is marker)
|
||||
|
||||
|
||||
# ========== 2. __init__.py 导入链(router + auto_bind_models)==========
|
||||
# ========== 2. __init__.py 导入链(router + Plugin 基类)==========
|
||||
import plugins.example as plug
|
||||
|
||||
check("plugins.example 导入成功", True)
|
||||
check("create_workers 存在", callable(getattr(plug, "create_workers", None)))
|
||||
check("cleanup 存在", callable(getattr(plug, "cleanup", None)))
|
||||
check("_initialized 默认 False", plug._initialized is False)
|
||||
check("plugin 实例存在", getattr(plug, "plugin", None) is not None)
|
||||
check("plugin._initialized 默认 False", plug.plugin._initialized is False)
|
||||
|
||||
# ========== 3. create_workers 幂等性(mock session/config)==========
|
||||
import asyncio
|
||||
|
||||
# 用桩替代 _create_workers,避免真实 zenoh 会话
|
||||
# 用桩替代 Plugin 基类的 worker 创建实现,避免真实 zenoh 会话
|
||||
calls = []
|
||||
|
||||
|
||||
@@ -64,7 +65,7 @@ def _stub_create_workers(session, config, count=1, worker_manager=None):
|
||||
return ["task-stub"]
|
||||
|
||||
|
||||
plug._create_workers = _stub_create_workers # type: ignore[attr-defined]
|
||||
plug.plugin._create_workers_impl = _stub_create_workers # type: ignore[method-assign]
|
||||
|
||||
|
||||
async def main():
|
||||
@@ -73,7 +74,7 @@ async def main():
|
||||
# 第一次调用:应初始化 db
|
||||
t1 = await plug.create_workers(session="s1", config=cfg, count=2)
|
||||
check("首次 create_workers 调用桩", t1 == ["task-stub"])
|
||||
check("_initialized 置 True", plug._initialized is True)
|
||||
check("plugin._initialized 置 True", plug.plugin._initialized is True)
|
||||
from plugins.example.global_var import db as gv_db
|
||||
check("global_var.db 已初始化", gv_db is not None)
|
||||
|
||||
@@ -86,7 +87,7 @@ async def main():
|
||||
|
||||
# cleanup 后复位
|
||||
await plug.cleanup("worker/example/worker-i-1")
|
||||
check("cleanup 后 _initialized 复位 False", plug._initialized is False)
|
||||
check("cleanup 后 plugin._initialized 复位 False", plug.plugin._initialized is False)
|
||||
from plugins.example.global_var import db as gv_db3
|
||||
check("cleanup 后 db 为 None", gv_db3 is None)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user