Skip to content

Commit

Permalink
Tst:mock etcd and nacos in ut (#1873)
Browse files Browse the repository at this point in the history
* godoc (#1755)

* update package comment (#1755)

* imports formatter (#1755)

* filter/graceful_shutdown license (#1755)

* filter/graceful_shutdown license (#1755)

* update some comment (#1755)

* gofmt (#1755)

* Update version.go

comment for blank (#1755)

* comment for blank (#1755)

* comment (#1755)

* ut mock nacos and etcd (#1774)

* mock nacos nolint

* mock etcd nolint

* ut mock update (#1774)
  • Loading branch information
pherzheyu authored May 2, 2022
1 parent e92d635 commit 7d4af3b
Show file tree
Hide file tree
Showing 11 changed files with 1,544 additions and 806 deletions.
4 changes: 2 additions & 2 deletions cluster/cluster/zoneaware/cluster_invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestZoneWareInvokerWithPreferredSuccess(t *testing.T) {
invoker.EXPECT().Invoke(gomock.Any()).DoAndReturn(
func(invocation protocol.Invocation) protocol.Result {
return &protocol.RPCResult{}
})
}).AnyTimes()
}

invokers = append(invokers, invoker)
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestZoneWareInvokerWithZoneSuccess(t *testing.T) {
Attrs: map[string]interface{}{constant.RegistryZoneKey: zoneValue},
Rest: clusterpkg.Rest{Tried: 0, Success: true},
}
})
}).AnyTimes()
invokers = append(invokers, invoker)
}

Expand Down
262 changes: 215 additions & 47 deletions config_center/nacos/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,85 +15,253 @@
* limitations under the License.
*/

// nolint
package nacos

import (
"net/url"
"reflect"
"sync"
"testing"
"time"
)

import (
"github.com/stretchr/testify/assert"
gxset "github.com/dubbogo/gost/container/set"
nacosClient "github.com/dubbogo/gost/database/kv/nacos"

"github.com/golang/mock/gomock"

"github.com/nacos-group/nacos-sdk-go/model"
"github.com/nacos-group/nacos-sdk-go/vo"
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/config_center"
"dubbo.apache.org/dubbo-go/v3/config_center/parser"
)

func getNacosConfig(t *testing.T) config_center.DynamicConfiguration {
params := url.Values{}
params.Set(constant.NacosNotLoadLocalCache, "true")
// MockIConfigClient is a mock of IConfigClient interface
type MockIConfigClient struct {
ctrl *gomock.Controller
recorder *MockIConfigClientMockRecorder
}

// MockIConfigClientMockRecorder is the mock recorder for MockIConfigClient
type MockIConfigClientMockRecorder struct {
mock *MockIConfigClient
}

params.Set(constant.NacosNamespaceID, "nacos")
params.Set(constant.TimeoutKey, "5s")
params.Set(constant.ClientNameKey, "nacos-client")
// NewMockIConfigClient creates a new mock instance
func NewMockIConfigClient(ctrl *gomock.Controller) *MockIConfigClient {
mock := &MockIConfigClient{ctrl: ctrl}
mock.recorder = &MockIConfigClientMockRecorder{mock}
return mock
}

registryUrl, err := common.NewURL("registry://console.nacos.io:80", common.WithParams(params))
assert.Nil(t, err)
nacosConfig, err := newNacosDynamicConfiguration(registryUrl)
assert.Nil(t, err)
return nacosConfig
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockIConfigClient) EXPECT() *MockIConfigClientMockRecorder {
return m.recorder
}

func TestPublishConfig(t *testing.T) {
nacosConfig := getNacosConfig(t)
data := `dubbo.protocol.name=dubbo`
err := nacosConfig.PublishConfig("dubbo.properties", "dubbo-go", data)
assert.Nil(t, err)
// GetConfig mocks base method
func (m *MockIConfigClient) GetConfig(param vo.ConfigParam) (string, error) {
ret := m.ctrl.Call(m, "GetConfig", param)
ret0, _ := ret[0].(string)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func TestGetConfig(t *testing.T) {
nacosConfig := getNacosConfig(t)
nacosConfig.SetParser(&parser.DefaultConfigurationParser{})
// GetConfig indicates an expected call of GetConfig
func (mr *MockIConfigClientMockRecorder) GetConfig(param interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfig", reflect.TypeOf((*MockIConfigClient)(nil).GetConfig), param)
}

config, err := nacosConfig.GetProperties("dubbo.properties", config_center.WithGroup("dubbo-go"))
assert.NotEmpty(t, config)
assert.NoError(t, err)
// PublishConfig mocks base method
func (m *MockIConfigClient) PublishConfig(param vo.ConfigParam) (bool, error) {
ret := m.ctrl.Call(m, "PublishConfig", param)
ret0, _ := ret[0].(bool)
ret1, _ := ret[1].(error)
return ret0, ret1
}

parse, err := nacosConfig.Parser().Parse(config)
assert.NoError(t, err)
assert.Equal(t, parse["dubbo.protocol.name"], "dubbo")
// PublishConfig indicates an expected call of PublishConfig
func (mr *MockIConfigClientMockRecorder) PublishConfig(param interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PublishConfig", reflect.TypeOf((*MockIConfigClient)(nil).PublishConfig), param)
}

func TestGetConfigKeysByGroup(t *testing.T) {
nacosConfig := getNacosConfig(t)
config, err := nacosConfig.GetConfigKeysByGroup("dubbo-go")
assert.NoError(t, err)
assert.True(t, config.Contains("dubbo.properties"))
// DeleteConfig mocks base method
func (m *MockIConfigClient) DeleteConfig(param vo.ConfigParam) (bool, error) {
ret := m.ctrl.Call(m, "DeleteConfig", param)
ret0, _ := ret[0].(bool)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func TestAddListener(t *testing.T) {
nacosConfig := getNacosConfig(t)
listener := &mockDataListener{}
time.Sleep(time.Second * 2)
nacosConfig.AddListener("dubbo.properties", listener)
// DeleteConfig indicates an expected call of DeleteConfig
func (mr *MockIConfigClientMockRecorder) DeleteConfig(param interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteConfig", reflect.TypeOf((*MockIConfigClient)(nil).DeleteConfig), param)
}

func TestRemoveListener(_ *testing.T) {
// TODO not supported in current go_nacos_sdk version
// ListenConfig mocks base method
func (m *MockIConfigClient) ListenConfig(params vo.ConfigParam) error {
ret := m.ctrl.Call(m, "ListenConfig", params)
ret0, _ := ret[0].(error)
return ret0
}

type mockDataListener struct {
wg sync.WaitGroup
event string
// ListenConfig indicates an expected call of ListenConfig
func (mr *MockIConfigClientMockRecorder) ListenConfig(params interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListenConfig", reflect.TypeOf((*MockIConfigClient)(nil).ListenConfig), params)
}

func (l *mockDataListener) Process(configType *config_center.ConfigChangeEvent) {
l.wg.Done()
l.event = configType.Key
// CancelListenConfig mocks base method
func (m *MockIConfigClient) CancelListenConfig(params vo.ConfigParam) error {
ret := m.ctrl.Call(m, "CancelListenConfig", params)
ret0, _ := ret[0].(error)
return ret0
}

// CancelListenConfig indicates an expected call of CancelListenConfig
func (mr *MockIConfigClientMockRecorder) CancelListenConfig(params interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CancelListenConfig", reflect.TypeOf((*MockIConfigClient)(nil).CancelListenConfig), params)
}

// SearchConfig mocks base method
func (m *MockIConfigClient) SearchConfig(param vo.SearchConfigParam) (*model.ConfigPage, error) {
ret := m.ctrl.Call(m, "SearchConfig", param)
ret0, _ := ret[0].(*model.ConfigPage)
ret1, _ := ret[1].(error)
return ret0, ret1
}

// SearchConfig indicates an expected call of SearchConfig
func (mr *MockIConfigClientMockRecorder) SearchConfig(param interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SearchConfig", reflect.TypeOf((*MockIConfigClient)(nil).SearchConfig), param)
}

// PublishAggr mocks base method
func (m *MockIConfigClient) PublishAggr(param vo.ConfigParam) (bool, error) {
ret := m.ctrl.Call(m, "PublishAggr", param)
ret0, _ := ret[0].(bool)
ret1, _ := ret[1].(error)
return ret0, ret1
}

// PublishAggr indicates an expected call of PublishAggr
func (mr *MockIConfigClientMockRecorder) PublishAggr(param interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PublishAggr", reflect.TypeOf((*MockIConfigClient)(nil).PublishAggr), param)
}

type fields struct {
BaseDynamicConfiguration config_center.BaseDynamicConfiguration
url *common.URL
rootPath string
wg sync.WaitGroup
cltLock sync.Mutex
done chan struct{}
client *nacosClient.NacosConfigClient
keyListeners sync.Map
parser parser.ConfigurationParser
}
type args struct {
key string
group string
value string
}

func newnNacosDynamicConfiguration(f fields) *nacosDynamicConfiguration {
return &nacosDynamicConfiguration{
BaseDynamicConfiguration: f.BaseDynamicConfiguration,
url: f.url,
rootPath: f.rootPath,
done: f.done,
client: f.client,
parser: f.parser,
}
}

func Test_nacosDynamicConfiguration_PublishConfig(t *testing.T) {
ctrl := gomock.NewController(t)
mnc := NewMockIConfigClient(ctrl)
mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil)
nc := &nacosClient.NacosConfigClient{}
nc.SetClient(mnc)

tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "test",
fields: fields{
client: nc,
},
args: args{
key: "dubbo.properties",
group: "dubbogo",
value: "dubbo.protocol.name=dubbo",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := newnNacosDynamicConfiguration(tt.fields)
if err := n.PublishConfig(tt.args.key, tt.args.group, tt.args.value); (err != nil) != tt.wantErr {
t.Errorf("PublishConfig() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func Test_nacosDynamicConfiguration_GetConfigKeysByGroup(t *testing.T) {
cp := &model.ConfigPage{
PageItems: []model.ConfigItem{
{
DataId: "dubbogo",
},
},
}
result := gxset.NewSet()
result.Add("dubbogo")
ctrl := gomock.NewController(t)
mnc := NewMockIConfigClient(ctrl)
mnc.EXPECT().SearchConfig(gomock.Any()).Return(cp, nil)
nc := &nacosClient.NacosConfigClient{}
nc.SetClient(mnc)

tests := []struct {
name string
fields fields
args args
want *gxset.HashSet
wantErr bool
}{
{
name: "test",
fields: fields{
client: nc,
},
args: args{
group: "dubbo",
},
want: result,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := newnNacosDynamicConfiguration(tt.fields)
got, err := n.GetConfigKeysByGroup(tt.args.group)
if (err != nil) != tt.wantErr {
t.Errorf("GetConfigKeysByGroup() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetConfigKeysByGroup() got = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/RoaringBitmap/roaring v0.7.1
github.com/Workiva/go-datastructures v1.0.52
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
github.com/agiledragon/gomonkey v2.0.2+incompatible
github.com/alibaba/sentinel-golang v1.0.4
github.com/apache/dubbo-getty v1.4.8
github.com/apache/dubbo-go-hessian2 v1.11.0
Expand All @@ -25,7 +26,7 @@ require (
github.com/go-co-op/gocron v1.9.0
github.com/go-playground/validator/v10 v10.10.1
github.com/go-resty/resty/v2 v2.7.0
github.com/golang/mock v1.4.4
github.com/golang/mock v1.5.0
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.8
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
Expand All @@ -46,7 +47,6 @@ require (
github.com/zouyx/agollo/v3 v3.4.5
go.etcd.io/etcd/api/v3 v3.5.4
go.etcd.io/etcd/client/v3 v3.5.4
go.etcd.io/etcd/server/v3 v3.5.4
go.uber.org/atomic v1.9.0
go.uber.org/zap v1.21.0
google.golang.org/genproto v0.0.0-20211104193956-4c6863e31247
Expand Down
Loading

0 comments on commit 7d4af3b

Please sign in to comment.