dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/nacos/registry_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package nacos 19 20 import ( 21 "net/url" 22 "reflect" 23 "strconv" 24 "testing" 25 ) 26 27 import ( 28 nacosClient "github.com/dubbogo/gost/database/kv/nacos" 29 30 "github.com/golang/mock/gomock" 31 32 "github.com/nacos-group/nacos-sdk-go/v2/model" 33 "github.com/nacos-group/nacos-sdk-go/v2/vo" 34 ) 35 36 import ( 37 "dubbo.apache.org/dubbo-go/v3/common" 38 "dubbo.apache.org/dubbo-go/v3/common/constant" 39 "dubbo.apache.org/dubbo-go/v3/registry" 40 ) 41 42 // MockINamingClient is a mock of INamingClient interface 43 type MockINamingClient struct { 44 ctrl *gomock.Controller 45 recorder *MockINamingClientMockRecorder 46 } 47 48 // MockINamingClientMockRecorder is the mock recorder for MockINamingClient 49 type MockINamingClientMockRecorder struct { 50 mock *MockINamingClient 51 } 52 53 // NewMockINamingClient creates a new mock instance 54 func NewMockINamingClient(ctrl *gomock.Controller) *MockINamingClient { 55 mock := &MockINamingClient{ctrl: ctrl} 56 mock.recorder = &MockINamingClientMockRecorder{mock} 57 return mock 58 } 59 60 // EXPECT returns an object that allows the caller to indicate expected use 61 func (m *MockINamingClient) EXPECT() *MockINamingClientMockRecorder { 62 return m.recorder 63 } 64 65 // RegisterInstance mocks base method 66 func (m *MockINamingClient) BatchRegisterInstance(param vo.BatchRegisterInstanceParam) (bool, error) { 67 ret := m.ctrl.Call(m, "RegisterInstance", param) 68 ret0, _ := ret[0].(bool) 69 ret1, _ := ret[1].(error) 70 return ret0, ret1 71 } 72 73 // RegisterInstance mocks base method 74 func (m *MockINamingClient) RegisterInstance(param vo.RegisterInstanceParam) (bool, error) { 75 ret := m.ctrl.Call(m, "RegisterInstance", param) 76 ret0, _ := ret[0].(bool) 77 ret1, _ := ret[1].(error) 78 return ret0, ret1 79 } 80 81 // RegisterInstance indicates an expected call of RegisterInstance 82 func (mr *MockINamingClientMockRecorder) RegisterInstance(param interface{}) *gomock.Call { 83 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterInstance", reflect.TypeOf((*MockINamingClient)(nil).RegisterInstance), param) 84 } 85 86 // DeregisterInstance mocks base method 87 func (m *MockINamingClient) DeregisterInstance(param vo.DeregisterInstanceParam) (bool, error) { 88 ret := m.ctrl.Call(m, "DeregisterInstance", param) 89 ret0, _ := ret[0].(bool) 90 ret1, _ := ret[1].(error) 91 return ret0, ret1 92 } 93 94 // DeregisterInstance indicates an expected call of DeregisterInstance 95 func (mr *MockINamingClientMockRecorder) DeregisterInstance(param interface{}) *gomock.Call { 96 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeregisterInstance", reflect.TypeOf((*MockINamingClient)(nil).DeregisterInstance), param) 97 } 98 99 // UpdateInstance mocks base method 100 func (m *MockINamingClient) UpdateInstance(param vo.UpdateInstanceParam) (bool, error) { 101 ret := m.ctrl.Call(m, "UpdateInstance", param) 102 ret0, _ := ret[0].(bool) 103 ret1, _ := ret[1].(error) 104 return ret0, ret1 105 } 106 107 // UpdateInstance indicates an expected call of UpdateInstance 108 func (mr *MockINamingClientMockRecorder) UpdateInstance(param interface{}) *gomock.Call { 109 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateInstance", reflect.TypeOf((*MockINamingClient)(nil).UpdateInstance), param) 110 } 111 112 // GetService mocks base method 113 func (m *MockINamingClient) GetService(param vo.GetServiceParam) (model.Service, error) { 114 ret := m.ctrl.Call(m, "GetService", param) 115 ret0, _ := ret[0].(model.Service) 116 ret1, _ := ret[1].(error) 117 return ret0, ret1 118 } 119 120 // CloseClient close nacos client 121 func (m *MockINamingClient) CloseClient() { 122 } 123 124 // GetService indicates an expected call of GetService 125 func (mr *MockINamingClientMockRecorder) GetService(param interface{}) *gomock.Call { 126 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetService", reflect.TypeOf((*MockINamingClient)(nil).GetService), param) 127 } 128 129 // SelectAllInstances mocks base method 130 func (m *MockINamingClient) SelectAllInstances(param vo.SelectAllInstancesParam) ([]model.Instance, error) { 131 ret := m.ctrl.Call(m, "SelectAllInstances", param) 132 ret0, _ := ret[0].([]model.Instance) 133 ret1, _ := ret[1].(error) 134 return ret0, ret1 135 } 136 137 // SelectAllInstances indicates an expected call of SelectAllInstances 138 func (mr *MockINamingClientMockRecorder) SelectAllInstances(param interface{}) *gomock.Call { 139 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SelectAllInstances", reflect.TypeOf((*MockINamingClient)(nil).SelectAllInstances), param) 140 } 141 142 // SelectInstances mocks base method 143 func (m *MockINamingClient) SelectInstances(param vo.SelectInstancesParam) ([]model.Instance, error) { 144 ret := m.ctrl.Call(m, "SelectInstances", param) 145 ret0, _ := ret[0].([]model.Instance) 146 ret1, _ := ret[1].(error) 147 return ret0, ret1 148 } 149 150 // SelectInstances indicates an expected call of SelectInstances 151 func (mr *MockINamingClientMockRecorder) SelectInstances(param interface{}) *gomock.Call { 152 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SelectInstances", reflect.TypeOf((*MockINamingClient)(nil).SelectInstances), param) 153 } 154 155 // SelectOneHealthyInstance mocks base method 156 func (m *MockINamingClient) SelectOneHealthyInstance(param vo.SelectOneHealthInstanceParam) (*model.Instance, error) { 157 ret := m.ctrl.Call(m, "SelectOneHealthyInstance", param) 158 ret0, _ := ret[0].(*model.Instance) 159 ret1, _ := ret[1].(error) 160 return ret0, ret1 161 } 162 163 // SelectOneHealthyInstance indicates an expected call of SelectOneHealthyInstance 164 func (mr *MockINamingClientMockRecorder) SelectOneHealthyInstance(param interface{}) *gomock.Call { 165 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SelectOneHealthyInstance", reflect.TypeOf((*MockINamingClient)(nil).SelectOneHealthyInstance), param) 166 } 167 168 // Subscribe mocks base method 169 func (m *MockINamingClient) Subscribe(param *vo.SubscribeParam) error { 170 ret := m.ctrl.Call(m, "Subscribe", param) 171 ret0, _ := ret[0].(error) 172 return ret0 173 } 174 175 // Subscribe indicates an expected call of Subscribe 176 func (mr *MockINamingClientMockRecorder) Subscribe(param interface{}) *gomock.Call { 177 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Subscribe", reflect.TypeOf((*MockINamingClient)(nil).Subscribe), param) 178 } 179 180 // Unsubscribe mocks base method 181 func (m *MockINamingClient) Unsubscribe(param *vo.SubscribeParam) error { 182 ret := m.ctrl.Call(m, "Unsubscribe", param) 183 ret0, _ := ret[0].(error) 184 return ret0 185 } 186 187 // Unsubscribe indicates an expected call of Unsubscribe 188 func (mr *MockINamingClientMockRecorder) Unsubscribe(param interface{}) *gomock.Call { 189 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Unsubscribe", reflect.TypeOf((*MockINamingClient)(nil).Unsubscribe), param) 190 } 191 192 // GetAllServicesInfo mocks base method 193 func (m *MockINamingClient) GetAllServicesInfo(param vo.GetAllServiceInfoParam) (model.ServiceList, error) { 194 ret := m.ctrl.Call(m, "GetAllServicesInfo", param) 195 ret0, _ := ret[0].(model.ServiceList) 196 ret1, _ := ret[1].(error) 197 return ret0, ret1 198 } 199 200 // GetAllServicesInfo indicates an expected call of GetAllServicesInfo 201 func (mr *MockINamingClientMockRecorder) GetAllServicesInfo(param interface{}) *gomock.Call { 202 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAllServicesInfo", reflect.TypeOf((*MockINamingClient)(nil).GetAllServicesInfo), param) 203 } 204 205 type fields struct { 206 URL *common.URL 207 namingClient *nacosClient.NacosNamingClient 208 registryUrls []*common.URL 209 } 210 type args struct { 211 url *common.URL 212 notifyListener registry.NotifyListener 213 } 214 215 func newNacosRegistryForTest(f fields) *nacosRegistry { 216 return &nacosRegistry{ 217 URL: f.URL, 218 namingClient: f.namingClient, 219 registryUrls: f.registryUrls, 220 } 221 } 222 223 func Test_nacosRegistry_Register(t *testing.T) { 224 params := url.Values{} 225 params.Set(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER)) 226 params.Set(constant.NacosNotLoadLocalCache, "true") 227 params.Set(constant.ClientNameKey, "nacos-client") 228 229 regURL, _ := common.NewURL("registry://test.nacos.io:80", common.WithParams(params)) 230 231 urlMap := url.Values{} 232 urlMap.Set(constant.GroupKey, "guangzhou-idc") 233 urlMap.Set(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER)) 234 urlMap.Set(constant.InterfaceKey, "com.ikurento.user.UserProvider") 235 urlMap.Set(constant.VersionKey, "1.0.0") 236 urlMap.Set(constant.ClusterKey, "mock") 237 urlMap.Set(constant.ClientNameKey, "nacos-client") 238 testURL, _ := common.NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParams(urlMap), common.WithMethods([]string{"GetUser", "AddUser"})) 239 240 ctrl := gomock.NewController(t) 241 mnc := NewMockINamingClient(ctrl) 242 mnc.EXPECT().RegisterInstance(gomock.Any()).Return(true, nil) 243 nc := &nacosClient.NacosNamingClient{} 244 nc.SetClient(mnc) 245 246 tests := []struct { 247 name string 248 fields fields 249 args args 250 wantErr bool 251 }{ 252 { 253 name: "test", 254 fields: fields{ 255 URL: regURL, 256 namingClient: nc, 257 registryUrls: nil, 258 }, 259 args: args{ 260 url: testURL, 261 }, 262 wantErr: false, 263 }, 264 } 265 for _, tt := range tests { 266 t.Run(tt.name, func(t *testing.T) { 267 nr := newNacosRegistryForTest(tt.fields) 268 if err := nr.Register(tt.args.url); (err != nil) != tt.wantErr { 269 t.Errorf("Register() error = %v, wantErr %v", err, tt.wantErr) 270 } 271 }) 272 } 273 } 274 275 func Test_nacosRegistry_UnRegister(t *testing.T) { 276 params := url.Values{} 277 params.Set(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER)) 278 params.Set(constant.NacosNotLoadLocalCache, "true") 279 params.Set(constant.ClientNameKey, "nacos-client") 280 281 regURL, _ := common.NewURL("registry://test.nacos.io:80", common.WithParams(params)) 282 283 urlMap := url.Values{} 284 urlMap.Set(constant.GroupKey, "guangzhou-idc") 285 urlMap.Set(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER)) 286 urlMap.Set(constant.InterfaceKey, "com.ikurento.user.UserProvider") 287 urlMap.Set(constant.VersionKey, "1.0.0") 288 urlMap.Set(constant.ClusterKey, "mock") 289 urlMap.Set(constant.ClientNameKey, "nacos-client") 290 testURL, _ := common.NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParams(urlMap), common.WithMethods([]string{"GetUser", "AddUser"})) 291 292 ctrl := gomock.NewController(t) 293 mnc := NewMockINamingClient(ctrl) 294 mnc.EXPECT().DeregisterInstance(gomock.Any()).Return(true, nil) 295 nc := &nacosClient.NacosNamingClient{} 296 nc.SetClient(mnc) 297 298 tests := []struct { 299 name string 300 fields fields 301 args args 302 wantErr bool 303 }{ 304 { 305 name: "test", 306 fields: fields{ 307 URL: regURL, 308 namingClient: nc, 309 registryUrls: nil, 310 }, 311 args: args{ 312 url: testURL, 313 }, 314 wantErr: false, 315 }, 316 } 317 for _, tt := range tests { 318 t.Run(tt.name, func(t *testing.T) { 319 nr := newNacosRegistryForTest(tt.fields) 320 if err := nr.UnRegister(tt.args.url); (err != nil) != tt.wantErr { 321 t.Errorf("UnRegister() error = %v, wantErr %v", err, tt.wantErr) 322 } 323 }) 324 } 325 } 326 327 func Test_nacosRegistry_Subscribe(t *testing.T) { 328 params := url.Values{} 329 params.Set(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER)) 330 params.Set(constant.NacosNotLoadLocalCache, "true") 331 params.Set(constant.ClientNameKey, "nacos-client") 332 333 regURL, _ := common.NewURL("registry://test.nacos.io:80", common.WithParams(params)) 334 335 urlMap := url.Values{} 336 urlMap.Set(constant.GroupKey, "guangzhou-idc") 337 urlMap.Set(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER)) 338 urlMap.Set(constant.InterfaceKey, "com.ikurento.user.UserProvider") 339 urlMap.Set(constant.VersionKey, "1.0.0") 340 urlMap.Set(constant.ClusterKey, "mock") 341 urlMap.Set(constant.ClientNameKey, "nacos-client") 342 testURL, _ := common.NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParams(urlMap), common.WithMethods([]string{"GetUser", "AddUser"})) 343 344 ctrl := gomock.NewController(t) 345 mnc := NewMockINamingClient(ctrl) 346 nc := &nacosClient.NacosNamingClient{} 347 nc.SetClient(mnc) 348 349 tests := []struct { 350 name string 351 fields fields 352 args args 353 wantErr bool 354 }{ 355 { 356 name: "test", 357 fields: fields{ 358 URL: regURL, 359 namingClient: nc, 360 registryUrls: nil, 361 }, 362 args: args{ 363 url: testURL, 364 }, 365 wantErr: false, 366 }, 367 } 368 for _, tt := range tests { 369 t.Run(tt.name, func(t *testing.T) { 370 nr := newNacosRegistryForTest(tt.fields) 371 if err := nr.Subscribe(tt.args.url, tt.args.notifyListener); (err != nil) != tt.wantErr { 372 t.Errorf("Subscribe() error = %v, wantErr %v", err, tt.wantErr) 373 } 374 }) 375 } 376 }