@@ -6,41 +6,44 @@ package processor
66
77import (
88 "context"
9- wcf "github.com/Clov614/wcf-rpc-sdk"
109 "testing"
1110 "time"
1211
12+ wcf "github.com/Clov614/wcf-rpc-sdk"
13+
1314 "github.com/Clov614/rikka-bot-wechat/rikkabot/message"
1415 "github.com/Clov614/rikka-bot-wechat/rikkabot/plugins"
16+ "github.com/Clov614/rikka-bot-wechat/rikkabot/processor/cache"
1517)
1618
17- type pTestMatcher struct {
18- isThought bool
19- }
20-
21- func (m * pTestMatcher ) Match (ctx context.Context , msg * message.Message ) bool {
22- return m .isThought
23- }
24-
25- // TestPlugin 是一个真实的插件,用于测试
19+ // 定义一个专门用于测试的 TestPlugin 结构体
2620type TestPlugin struct {
27- * plugins.Plugin // 嵌入标准的 Plugin 结构
28- executeCount int
21+ * plugins.Plugin // 嵌入标准的 Plugin 结构
22+ executeCount int // 执行计数器
2923}
3024
25+ // AddExecCount 增加执行计数
3126func (p * TestPlugin ) AddExecCount (delta int ) {
3227 p .executeCount += delta
3328}
3429
3530// NewTestPlugin 创建一个新的 TestPlugin 实例
3631func NewTestPlugin (name string , level plugins.PluginLevel ) * TestPlugin {
3732 p := & TestPlugin {
38- Plugin : plugins .DefaultPlugin (name ).AsEnable (), // 使用默认的 Plugin 初始化 // 设置插件名称
33+ Plugin : plugins .DefaultPlugin (name ).AsEnable (), // 使用默认的 Plugin 初始化并启用
3934 }
4035 p .Plugin .PluginOpt .Level = level // 设置插件级别
4136 return p
4237}
4338
39+ type pTestMatcher struct {
40+ isThought bool
41+ }
42+
43+ func (m * pTestMatcher ) Match (ctx context.Context , msg * message.Message ) bool {
44+ return m .isThought
45+ }
46+
4447// TestProcessor_RegisterAndStart 测试插件注册和启动流程
4548func TestProcessor_RegisterAndStart (t * testing.T ) {
4649 plugin := NewTestPlugin ("test RegistAndStart-01" , plugins .MediumLevel )
@@ -153,3 +156,102 @@ func TestProcessor_MessageFlow(t *testing.T) {
153156 t .Logf ("Sending message: %s" , outputMsg .Content )
154157 }
155158}
159+
160+ // TestProcessor_CloseAndCache 测试 Processor 的 Close 方法、cache 的联动以及重启后的加载
161+ func TestProcessor_CloseAndCache (t * testing.T ) {
162+ // 准备阶段:创建并注册插件
163+ pluginName := "testCloseAndCache-01"
164+ plugin := NewTestPlugin (pluginName , plugins .MediumLevel )
165+ plugin .AsAction (& plugins.ActionHandler {
166+ Name : "test action" ,
167+ Matcher : & pTestMatcher {true },
168+ Action : func (ctx context.Context , recvMsg * message.Message ) (reply message.Message , ok bool , err error ) {
169+ plugin .AddExecCount (1 ) // 增加执行计数
170+ return * recvMsg , true , nil
171+ },
172+ })
173+ plugins .GetAutoRegister ().RegisterPlugin (plugin ) // 直接获取并注册
174+
175+ // 阶段 1: 首次运行并关闭,测试缓存
176+ func () {
177+ ctx , cancel := context .WithCancel (context .Background ())
178+ defer cancel ()
179+ inputChan := make (chan * message.Message )
180+ sendChan := make (chan * message.Message , 10 )
181+ processor := NewProcessor (ctx , wcf .NewClient (10 , false , false ))
182+
183+ processor .Start (inputChan , sendChan )
184+ inputChan <- & message.Message {Content : "test message" } // 触发插件执行
185+ <- time .After (time .Second * 1 ) // 等待执行
186+
187+ processor .Close () // 关闭并触发缓存
188+
189+ // 验证插件执行次数和缓存状态
190+ if plugin .executeCount != 1 {
191+ t .Errorf ("Expected plugin executeCount to be 1, got: %d" , plugin .executeCount )
192+ }
193+ cachedPlugin , ok := cache .GetCache ().GetPluginInfo (pluginName )
194+ if ! ok {
195+ t .Fatalf ("Plugin %s not found in cache" , pluginName )
196+ }
197+ // 验证缓存的是 PluginOpt
198+ cachedOpt , ok := cachedPlugin .(plugins.PluginOpt )
199+ if ! ok {
200+ t .Fatalf ("Expected cached plugin info to be PluginOpt, got: %T" , cachedPlugin )
201+ }
202+ if ! cachedOpt .Enable {
203+ t .Errorf ("Expected plugin %s to be enabled in cache" , pluginName )
204+ }
205+ }() // 使用匿名函数隔离作用域
206+
207+ // 阶段 2: 重启并验证加载
208+ func () {
209+ ctx , cancel := context .WithCancel (context .Background ())
210+ defer cancel ()
211+ inputChan := make (chan * message.Message )
212+ sendChan := make (chan * message.Message , 10 )
213+ processor := NewProcessor (ctx , wcf .NewClient (10 , false , false ))
214+
215+ // 注意:这里不需要重新注册插件,因为 AutoRegister 会从 cache 加载
216+ processor .Start (inputChan , sendChan )
217+
218+ // 验证插件已从 cache 加载
219+ registeredPlugins := processor .LevelLayer [plugins .MediumLevel ].GetPlugins ()
220+ // 遍历找到我们的测试插件
221+ var plugin2 * TestPlugin
222+ for _ , p := range registeredPlugins {
223+ if (* p ).GetName () == pluginName {
224+ var ok bool
225+ plugin2 , ok = (* p ).(* TestPlugin )
226+ if ! ok {
227+ t .Fatalf ("Expected plugin '%s' to be *TestPlugin, got: %T" , pluginName , * p )
228+ }
229+ break
230+ }
231+ }
232+
233+ if plugin2 == nil {
234+ t .Fatalf ("Plugin '%s' not found in registered plugins" , pluginName )
235+ }
236+
237+ // 从缓存中恢复
238+ cachedOpt , ok := cache .GetCache ().GetPluginInfo (pluginName )
239+ if ! ok {
240+ t .Fatalf ("Plugin %s not found in cache" , pluginName )
241+ }
242+ plugin2 .PluginOpt = cachedOpt .(plugins.PluginOpt )
243+
244+ if ! plugin2 .PluginOpt .Enable {
245+ t .Errorf ("Expected plugin %s to be enabled after restart" , pluginName )
246+ }
247+
248+ // 再次触发插件执行,验证其功能
249+ inputChan <- & message.Message {Content : "test message" }
250+ <- time .After (time .Second * 1 )
251+ if plugin2 .executeCount != 2 { // 执行次数应为 2
252+ t .Errorf ("Expected plugin executeCount to be 2, got: %d" , plugin2 .executeCount )
253+ }
254+
255+ processor .Close () // 关闭
256+ }() // 使用匿名函数隔离作用域
257+ }
0 commit comments