topfans/backend/services/userService/port_test.go
zerosaturation a337f43f86 fix(config): port alignment + service test doubles (batch 4.1/4.5)
- gateway/config Dubbo URL 默认端口修正(原 20004/20005 与服务实际 20001/20004 不符);
- userService main.go 硬编码 WithPort(20000) 修死码, -port flag / PORT env 真正生效;
- .env.example 11 个 DUBBO URL 同步(对齐 serviceDefaultPorts map + 实际服务 bind);
- 删除 DUBBO_STARBOOK_SERVICE_URL 死引用(starbookService 已在 batch 4.2 删除);
- .env.example Service Ports 注释块补全 10 行, 与网关 Dubbo URL + port_test 三方一致;
- 10 个 service 各加 port_test.go, 断言 flag 默认与 serviceDefaultPorts map 一致;
- 11 个 port_test 总计 13 测试覆盖(网关 + 10 service);
- .gitignore 加 userService/main.go.bak 排除(临时备份)。

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-24 14:02:14 +08:00

34 lines
1.0 KiB
Go

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)
}
}