nacos-group / nacos-sdk-go

Nacos client in Golang
Apache License 2.0
1.14k stars 340 forks source link

运行单元测试时 GetService 只能返回IP不能返回正确的端口 #744

Open yangyile1990 opened 7 months ago

yangyile1990 commented 7 months ago

What version of nacos-sdk-go are you using?

github.com/nacos-group/nacos-sdk-go/v2 v2.2.5

What version of nacos-sever are you using?

docker run --name nacos-quick -e MODE=standalone -p 8848:8848 -p 9848:9848 -d nacos/nacos-server:v2.2.0

What version of Go are you using (go version)?

go version go1.22.2 darwin/arm64

What operating system (Linux, Windows, …) and version?

mac

What did you do?

If possible, provide a recipe for reproducing the error. code is:

service, err := client.GetService(vo.GetServiceParam{
    ServiceName: config.GlobalConfig.ApplicationName,
})

for _, host := range service.Hosts {
    var port = host.Port
    if port == 0 {
        return errors.New("port is zero")
    }
}

operation is: use the GoLand run testcase:

截屏2024-04-10 17 16 10

What did you expect to see?

port == 8080

What did you see instead?

port == 0

743

yangyile1990 commented 7 months ago

假如使用 go test -run TestNacosRoute_Online 和 go test -run TestNacosRoute_Offline 就万事大吉。

假如使用GoLand右键run test case 就会出错。

serverConfig := constant.NewServerConfig(ip, port)

clientConfig := constant.NewClientConfig(
    constant.WithEndpoint(endpoint),
    constant.WithNamespaceId(Namespace),
    constant.WithAppName(AppName),
)

client, err := clients.CreateNamingClient(map[string]interface{}{
    "serverConfigs": []constant.ServerConfig{*serverConfig},
    "clientConfig":  clientConfig,
})

service, err := client.GetService(vo.GetServiceParam{
    ServiceName: config.GlobalConfig.ApplicationName,
})

for _, host := range service.Hosts {
    var port = host.Port
    if port == 0 {
        return errors.New("port is zero")
    }
}

跟以前的 #743 是完全一样的,这导致我运行测试时非常不方便,你们可以下载最新的GoLand 或者前一个版本的GoLand,都是类似的问题。

binbin0325 commented 7 months ago

可以提供能复现的完整代码,包括注册实例的逻辑和获取实例