You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/04/30 07:03:11 UTC

[GitHub] [dubbo-go] pherzheyu opened a new pull request, #1873: Tst:mock etcd and nacos in ut

pherzheyu opened a new pull request, #1873:
URL: https://github.com/apache/dubbo-go/pull/1873

   <!--  Thanks for sending a pull request!
   Read https://github.com/apache/dubbo-go/blob/master/CONTRIBUTING.md before commit pull request.
   -->
   
   **What this PR does**: 
   mock etcd and nacos in ut
   
   **Which issue(s) this PR fixes**: 
   Fixes #
   
   **You should pay attention to items below to ensure your pr passes our ci test**
   We do not merge pr with ci tests failed
   
   - [ ] All ut passed (run 'go test ./...' in project root)
   - [ ] After go-fmt ed , run 'go fmt project' using goland.
   - [ ] Golangci-lint passed, run 'sudo golangci-lint run' in project root.
   - [ ] After import formatted, (using [imports-formatter](https://github.com/dubbogo/tools#5-how-to-get-imports-formatter) to run 'imports-formatter .' in project root, to format your import blocks, mentioned in [CONTRIBUTING.md](https://github.com/apache/dubbo-go/blob/master/CONTRIBUTING.md) above) 
   - [ ] Your new-created file needs to have [apache license](https://raw.githubusercontent.com/dubbogo/resources/master/tools/license/license.txt) at the top, like other existed file does.
   - [ ] All integration test passed. You can run integration test locally (with docker env). Clone our [dubbo-go-samples](https://github.com/apache/dubbo-go-samples) project and replace the go.mod to your dubbo-go, and run 'sudo sh start_integration_test.sh' at root of samples project root. (M1 Slice is not Support)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo-go] AlexStocks commented on a diff in pull request #1873: Tst:mock etcd and nacos in ut

Posted by GitBox <gi...@apache.org>.
AlexStocks commented on code in PR #1873:
URL: https://github.com/apache/dubbo-go/pull/1873#discussion_r862336855


##########
registry/etcdv3/registry_test.go:
##########
@@ -49,79 +57,103 @@ func initRegistry(t *testing.T) *etcdV3Registry {
 	return out
 }
 
-//
-//func (suite *RegistryTestSuite) TestRegister() {
-//	t := suite.T()
-//
-//	url, _ := common.NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParamsValue(constant.ClusterKey, "mock"), common.WithMethods([]string{"GetUser", "AddUser"}))
-//
-//	reg := initRegistry(t)
-//	err := reg.Register(url)
-//	assert.NoError(t, err)
-//	children, _, err := reg.client.GetChildrenKVList("/dubbo/com.ikurento.user.UserProvider/providers")
-//	if err != nil {
-//		t.Fatal(err)
-//	}
-//	assert.Regexp(t, ".*dubbo%3A%2F%2F127.0.0.1%3A20000%2Fcom.ikurento.user.UserProvider%3Fanyhost%3Dtrue%26cluster%3Dmock", children)
-//	assert.NoError(t, err)
-//}
-//
-//func (suite *RegistryTestSuite) TestSubscribe() {
-//	t := suite.T()
-//	regurl, _ := common.NewURL("registry://127.0.0.1:1111", common.WithParamsValue(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER)))
-//	url, _ := common.NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParamsValue(constant.ClusterKey, "mock"), common.WithMethods([]string{"GetUser", "AddUser"}))
-//
-//	reg := initRegistry(t)
-//	// provider register
-//	err := reg.Register(url)
-//	if err != nil {
-//		t.Fatal(err)
-//	}
-//
-//	// consumer register
-//	regurl.SetParam(constant.RegistryRoleKey, strconv.Itoa(common.Consumer))
-//	reg2 := initRegistry(t)
-//
-//	err = reg2.Register(url)
-//	assert.NoError(t, err)
-//	listener, err := reg2.DoSubscribe(url)
-//	if err != nil {
-//		t.Fatal(err)
-//	}
-//
-//	serviceEvent, err := listener.Next()
-//	if err != nil {
-//		t.Fatal(err)
-//	}
-//	assert.Regexp(t, ".*ServiceEvent{Action{add}.*", serviceEvent.String())
-//}
-
-func (suite *RegistryTestSuite) TestConsumerDestroy() {
-	t := suite.T()
-	url, _ := common.NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParamsValue(constant.ClusterKey, "mock"), common.WithMethods([]string{"GetUser", "AddUser"}))
-
-	reg := initRegistry(t)
-	_, err := reg.DoSubscribe(url)
-	if err != nil {
-		t.Fatal(err)
+func Test_etcdV3Registry_DoRegister(t *testing.T) {
+	type fields struct {
+		BaseRegistry   registry.BaseRegistry
+		cltLock        sync.Mutex
+		client         *gxetcd.Client
+		listenerLock   sync.RWMutex
+		listener       *etcdv3.EventListener
+		dataListener   *dataListener
+		configListener *configurationListener
+	}
+	type args struct {
+		root string
+		node string
 	}
 
-	// listener.Close()
-	time.Sleep(1e9)
-	reg.Destroy()
+	var client *gxetcd.Client
+	patches := gomonkey.NewPatches()
+	patches = patches.ApplyMethod(reflect.TypeOf(client), "RegisterTemp", func(_ *gxetcd.Client, k, v string) error {
+		return nil
+	})
+	defer patches.Reset()
 
-	assert.Equal(t, false, reg.IsAvailable())
+	tests := []struct {
+		name    string
+		fields  fields
+		args    args
+		wantErr bool
+	}{
+		{
+			name: "test",
+			fields: fields{
+				client: client,
+			},
+			args: args{
+				root: "/dubbo",
+				node: "/go",
+			},
+			wantErr: false,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			r := &etcdV3Registry{
+				BaseRegistry:   tt.fields.BaseRegistry,
+				cltLock:        tt.fields.cltLock,
+				client:         tt.fields.client,
+				listenerLock:   tt.fields.listenerLock,
+				listener:       tt.fields.listener,
+				dataListener:   tt.fields.dataListener,
+				configListener: tt.fields.configListener,

Review Comment:
   如上。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo-go] AlexStocks merged pull request #1873: Tst:mock etcd and nacos in ut

Posted by GitBox <gi...@apache.org>.
AlexStocks merged PR #1873:
URL: https://github.com/apache/dubbo-go/pull/1873


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo-go] AlexStocks commented on a diff in pull request #1873: Tst:mock etcd and nacos in ut

Posted by GitBox <gi...@apache.org>.
AlexStocks commented on code in PR #1873:
URL: https://github.com/apache/dubbo-go/pull/1873#discussion_r862336711


##########
config_center/nacos/impl_test.go:
##########
@@ -15,85 +15,277 @@
  * 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
+}
+
+// NewMockIConfigClient creates a new mock instance
+func NewMockIConfigClient(ctrl *gomock.Controller) *MockIConfigClient {
+	mock := &MockIConfigClient{ctrl: ctrl}
+	mock.recorder = &MockIConfigClientMockRecorder{mock}
+	return mock
+}
+
+// EXPECT returns an object that allows the caller to indicate expected use
+func (m *MockIConfigClient) EXPECT() *MockIConfigClientMockRecorder {
+	return m.recorder
+}
+
+// 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
+}
+
+// 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)
+}
+
+// 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
+}
 
-	params.Set(constant.NacosNamespaceID, "nacos")
-	params.Set(constant.TimeoutKey, "5s")
-	params.Set(constant.ClientNameKey, "nacos-client")
+// 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)
+}
 
-	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
+// 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 TestPublishConfig(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	data := `dubbo.protocol.name=dubbo`
-	err := nacosConfig.PublishConfig("dubbo.properties", "dubbo-go", data)
-	assert.Nil(t, err)
+// 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 TestGetConfig(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	nacosConfig.SetParser(&parser.DefaultConfigurationParser{})
+// 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
+}
 
-	config, err := nacosConfig.GetProperties("dubbo.properties", config_center.WithGroup("dubbo-go"))
-	assert.NotEmpty(t, config)
-	assert.NoError(t, err)
+// 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)
+}
+
+// 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
+}
 
