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