Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: the registered address #90

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 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,150 @@ 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,
Tags: info.Tags,
})
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)
assert.Nil(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)
}
}
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
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
)
Loading
Loading