-	parse, err := nacosConfig.Parser().Parse(config)
-	assert.NoError(t, err)
-	assert.Equal(t, parse["dubbo.protocol.name"], "dubbo")
+// 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)
 }
 
-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"))
+// 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
 }
 
-func TestAddListener(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	listener := &mockDataListener{}
-	time.Sleep(time.Second * 2)
-	nacosConfig.AddListener("dubbo.properties", listener)
+// 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)
 }
 
-func TestRemoveListener(_ *testing.T) {
-	// TODO not supported in current go_nacos_sdk version
+// 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
 }
 
-type mockDataListener struct {
-	wg    sync.WaitGroup
-	event string
+// 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)
 }
 
-func (l *mockDataListener) Process(configType *config_center.ConfigChangeEvent) {
-	l.wg.Done()
-	l.event = configType.Key
+func Test_nacosDynamicConfiguration_PublishConfig(t *testing.T) {
+	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
+	}

Review Comment:
   这俩 struct 挪出去,别写这种风格的代码



##########
config_center/nacos/impl_test.go:
##########
@@ -15,85 +15,277 @@
  * 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
+}
+
+// NewMockIConfigClient creates a new mock instance
+func NewMockIConfigClient(ctrl *gomock.Controller) *MockIConfigClient {
+	mock := &MockIConfigClient{ctrl: ctrl}
+	mock.recorder = &MockIConfigClientMockRecorder{mock}
+	return mock
+}
+
+// EXPECT returns an object that allows the caller to indicate expected use
+func (m *MockIConfigClient) EXPECT() *MockIConfigClientMockRecorder {
+	return m.recorder
+}
+
+// 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
+}
+
+// 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)
+}
+
+// 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
+}
 
