mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
Caddy 式进程内模块系统(详见 docs/plugin-architecture/MODULE-AUTHOR-GUIDE.md): - internal/plugin:命名空间注册表(init 注册/重复 panic)、Provision→Validate→Start→Stop 生命周期(Start 半途逆序回滚、Stop 逆序+ctx deadline)、Host 四项 ports 能力面、 modules: 配置子树(enabled 三态;viper 含点 key 手工提取)、Runtime 状态机 - internal/plugin/plugintest:模块测试夹具(NewHost Options + RunLifecycle) - internal/modules:standard/imports.go 唯一插装清单 + job.hello 示例模块(默认 disabled) - Wire/main 接入:Build+Start 失败 fail-fast(显式 Cleanup 防 leader 锁泄漏)、 runtime.Stop 在 cleanup 并行组首位(先于 Redis/Ent) - admin 可观测:GET /api/v1/admin/modules(只读,错误文本脱敏) - 默认配置零行为变更(Phase-0 安全网 + bench allocs 零漂移验证) 含两轮对抗式审计修复(1×P1 启动失败泄漏 leader 锁 + 11×P2)与回归测试。
35 lines
1.0 KiB
Go
35 lines
1.0 KiB
Go
package hello
|
||
|
||
// Phase-1.5 对抗式审计(2026-06-11)B-1 回归测试:
|
||
// Stop 传入已取消的 ctx 时,若 worker 实际已干净退出,必须返回 nil
|
||
// (修复前 select 双臂就绪的随机选取可能把成功停止误报为失败)。
|
||
|
||
import (
|
||
"context"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/stretchr/testify/require"
|
||
|
||
"github.com/Wei-Shaw/sub2api/internal/plugin/plugintest"
|
||
)
|
||
|
||
func TestStopWithPreCancelledContextAfterWorkerExit(t *testing.T) {
|
||
m := new(Module)
|
||
require.NoError(t, m.Provision(plugintest.NewHost(t)))
|
||
require.NoError(t, m.Validate())
|
||
require.NoError(t, m.Start(context.Background()))
|
||
|
||
// 让 worker 先行退出并确认退出完成(直接驱动内部 cancel,规避计时竞态)。
|
||
m.cancel()
|
||
select {
|
||
case <-m.done:
|
||
case <-time.After(5 * time.Second):
|
||
t.Fatal("worker 未在期限内退出")
|
||
}
|
||
|
||
cancelled, cancel := context.WithCancel(context.Background())
|
||
cancel()
|
||
require.NoError(t, m.Stop(cancelled), "worker 已退出时,即便 ctx 已取消 Stop 也应返回 nil")
|
||
}
|