123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- package internal
- import (
- "context"
- "os"
- "sync"
- "testing"
- "time"
- "github.com/golang/mock/gomock"
- "github.com/stretchr/testify/assert"
- "github.com/wuntsong-org/go-zero-plus/core/contextx"
- "github.com/wuntsong-org/go-zero-plus/core/lang"
- "github.com/wuntsong-org/go-zero-plus/core/logx"
- "github.com/wuntsong-org/go-zero-plus/core/stringx"
- "go.etcd.io/etcd/api/v3/etcdserverpb"
- "go.etcd.io/etcd/api/v3/mvccpb"
- clientv3 "go.etcd.io/etcd/client/v3"
- "go.etcd.io/etcd/client/v3/mock/mockserver"
- )
- var mockLock sync.Mutex
- func init() {
- logx.Disable()
- }
- func setMockClient(cli EtcdClient) func() {
- mockLock.Lock()
- NewClient = func([]string) (EtcdClient, error) {
- return cli, nil
- }
- return func() {
- NewClient = DialClient
- mockLock.Unlock()
- }
- }
- func TestGetCluster(t *testing.T) {
- AddAccount([]string{"first"}, "foo", "bar")
- c1, _ := GetRegistry().getCluster([]string{"first"})
- c2, _ := GetRegistry().getCluster([]string{"second"})
- c3, _ := GetRegistry().getCluster([]string{"first"})
- assert.Equal(t, c1, c3)
- assert.NotEqual(t, c1, c2)
- }
- func TestGetClusterKey(t *testing.T) {
- assert.Equal(t, getClusterKey([]string{"localhost:1234", "remotehost:5678"}),
- getClusterKey([]string{"remotehost:5678", "localhost:1234"}))
- }
- func TestCluster_HandleChanges(t *testing.T) {
- ctrl := gomock.NewController(t)
- l := NewMockUpdateListener(ctrl)
- l.EXPECT().OnAdd(KV{
- Key: "first",
- Val: "1",
- })
- l.EXPECT().OnAdd(KV{
- Key: "second",
- Val: "2",
- })
- l.EXPECT().OnDelete(KV{
- Key: "first",
- Val: "1",
- })
- l.EXPECT().OnDelete(KV{
- Key: "second",
- Val: "2",
- })
- l.EXPECT().OnAdd(KV{
- Key: "third",
- Val: "3",
- })
- l.EXPECT().OnAdd(KV{
- Key: "fourth",
- Val: "4",
- })
- c := newCluster([]string{"any"})
- c.listeners["any"] = []UpdateListener{l}
- c.handleChanges("any", []KV{
- {
- Key: "first",
- Val: "1",
- },
- {
- Key: "second",
- Val: "2",
- },
- })
- assert.EqualValues(t, map[string]string{
- "first": "1",
- "second": "2",
- }, c.values["any"])
- c.handleChanges("any", []KV{
- {
- Key: "third",
- Val: "3",
- },
- {
- Key: "fourth",
- Val: "4",
- },
- })
- assert.EqualValues(t, map[string]string{
- "third": "3",
- "fourth": "4",
- }, c.values["any"])
- }
- func TestCluster_Load(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- cli := NewMockEtcdClient(ctrl)
- restore := setMockClient(cli)
- defer restore()
- cli.EXPECT().Get(gomock.Any(), "any/", gomock.Any()).Return(&clientv3.GetResponse{
- Header: &etcdserverpb.ResponseHeader{},
- Kvs: []*mvccpb.KeyValue{
- {
- Key: []byte("hello"),
- Value: []byte("world"),
- },
- },
- }, nil)
- cli.EXPECT().Ctx().Return(context.Background())
- c := &cluster{
- values: make(map[string]map[string]string),
- }
- c.load(cli, "any")
- }
- func TestCluster_Watch(t *testing.T) {
- tests := []struct {
- name string
- method int
- eventType mvccpb.Event_EventType
- }{
- {
- name: "add",
- eventType: clientv3.EventTypePut,
- },
- {
- name: "delete",
- eventType: clientv3.EventTypeDelete,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- cli := NewMockEtcdClient(ctrl)
- restore := setMockClient(cli)
- defer restore()
- ch := make(chan clientv3.WatchResponse)
- cli.EXPECT().Watch(gomock.Any(), "any/", gomock.Any()).Return(ch)
- cli.EXPECT().Ctx().Return(context.Background())
- var wg sync.WaitGroup
- wg.Add(1)
- c := &cluster{
- listeners: make(map[string][]UpdateListener),
- values: make(map[string]map[string]string),
- }
- listener := NewMockUpdateListener(ctrl)
- c.listeners["any"] = []UpdateListener{listener}
- listener.EXPECT().OnAdd(gomock.Any()).Do(func(kv KV) {
- assert.Equal(t, "hello", kv.Key)
- assert.Equal(t, "world", kv.Val)
- wg.Done()
- }).MaxTimes(1)
- listener.EXPECT().OnDelete(gomock.Any()).Do(func(_ any) {
- wg.Done()
- }).MaxTimes(1)
- go c.watch(cli, "any", 0)
- ch <- clientv3.WatchResponse{
- Events: []*clientv3.Event{
- {
- Type: test.eventType,
- Kv: &mvccpb.KeyValue{
- Key: []byte("hello"),
- Value: []byte("world"),
- },
- },
- },
- }
- wg.Wait()
- })
- }
- }
- func TestClusterWatch_RespFailures(t *testing.T) {
- resps := []clientv3.WatchResponse{
- {
- Canceled: true,
- },
- {
- // cause resp.Err() != nil
- CompactRevision: 1,
- },
- }
- for _, resp := range resps {
- t.Run(stringx.Rand(), func(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- cli := NewMockEtcdClient(ctrl)
- restore := setMockClient(cli)
- defer restore()
- ch := make(chan clientv3.WatchResponse)
- cli.EXPECT().Watch(gomock.Any(), "any/", gomock.Any()).Return(ch).AnyTimes()
- cli.EXPECT().Ctx().Return(context.Background()).AnyTimes()
- c := new(cluster)
- c.done = make(chan lang.PlaceholderType)
- go func() {
- ch <- resp
- close(c.done)
- }()
- c.watch(cli, "any", 0)
- })
- }
- }
- func TestClusterWatch_CloseChan(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- cli := NewMockEtcdClient(ctrl)
- restore := setMockClient(cli)
- defer restore()
- ch := make(chan clientv3.WatchResponse)
- cli.EXPECT().Watch(gomock.Any(), "any/", gomock.Any()).Return(ch).AnyTimes()
- cli.EXPECT().Ctx().Return(context.Background()).AnyTimes()
- c := new(cluster)
- c.done = make(chan lang.PlaceholderType)
- go func() {
- close(ch)
- close(c.done)
- }()
- c.watch(cli, "any", 0)
- }
- func TestValueOnlyContext(t *testing.T) {
- ctx := contextx.ValueOnlyFrom(context.Background())
- ctx.Done()
- assert.Nil(t, ctx.Err())
- }
- func TestDialClient(t *testing.T) {
- svr, err := mockserver.StartMockServers(1)
- assert.NoError(t, err)
- svr.StartAt(0)
- certFile := createTempFile(t, []byte(certContent))
- defer os.Remove(certFile)
- keyFile := createTempFile(t, []byte(keyContent))
- defer os.Remove(keyFile)
- caFile := createTempFile(t, []byte(caContent))
- defer os.Remove(caFile)
- endpoints := []string{svr.Servers[0].Address}
- AddAccount(endpoints, "foo", "bar")
- assert.NoError(t, AddTLS(endpoints, certFile, keyFile, caFile, false))
- old := DialTimeout
- DialTimeout = time.Millisecond
- defer func() {
- DialTimeout = old
- }()
- _, err = DialClient(endpoints)
- assert.Error(t, err)
- }
- func TestRegistry_Monitor(t *testing.T) {
- svr, err := mockserver.StartMockServers(1)
- assert.NoError(t, err)
- svr.StartAt(0)
- endpoints := []string{svr.Servers[0].Address}
- GetRegistry().lock.Lock()
- GetRegistry().clusters = map[string]*cluster{
- getClusterKey(endpoints): {
- listeners: map[string][]UpdateListener{},
- values: map[string]map[string]string{
- "foo": {
- "bar": "baz",
- },
- },
- },
- }
- GetRegistry().lock.Unlock()
- assert.Error(t, GetRegistry().Monitor(endpoints, "foo", new(mockListener)))
- }
- type mockListener struct {
- }
- func (m *mockListener) OnAdd(_ KV) {
- }
- func (m *mockListener) OnDelete(_ KV) {
- }
|