-	params.Set(constant.NacosNamespaceID, "nacos")
-	params.Set(constant.TimeoutKey, "5s")
-	params.Set(constant.ClientNameKey, "nacos-client")
+// 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)
+}
 
-	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
+// 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 TestPublishConfig(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	data := `dubbo.protocol.name=dubbo`
-	err := nacosConfig.PublishConfig("dubbo.properties", "dubbo-go", data)
-	assert.Nil(t, err)
+// 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 TestGetConfig(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	nacosConfig.SetParser(&parser.DefaultConfigurationParser{})
+// 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
+}
 
-	config, err := nacosConfig.GetProperties("dubbo.properties", config_center.WithGroup("dubbo-go"))
-	assert.NotEmpty(t, config)
-	assert.NoError(t, err)
+// 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)
+}
+
+// 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
+}
 
-	parse, err := nacosConfig.Parser().Parse(config)
-	assert.NoError(t, err)
-	assert.Equal(t, parse["dubbo.protocol.name"], "dubbo")
+// 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)
 }
 
-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"))
+// 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
 }
 
-func TestAddListener(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	listener := &mockDataListener{}
-	time.Sleep(time.Second * 2)
-	nacosConfig.AddListener("dubbo.properties", listener)
+// 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)
 }
 
-func TestRemoveListener(_ *testing.T) {
-	// TODO not supported in current go_nacos_sdk version
+// 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
 }
 
-type mockDataListener struct {
-	wg    sync.WaitGroup
-	event string
+// 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)
 }
 
-func (l *mockDataListener) Process(configType *config_center.ConfigChangeEvent) {
-	l.wg.Done()
-	l.event = configType.Key
+func Test_nacosDynamicConfiguration_PublishConfig(t *testing.T) {
+	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
+	}
+
+	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 := &nacosDynamicConfiguration{
+				BaseDynamicConfiguration: tt.fields.BaseDynamicConfiguration,
+				url:                      tt.fields.url,
+				rootPath:                 tt.fields.rootPath,
+				wg:                       tt.fields.wg,
+				cltLock:                  tt.fields.cltLock,
+				done:                     tt.fields.done,
+				client:                   tt.fields.client,
+				keyListeners:             tt.fields.keyListeners,
+				parser:                   tt.fields.parser,
+			}
+			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) {
+	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 {
+		group string
+	}

Review Comment:
   如上



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo-go] AlexStocks commented on a diff in pull request #1873: Tst:mock etcd and nacos in ut

