Skip to content

Commit

Permalink
Optimize: the registered address
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Sep 26, 2023
1 parent 66c5157 commit 54ae15e
Show file tree
Hide file tree
Showing 4 changed files with 2,171 additions and 40 deletions.
161 changes: 161 additions & 0 deletions etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/cloudwego/hertz/pkg/app/client/discovery"
"github.com/stretchr/testify/require"
"io/ioutil"
"net/url"
"os"
"testing"
"time"

Expand All @@ -31,6 +36,7 @@ import (
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/stretchr/testify/assert"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/server/v3/embed"
)

var (
Expand Down Expand Up @@ -267,3 +273,158 @@ func TestEtcdRegistryWithHertz(t *testing.T) {
assert.Equal(t, 0, status1)
assert.Equal(t, "", string(body1))
}

func TestEtcdRegistryWithAddressBlank(t *testing.T) {
s, endpoint := setupEmbedEtcd(t)

rg, err := NewEtcdRegistry([]string{endpoint})
require.Nil(t, err)
rs, err := NewEtcdResolver([]string{endpoint})
require.Nil(t, err)

infoList := []registry.Info{
{
ServiceName: "registry-etcd-test",
Addr: utils.NewNetAddr("tcp", "[::]:8888"),
Weight: 27,
Tags: map[string]string{"hello": "world"},
},
{
ServiceName: "registry-etcd-test-suffix",
Addr: utils.NewNetAddr("tcp", "127.0.0.1:9999"),
Weight: 27,
Tags: map[string]string{"hello": "world"},
}}

// test register service
{
for _, info := range infoList {
err = rg.Register(&info)
require.Nil(t, err)

desc := rs.Target(context.TODO(), &discovery.TargetInfo{
Host: info.ServiceName,
})
result, err := rs.Resolve(context.TODO(), desc)
require.Nil(t, err)
address, err := rg.(*etcdRegistry).getAddressOfRegistration(&info)
require.Nil(t, err)
expected := discovery.Result{
CacheKey: info.ServiceName,
Instances: []discovery.Instance{
discovery.NewInstance(info.Addr.Network(), address, info.Weight, info.Tags),
},
}
require.Equal(t, expected, result)
}
}

// test deregister service
{
for _, info := range infoList {
err = rg.Deregister(&info)
require.Nil(t, err)
desc := rs.Target(context.TODO(), &discovery.TargetInfo{
Host: info.ServiceName,
})
_, err := rs.Resolve(context.TODO(), desc)
require.NotNil(t, err)
}
}
teardownEmbedEtcd(s)
}

func TestEtcdRegistryWithEnvironmentVariable(t *testing.T) {
s, endpoint := setupEmbedEtcd(t)
err := os.Setenv(hertzPortToRegistry, "8899")
if err != nil {
return
}
err = os.Setenv(hertzIpToRegistry, "127.0.0.2")
if err != nil {
return
}

rg, err := NewEtcdRegistry([]string{endpoint})
require.Nil(t, err)
rs, err := NewEtcdResolver([]string{endpoint})
require.Nil(t, err)

infoList := []registry.Info{
{
ServiceName: "registry-etcd-test",
Addr: utils.NewNetAddr("tcp", "[::]:8888"),
Weight: 27,
Tags: map[string]string{"hello": "world"},
},
{
ServiceName: "registry-etcd-test-suffix",
Addr: utils.NewNetAddr("tcp", "10.122.1.108:9999"),
Weight: 27,
Tags: map[string]string{"hello": "world"},
}}

// test register service
{
for _, info := range infoList {
err = rg.Register(&info)
require.Nil(t, err)

desc := rs.Target(context.TODO(), &discovery.TargetInfo{
Host: info.ServiceName,
})
result, err := rs.Resolve(context.TODO(), desc)
require.Nil(t, err)
address, err := rg.(*etcdRegistry).getAddressOfRegistration(&info)
require.Nil(t, err)
expected := discovery.Result{
CacheKey: info.ServiceName,
Instances: []discovery.Instance{
discovery.NewInstance(info.Addr.Network(), address, info.Weight, info.Tags),
},
}
require.Equal(t, expected, result)
}
}

// test deregister service
{
for _, info := range infoList {
err = rg.Deregister(&info)
require.Nil(t, err)
desc := rs.Target(context.TODO(), &discovery.TargetInfo{
Host: info.ServiceName,
})
_, err := rs.Resolve(context.TODO(), desc)
require.NotNil(t, err)
}
}
os.Unsetenv(hertzPortToRegistry)
os.Unsetenv(hertzIpToRegistry)
teardownEmbedEtcd(s)
}

func setupEmbedEtcd(t *testing.T) (*embed.Etcd, string) {
endpoint := fmt.Sprintf("unix://localhost:%06d", os.Getpid())
u, err := url.Parse(endpoint)
require.Nil(t, err)
dir, err := ioutil.TempDir("", "etcd_resolver_test")
require.Nil(t, err)

cfg := embed.NewConfig()
cfg.LCUrls = []url.URL{*u}
// disable etcd log
cfg.LogLevel = "panic"
cfg.Dir = dir

s, err := embed.StartEtcd(cfg)
require.Nil(t, err)

<-s.Server.ReadyNotify()
return s, endpoint
}

func teardownEmbedEtcd(s *embed.Etcd) {
s.Close()
_ = os.RemoveAll(s.Config().Dir)
}
22 changes: 18 additions & 4 deletions etcd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@ module github.com/hertz-contrib/registry/etcd
go 1.16

require (
github.com/bytedance/sonic v1.8.3
github.com/cloudwego/hertz v0.6.0
github.com/stretchr/testify v1.8.2
go.etcd.io/etcd/client/v3 v3.5.7
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
github.com/bytedance/sonic v1.10.1
github.com/cloudwego/hertz v0.6.8
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/stretchr/testify v1.8.3
go.etcd.io/etcd/client/v3 v3.5.9
go.etcd.io/etcd/server/v3 v3.5.7
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/net v0.15.0 // indirect
google.golang.org/genproto v0.0.0-20230920204549-e6e6cdab5c13 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230920204549-e6e6cdab5c13 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect
google.golang.org/grpc v1.58.2 // indirect

)
Loading

0 comments on commit 54ae15e

Please sign in to comment.