|
|
|
@ -2,8 +2,7 @@ package nacos |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"log" |
|
|
|
|
"net" |
|
|
|
|
"reflect" |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
@ -14,29 +13,9 @@ import ( |
|
|
|
|
"github.com/go-kratos/kratos/v2/registry" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func getIntranetIP() string { |
|
|
|
|
addrs, err := net.InterfaceAddrs() |
|
|
|
|
if err != nil { |
|
|
|
|
return "127.0.0.1" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, address := range addrs { |
|
|
|
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { |
|
|
|
|
if ipnet.IP.To4() != nil { |
|
|
|
|
return ipnet.IP.String() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return "127.0.0.1" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestRegistry(t *testing.T) { |
|
|
|
|
ip := getIntranetIP() |
|
|
|
|
serviceName := "golang-sms@grpc" |
|
|
|
|
ctx := context.Background() |
|
|
|
|
|
|
|
|
|
func TestRegistry_Register(t *testing.T) { |
|
|
|
|
sc := []constant.ServerConfig{ |
|
|
|
|
*constant.NewServerConfig(ip, 8848), |
|
|
|
|
*constant.NewServerConfig("127.0.0.1", 8848), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cc := constant.ClientConfig{ |
|
|
|
@ -60,76 +39,298 @@ func TestRegistry(t *testing.T) { |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
} |
|
|
|
|
r := New(client) |
|
|
|
|
|
|
|
|
|
_, err = client.RegisterInstance(vo.RegisterInstanceParam{ |
|
|
|
|
Ip: "f", |
|
|
|
|
Port: 8840, |
|
|
|
|
ServiceName: serviceName, |
|
|
|
|
Weight: 10, |
|
|
|
|
Enable: true, |
|
|
|
|
Healthy: true, |
|
|
|
|
Ephemeral: true, |
|
|
|
|
Metadata: map[string]string{"idc": "shanghai-xs"}, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
testServer := ®istry.ServiceInstance{ |
|
|
|
|
ID: "1", |
|
|
|
|
Name: "test1", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Endpoints: []string{"http://127.0.0.1:8080?isSecure=false"}, |
|
|
|
|
} |
|
|
|
|
testServerWithMetadata := ®istry.ServiceInstance{ |
|
|
|
|
ID: "1", |
|
|
|
|
Name: "test1", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Endpoints: []string{"http://127.0.0.1:8080?isSecure=false"}, |
|
|
|
|
Metadata: map[string]string{"idc": "shanghai-xs"}, |
|
|
|
|
} |
|
|
|
|
type fields struct { |
|
|
|
|
registry *Registry |
|
|
|
|
} |
|
|
|
|
type args struct { |
|
|
|
|
ctx context.Context |
|
|
|
|
service *registry.ServiceInstance |
|
|
|
|
} |
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
|
fields fields |
|
|
|
|
args args |
|
|
|
|
wantErr bool |
|
|
|
|
deferFunc func(t *testing.T) |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "normal", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: New(client), |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: testServer, |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
deferFunc: func(t *testing.T) { |
|
|
|
|
err = r.Deregister(context.Background(), testServer) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Error(err) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "withMetadata", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: New(client), |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: testServerWithMetadata, |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
deferFunc: func(t *testing.T) { |
|
|
|
|
err = r.Deregister(context.Background(), testServerWithMetadata) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Error(err) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "error", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: New(client), |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: ®istry.ServiceInstance{ |
|
|
|
|
ID: "1", |
|
|
|
|
Name: "", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Endpoints: []string{"http://127.0.0.1:8080?isSecure=false"}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
wantErr: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "urlError", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: New(client), |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: ®istry.ServiceInstance{ |
|
|
|
|
ID: "1", |
|
|
|
|
Name: "test", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Endpoints: []string{"127.0.0.1:8080"}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
wantErr: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "portError", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: New(client), |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: ®istry.ServiceInstance{ |
|
|
|
|
ID: "1", |
|
|
|
|
Name: "test", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Endpoints: []string{"http://127.0.0.1888"}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
wantErr: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "withCluster", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: New(client, WithCluster("test")), |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: testServer, |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "withGroup", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: New(client, WithGroup("TEST_GROUP")), |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: testServer, |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "withWeight", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: New(client, WithWeight(200)), |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: testServer, |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "withPrefix", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: New(client, WithPrefix("test")), |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: testServer, |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
|
|
|
r := tt.fields.registry |
|
|
|
|
if err := r.Register(tt.args.ctx, tt.args.service); (err != nil) != tt.wantErr { |
|
|
|
|
t.Errorf("Register error = %v, wantErr %v", err, tt.wantErr) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
func TestRegistry_Deregister(t *testing.T) { |
|
|
|
|
testServer := ®istry.ServiceInstance{ |
|
|
|
|
ID: "1", |
|
|
|
|
Name: "test2", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Endpoints: []string{"http://127.0.0.1:8080?isSecure=false"}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
is, err := client.GetService(vo.GetServiceParam{ |
|
|
|
|
ServiceName: serviceName, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
type args struct { |
|
|
|
|
ctx context.Context |
|
|
|
|
service *registry.ServiceInstance |
|
|
|
|
} |
|
|
|
|
t.Logf("is %#v", is) |
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
|
args args |
|
|
|
|
wantErr bool |
|
|
|
|
preFunc func(t *testing.T) |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "normal", |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: testServer, |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
preFunc: func(t *testing.T) { |
|
|
|
|
sc := []constant.ServerConfig{ |
|
|
|
|
*constant.NewServerConfig("127.0.0.1", 8848), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
r := New(client) |
|
|
|
|
cc := constant.ClientConfig{ |
|
|
|
|
NamespaceId: "public", // namespace id
|
|
|
|
|
TimeoutMs: 5000, |
|
|
|
|
NotLoadCacheAtStart: true, |
|
|
|
|
LogDir: "/tmp/nacos/log", |
|
|
|
|
CacheDir: "/tmp/nacos/cache", |
|
|
|
|
RotateTime: "1h", |
|
|
|
|
MaxAge: 3, |
|
|
|
|
LogLevel: "debug", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
|
var ( |
|
|
|
|
w registry.Watcher |
|
|
|
|
watchErr error |
|
|
|
|
) |
|
|
|
|
w, watchErr = r.Watch(ctx, "golang-sms@grpc") |
|
|
|
|
if watchErr != nil { |
|
|
|
|
log.Fatal(watchErr) |
|
|
|
|
} |
|
|
|
|
for { |
|
|
|
|
var res []*registry.ServiceInstance |
|
|
|
|
res, watchErr = w.Next() |
|
|
|
|
if watchErr != nil { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
log.Printf("watch: %d", len(res)) |
|
|
|
|
for _, r := range res { |
|
|
|
|
log.Printf("next: %+v", r) |
|
|
|
|
// a more graceful way to create naming client
|
|
|
|
|
client, err := clients.NewNamingClient( |
|
|
|
|
vo.NacosClientParam{ |
|
|
|
|
ClientConfig: &cc, |
|
|
|
|
ServerConfigs: sc, |
|
|
|
|
}, |
|
|
|
|
) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
} |
|
|
|
|
r := New(client) |
|
|
|
|
err = r.Register(context.Background(), testServer) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Error(err) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "error", |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: ®istry.ServiceInstance{ |
|
|
|
|
ID: "1", |
|
|
|
|
Name: "test", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Endpoints: []string{"127.0.0.1:8080"}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
wantErr: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "errorPort", |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
service: ®istry.ServiceInstance{ |
|
|
|
|
ID: "1", |
|
|
|
|
Name: "notExist", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Endpoints: []string{"http://127.0.0.18080"}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
wantErr: true, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
|
|
|
sc := []constant.ServerConfig{ |
|
|
|
|
*constant.NewServerConfig("127.0.0.1", 8848), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
cc := constant.ClientConfig{ |
|
|
|
|
NamespaceId: "public", // namespace id
|
|
|
|
|
TimeoutMs: 5000, |
|
|
|
|
NotLoadCacheAtStart: true, |
|
|
|
|
LogDir: "/tmp/nacos/log", |
|
|
|
|
CacheDir: "/tmp/nacos/cache", |
|
|
|
|
RotateTime: "1h", |
|
|
|
|
MaxAge: 3, |
|
|
|
|
LogLevel: "debug", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ins, err := r.GetService(ctx, serviceName) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
} |
|
|
|
|
for _, in := range ins { |
|
|
|
|
t.Logf("ins: %#v", in) |
|
|
|
|
// a more graceful way to create naming client
|
|
|
|
|
client, err := clients.NewNamingClient( |
|
|
|
|
vo.NacosClientParam{ |
|
|
|
|
ClientConfig: &cc, |
|
|
|
|
ServerConfigs: sc, |
|
|
|
|
}, |
|
|
|
|
) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
} |
|
|
|
|
r := New(client) |
|
|
|
|
if tt.preFunc != nil { |
|
|
|
|
tt.preFunc(t) |
|
|
|
|
} |
|
|
|
|
if err := r.Deregister(tt.args.ctx, tt.args.service); (err != nil) != tt.wantErr { |
|
|
|
|
t.Errorf("Deregister error = %v, wantErr %v", err, tt.wantErr) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestRegistryMany(t *testing.T) { |
|
|
|
|
ip := getIntranetIP() |
|
|
|
|
serviceName := "golang-sms@grpc" |
|
|
|
|
// ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
func TestRegistry_GetService(t *testing.T) { |
|
|
|
|
sc := []constant.ServerConfig{ |
|
|
|
|
*constant.NewServerConfig(ip, 8848), |
|
|
|
|
*constant.NewServerConfig("127.0.0.1", 8848), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cc := constant.ClientConfig{ |
|
|
|
@ -153,61 +354,219 @@ func TestRegistryMany(t *testing.T) { |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_, err = client.RegisterInstance(vo.RegisterInstanceParam{ |
|
|
|
|
Ip: "f1", |
|
|
|
|
Port: 8840, |
|
|
|
|
ServiceName: serviceName, |
|
|
|
|
Weight: 10, |
|
|
|
|
Enable: true, |
|
|
|
|
Healthy: true, |
|
|
|
|
Ephemeral: true, |
|
|
|
|
Metadata: map[string]string{"idc": "shanghai-xs"}, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
r := New(client) |
|
|
|
|
testServer := ®istry.ServiceInstance{ |
|
|
|
|
ID: "1", |
|
|
|
|
Name: "test3", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Endpoints: []string{"grpc://127.0.0.1:8080?isSecure=false"}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_, err = client.RegisterInstance(vo.RegisterInstanceParam{ |
|
|
|
|
Ip: "f2", |
|
|
|
|
Port: 8840, |
|
|
|
|
ServiceName: serviceName, |
|
|
|
|
Weight: 10, |
|
|
|
|
Enable: true, |
|
|
|
|
Healthy: true, |
|
|
|
|
Ephemeral: true, |
|
|
|
|
Metadata: map[string]string{"idc": "shanghai-xs"}, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
type fields struct { |
|
|
|
|
registry *Registry |
|
|
|
|
} |
|
|
|
|
type args struct { |
|
|
|
|
ctx context.Context |
|
|
|
|
serviceName string |
|
|
|
|
} |
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
|
fields fields |
|
|
|
|
args args |
|
|
|
|
want []*registry.ServiceInstance |
|
|
|
|
wantErr bool |
|
|
|
|
preFunc func(t *testing.T) |
|
|
|
|
deferFunc func(t *testing.T) |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "normal", |
|
|
|
|
preFunc: func(t *testing.T) { |
|
|
|
|
err = r.Register(context.Background(), testServer) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Error(err) |
|
|
|
|
} |
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
}, |
|
|
|
|
deferFunc: func(t *testing.T) { |
|
|
|
|
err = r.Deregister(context.Background(), testServer) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Error(err) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: r, |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
serviceName: testServer.Name + "." + "grpc", |
|
|
|
|
}, |
|
|
|
|
want: []*registry.ServiceInstance{{ |
|
|
|
|
ID: "127.0.0.1#8080#DEFAULT#DEFAULT_GROUP@@test3.grpc", |
|
|
|
|
Name: "DEFAULT_GROUP@@test3.grpc", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Metadata: map[string]string{"version": "v1.0.0", "kind": "grpc"}, |
|
|
|
|
Endpoints: []string{"grpc://127.0.0.1:8080"}, |
|
|
|
|
}}, |
|
|
|
|
wantErr: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "errorNotExist", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: r, |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
serviceName: "notExist", |
|
|
|
|
}, |
|
|
|
|
want: nil, |
|
|
|
|
wantErr: true, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
|
|
|
if tt.preFunc != nil { |
|
|
|
|
tt.preFunc(t) |
|
|
|
|
} |
|
|
|
|
if tt.deferFunc != nil { |
|
|
|
|
defer tt.deferFunc(t) |
|
|
|
|
} |
|
|
|
|
r := tt.fields.registry |
|
|
|
|
got, err := r.GetService(tt.args.ctx, tt.args.serviceName) |
|
|
|
|
if (err != nil) != tt.wantErr { |
|
|
|
|
t.Errorf("GetService error = %v, wantErr %v", err, tt.wantErr) |
|
|
|
|
t.Errorf("GetService got = %v", got) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if !reflect.DeepEqual(got, tt.want) { |
|
|
|
|
t.Errorf("GetService got = %v, want %v", got, tt.want) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_, err = client.RegisterInstance(vo.RegisterInstanceParam{ |
|
|
|
|
Ip: "f3", |
|
|
|
|
Port: 8840, |
|
|
|
|
ServiceName: serviceName, |
|
|
|
|
Weight: 10, |
|
|
|
|
Enable: true, |
|
|
|
|
Healthy: true, |
|
|
|
|
Ephemeral: true, |
|
|
|
|
Metadata: map[string]string{"idc": "shanghai-xs"}, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
func TestRegistry_Watch(t *testing.T) { |
|
|
|
|
sc := []constant.ServerConfig{ |
|
|
|
|
*constant.NewServerConfig("127.0.0.1", 8848), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
cc := constant.ClientConfig{ |
|
|
|
|
NamespaceId: "public", // namespace id
|
|
|
|
|
TimeoutMs: 5000, |
|
|
|
|
NotLoadCacheAtStart: true, |
|
|
|
|
LogDir: "/tmp/nacos/log", |
|
|
|
|
CacheDir: "/tmp/nacos/cache", |
|
|
|
|
RotateTime: "1h", |
|
|
|
|
MaxAge: 3, |
|
|
|
|
LogLevel: "debug", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
is, err := client.GetService(vo.GetServiceParam{ |
|
|
|
|
ServiceName: serviceName, |
|
|
|
|
}) |
|
|
|
|
// a more graceful way to create naming client
|
|
|
|
|
client, err := clients.NewNamingClient( |
|
|
|
|
vo.NacosClientParam{ |
|
|
|
|
ClientConfig: &cc, |
|
|
|
|
ServerConfigs: sc, |
|
|
|
|
}, |
|
|
|
|
) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatal(err) |
|
|
|
|
} |
|
|
|
|
r := New(client) |
|
|
|
|
|
|
|
|
|
for _, host := range is.Hosts { |
|
|
|
|
t.Logf("host: %#v,e: %v", host, err) |
|
|
|
|
testServer := ®istry.ServiceInstance{ |
|
|
|
|
ID: "1", |
|
|
|
|
Name: "test4", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Endpoints: []string{"grpc://127.0.0.1:8080?isSecure=false"}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
cancelCtx, cancel := context.WithCancel(context.Background()) |
|
|
|
|
type fields struct { |
|
|
|
|
registry *Registry |
|
|
|
|
} |
|
|
|
|
type args struct { |
|
|
|
|
ctx context.Context |
|
|
|
|
serviceName string |
|
|
|
|
} |
|
|
|
|
tests := []struct { |
|
|
|
|
name string |
|
|
|
|
fields fields |
|
|
|
|
args args |
|
|
|
|
wantErr bool |
|
|
|
|
want []*registry.ServiceInstance |
|
|
|
|
processFunc func(t *testing.T) |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "normal", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: New(client), |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: context.Background(), |
|
|
|
|
serviceName: testServer.Name + "." + "grpc", |
|
|
|
|
}, |
|
|
|
|
wantErr: false, |
|
|
|
|
want: []*registry.ServiceInstance{{ |
|
|
|
|
ID: "127.0.0.1#8080#DEFAULT#DEFAULT_GROUP@@test4.grpc", |
|
|
|
|
Name: "DEFAULT_GROUP@@test4.grpc", |
|
|
|
|
Version: "v1.0.0", |
|
|
|
|
Metadata: map[string]string{"version": "v1.0.0", "kind": "grpc"}, |
|
|
|
|
Endpoints: []string{"grpc://127.0.0.1:8080"}, |
|
|
|
|
}}, |
|
|
|
|
processFunc: func(t *testing.T) { |
|
|
|
|
err = r.Register(context.Background(), testServer) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Error(err) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "ctxCancel", |
|
|
|
|
fields: fields{ |
|
|
|
|
registry: r, |
|
|
|
|
}, |
|
|
|
|
args: args{ |
|
|
|
|
ctx: cancelCtx, |
|
|
|
|
serviceName: testServer.Name, |
|
|
|
|
}, |
|
|
|
|
wantErr: true, |
|
|
|
|
want: nil, |
|
|
|
|
processFunc: func(t *testing.T) { |
|
|
|
|
cancel() |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, tt := range tests { |
|
|
|
|
t.Run(tt.name, func(t *testing.T) { |
|
|
|
|
r := tt.fields.registry |
|
|
|
|
watch, err := r.Watch(tt.args.ctx, tt.args.serviceName) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Error(err) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
defer func() { |
|
|
|
|
err = watch.Stop() |
|
|
|
|
if err != nil { |
|
|
|
|
t.Error(err) |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
_, err = watch.Next() |
|
|
|
|
if err != nil { |
|
|
|
|
t.Error(err) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if tt.processFunc != nil { |
|
|
|
|
tt.processFunc(t) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
want, err := watch.Next() |
|
|
|
|
if (err != nil) != tt.wantErr { |
|
|
|
|
t.Errorf("Watch error = %v, wantErr %v", err, tt.wantErr) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if !reflect.DeepEqual(want, tt.want) { |
|
|
|
|
t.Errorf("Watch watcher = %v, want %v", watch, tt.want) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|