Posted by GitBox <gi...@apache.org>.
AlexStocks commented on code in PR #1873:
URL: https://github.com/apache/dubbo-go/pull/1873#discussion_r862336810


##########
config_center/nacos/impl_test.go:
##########
@@ -15,85 +15,277 @@
  * 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
+}
+
+// NewMockIConfigClient creates a new mock instance
+func NewMockIConfigClient(ctrl *gomock.Controller) *MockIConfigClient {
+	mock := &MockIConfigClient{ctrl: ctrl}
+	mock.recorder = &MockIConfigClientMockRecorder{mock}
+	return mock
+}
+
+// EXPECT returns an object that allows the caller to indicate expected use
+func (m *MockIConfigClient) EXPECT() *MockIConfigClientMockRecorder {
+	return m.recorder
+}
+
+// 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
+}
+
+// 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)
+}
+
+// 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
+}
 
-	params.Set(constant.NacosNamespaceID, "nacos")
-	params.Set(constant.TimeoutKey, "5s")
-	params.Set(constant.ClientNameKey, "nacos-client")
+// 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)
+}
 
-	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
+// 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 TestPublishConfig(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	data := `dubbo.protocol.name=dubbo`
-	err := nacosConfig.PublishConfig("dubbo.properties", "dubbo-go", data)
-	assert.Nil(t, err)
+// 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 TestGetConfig(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	nacosConfig.SetParser(&parser.DefaultConfigurationParser{})
+// 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
+}
 
-	config, err := nacosConfig.GetProperties("dubbo.properties", config_center.WithGroup("dubbo-go"))
-	assert.NotEmpty(t, config)
-	assert.NoError(t, err)
+// 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)
+}
+
+// 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
+}
 
-	parse, err := nacosConfig.Parser().Parse(config)
-	assert.NoError(t, err)
-	assert.Equal(t, parse["dubbo.protocol.name"], "dubbo")
+// 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)
 }
 
-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"))
+// 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
 }
 
-func TestAddListener(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	listener := &mockDataListener{}
-	time.Sleep(time.Second * 2)
-	nacosConfig.AddListener("dubbo.properties", listener)
+// 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)
 }
 
-func TestRemoveListener(_ *testing.T) {
-	// TODO not supported in current go_nacos_sdk version
+// 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
 }
 
-type mockDataListener struct {
-	wg    sync.WaitGroup
-	event string
+// 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)
 }
 
