package main import ( "flag" "strconv" "testing" ) // TestPort_Default 校验 userService 默认 Dubbo 端口与配置断言测试期望一致。 // // 该测试通过 flag.Lookup("port").DefValue 取 main.go 顶部 // // flag.Int("port", getEnvInt("PORT", 20000), "Dubbo service port") // // 注册时的默认值字符串。注意:PORT 环境变量会在包初始化时被读取,因此运行 // `go test` 前请确保 PORT 未设置(CI 默认为未设置)。 // // 配合 gateway/config/port_test.go 的 TestGateway_DefaultDubboURL_MatchesServiceBinds // 形成双端断言:任何一端默认值漂移都会立即失败。 func TestPort_Default(t *testing.T) { const want = 20000 f := flag.Lookup("port") if f == nil { t.Fatal("port flag 未注册(main.go 缺少 flag.Int(\"port\", ...))") } got, err := strconv.Atoi(f.DefValue) if err != nil { t.Fatalf("port DefValue=%q 解析失败: %v", f.DefValue, err) } if got != want { t.Errorf("userService 默认端口=%d, 期望=%d (PORT 环境变量可能影响,测试前请 unset)", got, want) } }