阶段4:移交应用生命周期登记责任
This commit is contained in:
1 parent
d2d8e02a39
commit
2eeb37fd77
2 files changed
+31
-7
No files matched your search
@@ -196,6 +196,10 @@ func checkServerCompositionCalls(relativePath string, syntax *ast.File, fset *to
|
||||
"NewReconciliationRecoveryCoordinator": "订单补偿恢复协调器必须由 cmd 组合根构造后注入 Server",
|
||||
"NewDatabaseHealth": "数据库健康检查端口必须由 cmd 组合根构造后注入 Server",
|
||||
}
|
||||
// forbiddenMethods 记录 Server 不得再作为应用 worker 生命周期反向提供者的遗留方法。
|
||||
forbiddenMethods := map[string]string{
|
||||
"ApplicationLifecycleComponents": "应用 worker 生命周期组件必须由组合根的应用服务集合登记,Server 不得反向返还组件",
|
||||
}
|
||||
// violations 保存当前 Server 文件中发现的组合根回流问题。
|
||||
var violations []violation
|
||||
ast.Inspect(syntax, func(node ast.Node) bool {
|
||||
@@ -209,16 +213,18 @@ func checkServerCompositionCalls(relativePath string, syntax *ast.File, fset *to
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
// message 是命中构造函数后返回的组合根迁移提示。
|
||||
// message 是命中构造函数或遗留生命周期方法后返回的组合根迁移提示。
|
||||
message, forbidden := forbiddenConstructors[selector.Sel.Name]
|
||||
if !forbidden {
|
||||
return true
|
||||
message, forbidden = forbiddenMethods[selector.Sel.Name]
|
||||
}
|
||||
if forbidden {
|
||||
violations = append(violations, violation{
|
||||
file: normalizedPath,
|
||||
line: fset.Position(call.Pos()).Line,
|
||||
message: message,
|
||||
})
|
||||
}
|
||||
violations = append(violations, violation{
|
||||
file: normalizedPath,
|
||||
line: fset.Position(call.Pos()).Line,
|
||||
message: message,
|
||||
})
|
||||
return true
|
||||
})
|
||||
return violations
|
||||
|
||||
@@ -226,6 +226,24 @@ func accept(service any) { _ = service }
|
||||
}
|
||||
}
|
||||
|
||||
// TestServerLifecycleComponentBoundary 验证 Server 不能重新成为应用 worker 生命周期组件的反向提供者。
|
||||
func TestServerLifecycleComponentBoundary(t *testing.T) {
|
||||
// fset 是组合根迁移源码片段的统一位置集合。
|
||||
fset := token.NewFileSet()
|
||||
// syntax、parseErr 分别是错误调用遗留生命周期方法的模拟 Server 源码及解析错误。
|
||||
syntax, parseErr := parser.ParseFile(fset, "lifecycle.go", []byte(`package server
|
||||
func handler(server any) { server.ApplicationLifecycleComponents() }
|
||||
`), parser.ParseComments)
|
||||
if parseErr != nil {
|
||||
t.Fatal(parseErr)
|
||||
}
|
||||
// violations 是遗留生命周期反转必须触发的架构违规。
|
||||
violations := checkServerCompositionCalls("internal/server/lifecycle.go", syntax, fset)
|
||||
if len(violations) != 1 {
|
||||
t.Fatalf("violations=%+v", violations)
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicationTypeLeakBoundary 验证应用 Port 类型扫描会拒绝基础设施和 Server 类型。
|
||||
func TestApplicationTypeLeakBoundary(t *testing.T) {
|
||||
// fset 是测试源代码的文件位置集合。
|
||||
|
||||
Reference in new issue
Block a user