-func (l *mockDataListener) Process(configType *config_center.ConfigChangeEvent) {
-	l.wg.Done()
-	l.event = configType.Key
+func Test_nacosDynamicConfiguration_PublishConfig(t *testing.T) {
+	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
+	}
+
+	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 := &nacosDynamicConfiguration{
+				BaseDynamicConfiguration: tt.fields.BaseDynamicConfiguration,
+				url:                      tt.fields.url,
+				rootPath:                 tt.fields.rootPath,
+				wg:                       tt.fields.wg,
+				cltLock:                  tt.fields.cltLock,
+				done:                     tt.fields.done,
+				client:                   tt.fields.client,
+				keyListeners:             tt.fields.keyListeners,
+				parser:                   tt.fields.parser,
+			}
+			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) {
+	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 {
+		group string
+	}
+
+	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 := &nacosDynamicConfiguration{
+				BaseDynamicConfiguration: tt.fields.BaseDynamicConfiguration,
+				url:                      tt.fields.url,
+				rootPath:                 tt.fields.rootPath,
+				wg:                       tt.fields.wg,
+				cltLock:                  tt.fields.cltLock,
+				done:                     tt.fields.done,
+				client:                   tt.fields.client,
+				keyListeners:             tt.fields.keyListeners,
+				parser:                   tt.fields.parser,
+			}

Review Comment:
   n := &nacosDynamicConfiguration{}
   *n = tt.fields



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo-go] codecov-commenter commented on pull request #1873: Tst:mock etcd and nacos in ut

Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on PR #1873:
URL: https://github.com/apache/dubbo-go/pull/1873#issuecomment-1113939096

   # [Codecov](https://codecov.io/gh/apache/dubbo-go/pull/1873?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#1873](https://codecov.io/gh/apache/dubbo-go/pull/1873?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (d9d98c9) into [3.0](https://codecov.io/gh/apache/dubbo-go/commit/406f8013f02781d0d3e8b3f757a65d05c1989608?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (406f801) will **decrease** coverage by `1.68%`.
   > The diff coverage is `n/a`.
   
   > :exclamation: Current head d9d98c9 differs from pull request most recent head 03155c9. Consider uploading reports for the commit 03155c9 to get more accurate results
   
   ```diff
   @@            Coverage Diff             @@
   ##              3.0    #1873      +/-   ##
   ==========================================
   - Coverage   46.80%   45.12%   -1.69%     
   ==========================================
     Files         295      295              
     Lines       17178    17178              
   ==========================================
   - Hits         8041     7751     -290     
   - Misses       8285     8595     +310     
   + Partials      852      832      -20     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/dubbo-go/pull/1873?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [registry/nacos/listener.go](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cmVnaXN0cnkvbmFjb3MvbGlzdGVuZXIuZ28=) | `0.00% <0.00%> (-80.87%)` | :arrow_down: |
   | [registry/etcdv3/registry.go](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cmVnaXN0cnkvZXRjZHYzL3JlZ2lzdHJ5Lmdv) | `9.23% <0.00%> (-64.62%)` | :arrow_down: |
   | [config\_center/nacos/listener.go](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29uZmlnX2NlbnRlci9uYWNvcy9saXN0ZW5lci5nbw==) | `0.00% <0.00%> (-50.00%)` | :arrow_down: |
   | [remoting/etcdv3/listener.go](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cmVtb3RpbmcvZXRjZHYzL2xpc3RlbmVyLmdv) | `3.53% <0.00%> (-43.37%)` | :arrow_down: |
   | [registry/etcdv3/listener.go](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cmVnaXN0cnkvZXRjZHYzL2xpc3RlbmVyLmdv) | `10.00% <0.00%> (-40.00%)` | :arrow_down: |
   | [config\_center/nacos/impl.go](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-Y29uZmlnX2NlbnRlci9uYWNvcy9pbXBsLmdv) | `40.59% <0.00%> (-34.66%)` | :arrow_down: |
   | [metadata/report/etcd/report.go](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-bWV0YWRhdGEvcmVwb3J0L2V0Y2QvcmVwb3J0Lmdv) | `33.33% <0.00%> (-28.89%)` | :arrow_down: |
   | [filter/metrics/filter.go](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-ZmlsdGVyL21ldHJpY3MvZmlsdGVyLmdv) | `85.00% <0.00%> (-15.00%)` | :arrow_down: |
   | [remoting/nacos/builder.go](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cmVtb3RpbmcvbmFjb3MvYnVpbGRlci5nbw==) | `63.76% <0.00%> (-10.15%)` | :arrow_down: |
   | [registry/nacos/registry.go](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cmVnaXN0cnkvbmFjb3MvcmVnaXN0cnkuZ28=) | `43.31% <0.00%> (-3.83%)` | :arrow_down: |
   | ... and [2 more](https://codecov.io/gh/apache/dubbo-go/pull/1873/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/dubbo-go/pull/1873?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/dubbo-go/pull/1873?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [406f801...03155c9](https://codecov.io/gh/apache/dubbo-go/pull/1873?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo-go] AlexStocks commented on a diff in pull request #1873: Tst:mock etcd and nacos in ut

Posted by GitBox <gi...@apache.org>.
AlexStocks commented on code in PR #1873:
URL: https://github.com/apache/dubbo-go/pull/1873#discussion_r862336810


##########
config_center/nacos/impl_test.go:
##########
@@ -15,85 +15,277 @@
  * 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
+}
+
+// NewMockIConfigClient creates a new mock instance
+func NewMockIConfigClient(ctrl *gomock.Controller) *MockIConfigClient {
+	mock := &MockIConfigClient{ctrl: ctrl}
+	mock.recorder = &MockIConfigClientMockRecorder{mock}
+	return mock
+}
+
+// EXPECT returns an object that allows the caller to indicate expected use
+func (m *MockIConfigClient) EXPECT() *MockIConfigClientMockRecorder {
+	return m.recorder
+}
+
+// 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
+}
+
+// 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)
+}
+
+// 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
+}
 
-	params.Set(constant.NacosNamespaceID, "nacos")
-	params.Set(constant.TimeoutKey, "5s")
-	params.Set(constant.ClientNameKey, "nacos-client")
+// 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)
+}
 
-	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
+// 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 TestPublishConfig(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	data := `dubbo.protocol.name=dubbo`
-	err := nacosConfig.PublishConfig("dubbo.properties", "dubbo-go", data)
-	assert.Nil(t, err)
+// 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 TestGetConfig(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	nacosConfig.SetParser(&parser.DefaultConfigurationParser{})
+// 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
+}
 
-	config, err := nacosConfig.GetProperties("dubbo.properties", config_center.WithGroup("dubbo-go"))
-	assert.NotEmpty(t, config)
-	assert.NoError(t, err)
+// 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)
+}
+
+// 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
+}
 
-	parse, err := nacosConfig.Parser().Parse(config)
-	assert.NoError(t, err)
-	assert.Equal(t, parse["dubbo.protocol.name"], "dubbo")
+// 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)
 }
 
-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"))
+// 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
 }
 
-func TestAddListener(t *testing.T) {
-	nacosConfig := getNacosConfig(t)
-	listener := &mockDataListener{}
-	time.Sleep(time.Second * 2)
-	nacosConfig.AddListener("dubbo.properties", listener)
+// 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)
 }
 
-func TestRemoveListener(_ *testing.T) {
-	// TODO not supported in current go_nacos_sdk version
+// 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
 }
 
-type mockDataListener struct {
-	wg    sync.WaitGroup
-	event string
+// 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)
 }
 
-func (l *mockDataListener) Process(configType *config_center.ConfigChangeEvent) {
-	l.wg.Done()
-	l.event = configType.Key
+func Test_nacosDynamicConfiguration_PublishConfig(t *testing.T) {
+	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
+	}
+
+	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 := &nacosDynamicConfiguration{
+				BaseDynamicConfiguration: tt.fields.BaseDynamicConfiguration,
+				url:                      tt.fields.url,
+				rootPath:                 tt.fields.rootPath,
+				wg:                       tt.fields.wg,
+				cltLock:                  tt.fields.cltLock,
+				done:                     tt.fields.done,
+				client:                   tt.fields.client,
+				keyListeners:             tt.fields.keyListeners,
+				parser:                   tt.fields.parser,
+			}
+			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) {
+	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 {
+		group string
+	}
+
+	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 := &nacosDynamicConfiguration{
+				BaseDynamicConfiguration: tt.fields.BaseDynamicConfiguration,
+				url:                      tt.fields.url,
+				rootPath:                 tt.fields.rootPath,
+				wg:                       tt.fields.wg,
+				cltLock:                  tt.fields.cltLock,
+				done:                     tt.fields.done,
+				client:                   tt.fields.client,
+				keyListeners:             tt.fields.keyListeners,
+				parser:                   tt.fields.parser,
+			}

Review Comment:
   如果 n 和 tt.fields 类型相同,则可以这么写:
   n := &nacosDynamicConfiguration{}
   *n = tt.fields
   不行就封装一个函数出来,我看你别的地方也有类似的逻辑,写一个函数也好复用。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org