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 9c1b622
Show file tree
Hide file tree
Showing 4 changed files with 550 additions and 10 deletions.
164 changes: 164 additions & 0 deletions etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"testing"
"time"

"github.com/cloudwego/hertz/pkg/app/client/discovery"
"github.com/stretchr/testify/require"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/app/middlewares/client/sd"
Expand All @@ -31,6 +37,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 +274,160 @@ 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)
}
1 change: 1 addition & 0 deletions etcd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/cloudwego/hertz v0.6.0
github.com/stretchr/testify v1.8.2
go.etcd.io/etcd/client/v3 v3.5.7
go.etcd.io/etcd/server/v3 v3.5.7
)
Loading

0 comments on commit 9c1b622

Please sign in to comment.