feat: 插件自动挂载测试机制 — capabilities + self_test + 3阶段启动
- Plugin trait 增加 capabilities() 和 self_test() 方法 - PluginVTable 增加 get_capabilities 和 self_test FFI - ServiceManager 三阶段启动: init → self_test → start - SendCallback 改为 ctx 参数传递,消除 thread_local - export_plugin! 宏所有 FFI 函数包裹 catch_unwind - PluginManifest 增加 capabilities/required_capabilities/auto_test - 新增 3 个自测相关测试用例 (共 59 测试)
This commit is contained in:
@@ -1,18 +1,23 @@
|
||||
//! 示例动态插件 — 展示如何使用 showen-plugin-sdk 编写插件
|
||||
//!
|
||||
//! 此插件仅打印日志,用于验证动态加载流程。
|
||||
//! 此插件演示动态加载流程及自测机制。
|
||||
|
||||
use showen_plugin_sdk::{
|
||||
export_plugin, Message, MessageSender, PluginInfo, ShowenPlugin,
|
||||
export_plugin, CapabilityTestResult, Message, MessageSender, PluginInfo, ShowenPlugin,
|
||||
};
|
||||
|
||||
pub struct ExamplePlugin {
|
||||
sender: Option<MessageSender>,
|
||||
/// 用于演示可配置的自测失败
|
||||
fail_optional_test: bool,
|
||||
}
|
||||
|
||||
impl ExamplePlugin {
|
||||
pub fn new() -> Self {
|
||||
Self { sender: None }
|
||||
Self {
|
||||
sender: None,
|
||||
fail_optional_test: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +31,29 @@ impl ShowenPlugin for ExamplePlugin {
|
||||
}
|
||||
}
|
||||
|
||||
fn capabilities(&self) -> Vec<String> {
|
||||
vec!["logging".to_string(), "metrics".to_string()]
|
||||
}
|
||||
|
||||
fn self_test(&mut self) -> Vec<CapabilityTestResult> {
|
||||
vec![
|
||||
CapabilityTestResult {
|
||||
capability: "logging".to_string(),
|
||||
passed: true,
|
||||
message: "log output verified".to_string(),
|
||||
},
|
||||
CapabilityTestResult {
|
||||
capability: "metrics".to_string(),
|
||||
passed: !self.fail_optional_test,
|
||||
message: if self.fail_optional_test {
|
||||
"metrics backend unreachable".to_string()
|
||||
} else {
|
||||
"metrics endpoint ok".to_string()
|
||||
},
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn init(&mut self, config_json: &str, sender: MessageSender) -> Result<(), String> {
|
||||
eprintln!("[ExamplePlugin] init called, config length: {}", config_json.len());
|
||||
self.sender = Some(sender);
|
||||
|
||||
Reference in New Issue
Block a user