test(contrib): update unit test for contrib/registry/polaris (#2196)

Co-authored-by: rogerogers <rogers@rogerogers.com>
pull/2207/head
rogerogers 2 years ago committed by GitHub
parent 85af73a84b
commit bcef2b8e3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 218
      contrib/registry/polaris/registry_test.go

@ -5,20 +5,26 @@ import (
"testing"
"time"
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
"github.com/polarismesh/polaris-go/pkg/config"
)
// TestRegistry . TestRegistryManyService
// TestRegistry
func TestRegistry(t *testing.T) {
conf := config.NewDefaultConfiguration([]string{"127.0.0.1:8091"})
r := NewRegistryWithConfig(
conf,
WithTimeout(time.Second*10),
WithTTL(100),
WithTimeout(time.Second),
WithHeartbeat(true),
WithHealthy(true),
WithIsolate(true),
WithNamespace("default"),
WithProtocol("tcp"),
WithRetryCount(0),
WithWeight(100),
WithTTL(10),
)
ctx := context.Background()
@ -35,171 +41,197 @@ func TestRegistry(t *testing.T) {
t.Fatal(err)
}
err = r.Deregister(ctx, svc)
time.Sleep(time.Second)
result, err := r.GetService(context.Background(), "kratos-provider-0-tcp")
if err != nil {
t.Fatal(err)
}
}
// TestRegistryMany . TestRegistryManyService
func TestRegistryMany(t *testing.T) {
conf := config.NewDefaultConfiguration([]string{"127.0.0.1:8091"})
if len(result) != 1 {
t.Fatal("register error")
}
r := NewRegistryWithConfig(
conf,
WithTimeout(time.Second*10),
WithTTL(100),
)
for _, item := range result {
if item.Name != "kratos-provider-0-tcp" || item.Endpoints[0] != "tcp://127.0.0.1:9000" {
t.Fatal("register error")
}
}
svc := &registry.ServiceInstance{
Name: "kratos-provider-1-",
Version: "test",
Metadata: map[string]string{"app": "kratos"},
Endpoints: []string{"tcp://127.0.0.1:9000?isSecure=false"},
watch, err := r.Watch(ctx, "kratos-provider-0-tcp")
if err != nil {
t.Fatal(err)
}
// Test update
svc.Version = "release1.0.0"
if err = r.Register(ctx, svc); err != nil {
t.Fatal(err)
}
result, err = watch.Next()
if err != nil {
t.Fatal(err)
}
if len(result) != 1 || result[0].Version != "release1.0.0" {
t.Fatal("register error")
}
// Test add instance
svc1 := &registry.ServiceInstance{
Name: "kratos-provider-2-",
Name: "kratos-provider-0-",
Version: "test",
Metadata: map[string]string{"app": "kratos"},
Endpoints: []string{"tcp://127.0.0.1:9001?isSecure=false"},
}
svc2 := &registry.ServiceInstance{
Name: "kratos-provider-3-",
Version: "test",
Metadata: map[string]string{"app": "kratos"},
Endpoints: []string{"tcp://127.0.0.1:9002?isSecure=false"},
}
err := r.Register(context.Background(), svc)
if err != nil {
if err = r.Register(ctx, svc1); err != nil {
t.Fatal(err)
}
err = r.Register(context.Background(), svc1)
if err != nil {
if _, err = watch.Next(); err != nil {
t.Fatal(err)
}
err = r.Register(context.Background(), svc2)
result, err = r.GetService(ctx, "kratos-provider-0-tcp")
if err != nil {
t.Fatal(err)
}
err = r.Deregister(context.Background(), svc)
if err != nil {
t.Fatal(err)
if len(result) != 2 {
t.Fatal("register error")
}
err = r.Deregister(context.Background(), svc1)
if err != nil {
if err = r.Deregister(ctx, svc); err != nil {
t.Fatal(err)
}
if err = r.Deregister(ctx, svc1); err != nil {
t.Fatal(err)
}
err = r.Deregister(context.Background(), svc2)
result, err = watch.Next()
if err != nil {
t.Fatal(err)
}
if len(result) != 0 {
t.Fatal("register error")
}
}
// TestGetService . TestGetService
func TestGetService(t *testing.T) {
// TestRegistryMany
func TestRegistryMany(t *testing.T) {
conf := config.NewDefaultConfiguration([]string{"127.0.0.1:8091"})
r := NewRegistryWithConfig(
conf,
WithTimeout(time.Second*10),
WithTTL(100),
WithTimeout(time.Second),
WithHeartbeat(true),
WithHealthy(true),
WithIsolate(true),
WithNamespace("default"),
WithProtocol("tcp"),
WithRetryCount(0),
WithWeight(100),
WithTTL(10),
)
ctx := context.Background()
// Multi endpoint
svc := &registry.ServiceInstance{
Name: "kratos-provider-4-",
Name: "kratos-provider-1-",
Version: "test",
Metadata: map[string]string{"app": "kratos"},
Endpoints: []string{"tcp://127.0.0.1:9000?isSecure=false"},
Endpoints: []string{"tcp://127.0.0.1:9000?isSecure=false", "tcp://127.0.0.1:9001?isSecure=false"},
}
// Normal
svc1 := &registry.ServiceInstance{
Name: "kratos-provider-2-",
Version: "test",
Metadata: map[string]string{"app": "kratos"},
Endpoints: []string{"tcp://127.0.0.1:9002?isSecure=false"},
}
// Without metadata
svc2 := &registry.ServiceInstance{
Name: "kratos-provider-3-",
Version: "test",
Endpoints: []string{"tcp://127.0.0.1:9003?isSecure=false"},
}
err := r.Register(ctx, svc)
if err != nil {
if err := r.Register(ctx, svc); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second * 1)
serviceInstances, err := r.GetService(ctx, "kratos-provider-4-tcp")
if err != nil {
if err := r.Register(ctx, svc1); err != nil {
t.Fatal(err)
}
for _, instance := range serviceInstances {
log.Info(instance)
}
err = r.Deregister(ctx, svc)
if err != nil {
if err := r.Register(ctx, svc2); err != nil {
t.Fatal(err)
}
}
// TestWatch . TestWatch
func TestWatch(t *testing.T) {
conf := config.NewDefaultConfiguration([]string{"127.0.0.1:8091"})
time.Sleep(3 * time.Second)
r := NewRegistryWithConfig(
conf,
WithTimeout(time.Second*10),
WithTTL(100),
)
result1, err := r.GetService(ctx, "kratos-provider-1-tcp")
svc := &registry.ServiceInstance{
Name: "kratos-provider-4-",
Version: "test",
Metadata: map[string]string{"app": "kratos"},
Endpoints: []string{"tcp://127.0.0.1:9000?isSecure=false"},
if err != nil || len(result1) != 2 || result1[0].Name != "kratos-provider-1-tcp" {
t.Fatal(err)
}
watch, err := r.Watch(context.Background(), "kratos-provider-4-tcp")
if err != nil {
result2, err := r.GetService(ctx, "kratos-provider-2-tcp")
if err != nil || len(result2) != 1 || result2[0].Name != "kratos-provider-2-tcp" || result2[0].Endpoints[0] != "tcp://127.0.0.1:9002" {
t.Fatal(err)
}
err = r.Register(context.Background(), svc)
if err != nil {
result3, err := r.GetService(ctx, "kratos-provider-3-tcp")
if err != nil || len(result3) != 1 || result3[0].Name != "kratos-provider-3-tcp" || result3[0].Endpoints[0] != "tcp://127.0.0.1:9003" {
t.Fatal(err)
}
// watch svc
time.Sleep(time.Second * 1)
// svc register, AddEvent
next, err := watch.Next()
watch1, err := r.Watch(ctx, "kratos-provider-1-tcp")
if err != nil {
t.Fatal(err)
}
for _, instance := range next {
// it will output one instance
log.Info(instance)
watch2, err := r.Watch(ctx, "kratos-provider-2-tcp")
if err != nil {
t.Fatal(err)
}
err = r.Deregister(context.Background(), svc)
watch3, err := r.Watch(ctx, "kratos-provider-3-tcp")
if err != nil {
t.Fatal(err)
}
// svc deregister, DeleteEvent
next, err = watch.Next()
if err != nil {
if err = r.Deregister(ctx, svc); err != nil {
t.Fatal(err)
}
for _, instance := range next {
// it will output nothing
log.Info(instance)
result1, err = watch1.Next()
if err != nil || len(result1) != 0 {
t.Fatal("deregister error")
}
err = watch.Stop()
err = r.Deregister(ctx, svc1)
if err != nil {
t.Fatal(err)
}
_, err = watch.Next()
if err == nil {
// if nil, stop failed
t.Fatal()
result2, err = watch2.Next()
if err != nil || len(result2) != 0 {
t.Fatal("deregister error")
}
err = r.Deregister(ctx, svc2)
if err != nil {
t.Fatal(err)
}
result3, err = watch3.Next()
if err != nil || len(result3) != 0 {
t.Fatal("deregister error")
}
}

Loading…
Cancel
Save