github.com/nacos-group/nacos-sdk-go@v1.1.4/clients/naming_client/naming_client_test.go (about) 1 /* 2 * Copyright 1999-2020 Alibaba Group Holding Ltd. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package naming_client 18 19 import ( 20 "net/http" 21 "testing" 22 23 "github.com/golang/mock/gomock" 24 "github.com/stretchr/testify/assert" 25 26 "github.com/nacos-group/nacos-sdk-go/clients/nacos_client" 27 "github.com/nacos-group/nacos-sdk-go/common/constant" 28 "github.com/nacos-group/nacos-sdk-go/common/http_agent" 29 "github.com/nacos-group/nacos-sdk-go/mock" 30 "github.com/nacos-group/nacos-sdk-go/model" 31 "github.com/nacos-group/nacos-sdk-go/vo" 32 ) 33 34 var clientConfigTest = *constant.NewClientConfig( 35 constant.WithTimeoutMs(10*1000), 36 constant.WithBeatInterval(5*1000), 37 constant.WithNotLoadCacheAtStart(true), 38 ) 39 40 var serverConfigTest = *constant.NewServerConfig("console.nacos.io", 80, constant.WithContextPath("/nacos")) 41 42 func Test_RegisterServiceInstance_withoutGroupName(t *testing.T) { 43 ctrl := gomock.NewController(t) 44 defer func() { 45 ctrl.Finish() 46 }() 47 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 48 mockIHttpAgent.EXPECT().Request(gomock.Eq("POST"), 49 gomock.Eq("http://console.nacos.io:80/nacos/v1/ns/instance"), 50 gomock.AssignableToTypeOf(http.Header{}), 51 gomock.Eq(uint64(10*1000)), 52 gomock.Eq(map[string]string{ 53 "namespaceId": "", 54 "serviceName": "DEFAULT_GROUP@@DEMO", 55 "groupName": "DEFAULT_GROUP", 56 "app": "", 57 "clusterName": "", 58 "ip": "10.0.0.10", 59 "port": "80", 60 "weight": "0", 61 "enable": "false", 62 "healthy": "false", 63 "metadata": "{}", 64 "ephemeral": "false", 65 })).Times(1). 66 Return(http_agent.FakeHttpResponse(200, `ok`), nil) 67 nc := nacos_client.NacosClient{} 68 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 69 _ = nc.SetClientConfig(clientConfigTest) 70 _ = nc.SetHttpAgent(mockIHttpAgent) 71 client, _ := NewNamingClient(&nc) 72 success, err := client.RegisterInstance(vo.RegisterInstanceParam{ 73 ServiceName: "DEMO", 74 Ip: "10.0.0.10", 75 Port: 80, 76 Ephemeral: false, 77 }) 78 assert.Equal(t, nil, err) 79 assert.Equal(t, true, success) 80 } 81 82 func Test_RegisterServiceInstance_withGroupName(t *testing.T) { 83 ctrl := gomock.NewController(t) 84 defer func() { 85 ctrl.Finish() 86 }() 87 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 88 89 mockIHttpAgent.EXPECT().Request(gomock.Eq("POST"), 90 gomock.Eq("http://console.nacos.io:80/nacos/v1/ns/instance"), 91 gomock.AssignableToTypeOf(http.Header{}), 92 gomock.Eq(uint64(10*1000)), 93 gomock.Eq(map[string]string{ 94 "namespaceId": "", 95 "serviceName": "test_group@@DEMO2", 96 "groupName": "test_group", 97 "app": "", 98 "clusterName": "", 99 "ip": "10.0.0.10", 100 "port": "80", 101 "weight": "0", 102 "enable": "false", 103 "healthy": "false", 104 "metadata": "{}", 105 "ephemeral": "false", 106 })).Times(1). 107 Return(http_agent.FakeHttpResponse(200, `ok`), nil) 108 109 nc := nacos_client.NacosClient{} 110 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 111 _ = nc.SetClientConfig(clientConfigTest) 112 _ = nc.SetHttpAgent(mockIHttpAgent) 113 client, _ := NewNamingClient(&nc) 114 success, err := client.RegisterInstance(vo.RegisterInstanceParam{ 115 ServiceName: "DEMO2", 116 Ip: "10.0.0.10", 117 Port: 80, 118 GroupName: "test_group", 119 Ephemeral: false, 120 }) 121 assert.Equal(t, nil, err) 122 assert.Equal(t, true, success) 123 } 124 125 func Test_RegisterServiceInstance_withCluster(t *testing.T) { 126 ctrl := gomock.NewController(t) 127 defer func() { 128 ctrl.Finish() 129 }() 130 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 131 132 mockIHttpAgent.EXPECT().Request(gomock.Eq("POST"), 133 gomock.Eq("http://console.nacos.io:80/nacos/v1/ns/instance"), 134 gomock.AssignableToTypeOf(http.Header{}), 135 gomock.Eq(uint64(10*1000)), 136 gomock.Eq(map[string]string{ 137 "namespaceId": "", 138 "serviceName": "test_group@@DEMO3", 139 "groupName": "test_group", 140 "app": "", 141 "clusterName": "test", 142 "ip": "10.0.0.10", 143 "port": "80", 144 "weight": "0", 145 "enable": "false", 146 "healthy": "false", 147 "metadata": "{}", 148 "ephemeral": "false", 149 })).Times(1). 150 Return(http_agent.FakeHttpResponse(200, `ok`), nil) 151 152 nc := nacos_client.NacosClient{} 153 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 154 _ = nc.SetClientConfig(clientConfigTest) 155 _ = nc.SetHttpAgent(mockIHttpAgent) 156 client, _ := NewNamingClient(&nc) 157 success, err := client.RegisterInstance(vo.RegisterInstanceParam{ 158 ServiceName: "DEMO3", 159 Ip: "10.0.0.10", 160 Port: 80, 161 GroupName: "test_group", 162 ClusterName: "test", 163 Ephemeral: false, 164 }) 165 assert.Equal(t, nil, err) 166 assert.Equal(t, true, success) 167 } 168 169 func Test_RegisterServiceInstance_401(t *testing.T) { 170 ctrl := gomock.NewController(t) 171 defer func() { 172 ctrl.Finish() 173 }() 174 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 175 176 mockIHttpAgent.EXPECT().Request(gomock.Eq("POST"), 177 gomock.Eq("http://console.nacos.io:80/nacos/v1/ns/instance"), 178 gomock.AssignableToTypeOf(http.Header{}), 179 gomock.Eq(uint64(10*1000)), 180 gomock.Eq(map[string]string{ 181 "namespaceId": "", 182 "serviceName": "test_group@@DEMO4", 183 "groupName": "test_group", 184 "app": "", 185 "clusterName": "", 186 "ip": "10.0.0.10", 187 "port": "80", 188 "weight": "0", 189 "enable": "false", 190 "healthy": "false", 191 "metadata": "{}", 192 "ephemeral": "false", 193 })).Times(3). 194 Return(http_agent.FakeHttpResponse(401, `no security`), nil) 195 196 nc := nacos_client.NacosClient{} 197 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 198 _ = nc.SetClientConfig(clientConfigTest) 199 _ = nc.SetHttpAgent(mockIHttpAgent) 200 client, _ := NewNamingClient(&nc) 201 result, err := client.RegisterInstance(vo.RegisterInstanceParam{ 202 ServiceName: "DEMO4", 203 Ip: "10.0.0.10", 204 Port: 80, 205 GroupName: "test_group", 206 Ephemeral: false, 207 }) 208 assert.Equal(t, false, result) 209 assert.NotNil(t, err) 210 } 211 212 func TestNamingProxy_DeregisterService_WithoutGroupName(t *testing.T) { 213 ctrl := gomock.NewController(t) 214 defer func() { 215 ctrl.Finish() 216 }() 217 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 218 219 mockIHttpAgent.EXPECT().Request(gomock.Eq("DELETE"), 220 gomock.Eq("http://console.nacos.io:80/nacos/v1/ns/instance"), 221 gomock.AssignableToTypeOf(http.Header{}), 222 gomock.Eq(uint64(10*1000)), 223 gomock.Eq(map[string]string{ 224 "namespaceId": "", 225 "serviceName": "DEFAULT_GROUP@@DEMO5", 226 "clusterName": "", 227 "ip": "10.0.0.10", 228 "port": "80", 229 "ephemeral": "true", 230 })).Times(1). 231 Return(http_agent.FakeHttpResponse(200, `ok`), nil) 232 nc := nacos_client.NacosClient{} 233 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 234 _ = nc.SetClientConfig(clientConfigTest) 235 _ = nc.SetHttpAgent(mockIHttpAgent) 236 client, _ := NewNamingClient(&nc) 237 _, _ = client.DeregisterInstance(vo.DeregisterInstanceParam{ 238 ServiceName: "DEMO5", 239 Ip: "10.0.0.10", 240 Port: 80, 241 Ephemeral: true, 242 }) 243 } 244 245 func TestNamingProxy_DeregisterService_WithGroupName(t *testing.T) { 246 ctrl := gomock.NewController(t) 247 defer func() { 248 ctrl.Finish() 249 }() 250 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 251 252 mockIHttpAgent.EXPECT().Request(gomock.Eq("DELETE"), 253 gomock.Eq("http://console.nacos.io:80/nacos/v1/ns/instance"), 254 gomock.AssignableToTypeOf(http.Header{}), 255 gomock.Eq(uint64(10*1000)), 256 gomock.Eq(map[string]string{ 257 "namespaceId": "", 258 "serviceName": "test_group@@DEMO6", 259 "clusterName": "", 260 "ip": "10.0.0.10", 261 "port": "80", 262 "ephemeral": "true", 263 })).Times(1). 264 Return(http_agent.FakeHttpResponse(200, `ok`), nil) 265 nc := nacos_client.NacosClient{} 266 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 267 _ = nc.SetClientConfig(clientConfigTest) 268 _ = nc.SetHttpAgent(mockIHttpAgent) 269 client, _ := NewNamingClient(&nc) 270 _, _ = client.DeregisterInstance(vo.DeregisterInstanceParam{ 271 ServiceName: "DEMO6", 272 Ip: "10.0.0.10", 273 Port: 80, 274 GroupName: "test_group", 275 Ephemeral: true, 276 }) 277 } 278 279 func Test_UpdateServiceInstance_withoutGroupName(t *testing.T) { 280 ctrl := gomock.NewController(t) 281 defer func() { 282 ctrl.Finish() 283 }() 284 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 285 mockIHttpAgent.EXPECT().Request(gomock.Eq("PUT"), 286 gomock.Eq("http://console.nacos.io:80/nacos/v1/ns/instance"), 287 gomock.AssignableToTypeOf(http.Header{}), 288 gomock.Eq(uint64(10*1000)), 289 gomock.Eq(map[string]string{ 290 "namespaceId": "", 291 "serviceName": "DEFAULT_GROUP@@DEMO", 292 "clusterName": "", 293 "ip": "10.0.0.10", 294 "port": "80", 295 "weight": "0", 296 "enable": "false", 297 "metadata": "{}", 298 "ephemeral": "false", 299 })).Times(1). 300 Return(http_agent.FakeHttpResponse(200, `ok`), nil) 301 nc := nacos_client.NacosClient{} 302 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 303 _ = nc.SetClientConfig(clientConfigTest) 304 _ = nc.SetHttpAgent(mockIHttpAgent) 305 client, _ := NewNamingClient(&nc) 306 success, err := client.UpdateInstance(vo.UpdateInstanceParam{ 307 ServiceName: "DEMO", 308 Ip: "10.0.0.10", 309 Port: 80, 310 Ephemeral: false, 311 Metadata: map[string]string{}, 312 }) 313 assert.Equal(t, nil, err) 314 assert.Equal(t, true, success) 315 } 316 317 func TestNamingProxy_DeregisterService_401(t *testing.T) { 318 ctrl := gomock.NewController(t) 319 defer func() { 320 ctrl.Finish() 321 }() 322 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 323 324 mockIHttpAgent.EXPECT().Request(gomock.Eq("DELETE"), 325 gomock.Eq("http://console.nacos.io:80/nacos/v1/ns/instance"), 326 gomock.AssignableToTypeOf(http.Header{}), 327 gomock.Eq(uint64(10*1000)), 328 gomock.Eq(map[string]string{ 329 "namespaceId": "", 330 "serviceName": "test_group@@DEMO7", 331 "clusterName": "", 332 "ip": "10.0.0.10", 333 "port": "80", 334 "ephemeral": "true", 335 })).Times(3). 336 Return(http_agent.FakeHttpResponse(401, `no security`), nil) 337 nc := nacos_client.NacosClient{} 338 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 339 _ = nc.SetClientConfig(clientConfigTest) 340 _ = nc.SetHttpAgent(mockIHttpAgent) 341 client, _ := NewNamingClient(&nc) 342 _, _ = client.DeregisterInstance(vo.DeregisterInstanceParam{ 343 ServiceName: "DEMO7", 344 Ip: "10.0.0.10", 345 Port: 80, 346 GroupName: "test_group", 347 Ephemeral: true, 348 }) 349 } 350 351 func TestNamingClient_SelectOneHealthyInstance_SameWeight(t *testing.T) { 352 services := model.Service(model.Service{ 353 Name: "DEFAULT_GROUP@@DEMO", 354 CacheMillis: 1000, 355 UseSpecifiedURL: false, 356 Hosts: []model.Instance{ 357 { 358 Valid: true, 359 Marked: false, 360 InstanceId: "10.10.10.10-80-a-DEMO", 361 Port: 80, 362 Ip: "10.10.10.10", 363 Weight: 1, 364 Metadata: map[string]string{}, 365 ClusterName: "a", 366 ServiceName: "DEMO1", 367 Enable: true, 368 Healthy: true, 369 }, 370 { 371 Valid: true, 372 Marked: false, 373 InstanceId: "10.10.10.11-80-a-DEMO", 374 Port: 80, 375 Ip: "10.10.10.11", 376 Weight: 1, 377 Metadata: map[string]string{}, 378 ClusterName: "a", 379 ServiceName: "DEMO", 380 Enable: true, 381 Healthy: true, 382 }, 383 { 384 Valid: true, 385 Marked: false, 386 InstanceId: "10.10.10.12-80-a-DEMO", 387 Port: 80, 388 Ip: "10.10.10.12", 389 Weight: 1, 390 Metadata: map[string]string{}, 391 ClusterName: "a", 392 ServiceName: "DEMO", 393 Enable: true, 394 Healthy: false, 395 }, 396 { 397 Valid: true, 398 Marked: false, 399 InstanceId: "10.10.10.13-80-a-DEMO", 400 Port: 80, 401 Ip: "10.10.10.13", 402 Weight: 1, 403 Metadata: map[string]string{}, 404 ClusterName: "a", 405 ServiceName: "DEMO", 406 Enable: false, 407 Healthy: true, 408 }, 409 { 410 Valid: true, 411 Marked: false, 412 InstanceId: "10.10.10.14-80-a-DEMO", 413 Port: 80, 414 Ip: "10.10.10.14", 415 Weight: 0, 416 Metadata: map[string]string{}, 417 ClusterName: "a", 418 ServiceName: "DEMO", 419 Enable: true, 420 Healthy: true, 421 }, 422 }, 423 Checksum: "3bbcf6dd1175203a8afdade0e77a27cd1528787794594", 424 LastRefTime: 1528787794594, Env: "", Clusters: "a", 425 Metadata: map[string]string(nil)}) 426 ctrl := gomock.NewController(t) 427 defer func() { 428 ctrl.Finish() 429 }() 430 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 431 432 nc := nacos_client.NacosClient{} 433 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 434 _ = nc.SetClientConfig(clientConfigTest) 435 _ = nc.SetHttpAgent(mockIHttpAgent) 436 client, _ := NewNamingClient(&nc) 437 instance1, err := client.selectOneHealthyInstances(services) 438 assert.Nil(t, err) 439 assert.NotNil(t, instance1) 440 instance2, err := client.selectOneHealthyInstances(services) 441 assert.Nil(t, err) 442 assert.NotNil(t, instance2) 443 } 444 445 func TestNamingClient_SelectOneHealthyInstance_Empty(t *testing.T) { 446 services := model.Service(model.Service{ 447 Name: "DEFAULT_GROUP@@DEMO", 448 CacheMillis: 1000, 449 UseSpecifiedURL: false, 450 Hosts: []model.Instance{}, 451 Checksum: "3bbcf6dd1175203a8afdade0e77a27cd1528787794594", 452 LastRefTime: 1528787794594, Env: "", Clusters: "a", 453 Metadata: map[string]string(nil)}) 454 ctrl := gomock.NewController(t) 455 defer func() { 456 ctrl.Finish() 457 }() 458 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 459 460 nc := nacos_client.NacosClient{} 461 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 462 _ = nc.SetClientConfig(clientConfigTest) 463 _ = nc.SetHttpAgent(mockIHttpAgent) 464 client, _ := NewNamingClient(&nc) 465 instance, err := client.selectOneHealthyInstances(services) 466 assert.NotNil(t, err) 467 assert.Nil(t, instance) 468 } 469 470 func TestNamingClient_SelectInstances_Healthy(t *testing.T) { 471 services := model.Service(model.Service{ 472 Name: "DEFAULT_GROUP@@DEMO", 473 CacheMillis: 1000, 474 UseSpecifiedURL: false, 475 Hosts: []model.Instance{ 476 { 477 Valid: true, 478 Marked: false, 479 InstanceId: "10.10.10.10-80-a-DEMO", 480 Port: 80, 481 Ip: "10.10.10.10", 482 Weight: 1, 483 Metadata: map[string]string{}, 484 ClusterName: "a", 485 ServiceName: "DEMO", 486 Enable: true, 487 Healthy: true, 488 }, 489 { 490 Valid: true, 491 Marked: false, 492 InstanceId: "10.10.10.11-80-a-DEMO", 493 Port: 80, 494 Ip: "10.10.10.11", 495 Weight: 1, 496 Metadata: map[string]string{}, 497 ClusterName: "a", 498 ServiceName: "DEMO", 499 Enable: true, 500 Healthy: true, 501 }, 502 { 503 Valid: true, 504 Marked: false, 505 InstanceId: "10.10.10.12-80-a-DEMO", 506 Port: 80, 507 Ip: "10.10.10.12", 508 Weight: 1, 509 Metadata: map[string]string{}, 510 ClusterName: "a", 511 ServiceName: "DEMO", 512 Enable: true, 513 Healthy: false, 514 }, 515 { 516 Valid: true, 517 Marked: false, 518 InstanceId: "10.10.10.13-80-a-DEMO", 519 Port: 80, 520 Ip: "10.10.10.13", 521 Weight: 1, 522 Metadata: map[string]string{}, 523 ClusterName: "a", 524 ServiceName: "DEMO", 525 Enable: false, 526 Healthy: true, 527 }, 528 { 529 Valid: true, 530 Marked: false, 531 InstanceId: "10.10.10.14-80-a-DEMO", 532 Port: 80, 533 Ip: "10.10.10.14", 534 Weight: 0, 535 Metadata: map[string]string{}, 536 ClusterName: "a", 537 ServiceName: "DEMO", 538 Enable: true, 539 Healthy: true, 540 }, 541 }, 542 Checksum: "3bbcf6dd1175203a8afdade0e77a27cd1528787794594", 543 LastRefTime: 1528787794594, Env: "", Clusters: "a", 544 Metadata: map[string]string(nil)}) 545 ctrl := gomock.NewController(t) 546 defer func() { 547 ctrl.Finish() 548 }() 549 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 550 551 nc := nacos_client.NacosClient{} 552 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 553 _ = nc.SetClientConfig(clientConfigTest) 554 _ = nc.SetHttpAgent(mockIHttpAgent) 555 client, _ := NewNamingClient(&nc) 556 instances, err := client.selectInstances(services, true) 557 assert.Nil(t, err) 558 assert.Equal(t, 2, len(instances)) 559 } 560 561 func TestNamingClient_SelectInstances_Unhealthy(t *testing.T) { 562 services := model.Service(model.Service{ 563 Name: "DEFAULT_GROUP@@DEMO", 564 CacheMillis: 1000, 565 UseSpecifiedURL: false, 566 Hosts: []model.Instance{ 567 { 568 Valid: true, 569 Marked: false, 570 InstanceId: "10.10.10.10-80-a-DEMO", 571 Port: 80, 572 Ip: "10.10.10.10", 573 Weight: 1, 574 Metadata: map[string]string{}, 575 ClusterName: "a", 576 ServiceName: "DEMO", 577 Enable: true, 578 Healthy: true, 579 }, 580 { 581 Valid: true, 582 Marked: false, 583 InstanceId: "10.10.10.11-80-a-DEMO", 584 Port: 80, 585 Ip: "10.10.10.11", 586 Weight: 1, 587 Metadata: map[string]string{}, 588 ClusterName: "a", 589 ServiceName: "DEMO", 590 Enable: true, 591 Healthy: true, 592 }, 593 { 594 Valid: true, 595 Marked: false, 596 InstanceId: "10.10.10.12-80-a-DEMO", 597 Port: 80, 598 Ip: "10.10.10.12", 599 Weight: 1, 600 Metadata: map[string]string{}, 601 ClusterName: "a", 602 ServiceName: "DEMO", 603 Enable: true, 604 Healthy: false, 605 }, 606 { 607 Valid: true, 608 Marked: false, 609 InstanceId: "10.10.10.13-80-a-DEMO", 610 Port: 80, 611 Ip: "10.10.10.13", 612 Weight: 1, 613 Metadata: map[string]string{}, 614 ClusterName: "a", 615 ServiceName: "DEMO", 616 Enable: false, 617 Healthy: true, 618 }, 619 { 620 Valid: true, 621 Marked: false, 622 InstanceId: "10.10.10.14-80-a-DEMO", 623 Port: 80, 624 Ip: "10.10.10.14", 625 Weight: 0, 626 Metadata: map[string]string{}, 627 ClusterName: "a", 628 ServiceName: "DEMO", 629 Enable: true, 630 Healthy: true, 631 }, 632 }, 633 Checksum: "3bbcf6dd1175203a8afdade0e77a27cd1528787794594", 634 LastRefTime: 1528787794594, Env: "", Clusters: "a", 635 Metadata: map[string]string(nil)}) 636 ctrl := gomock.NewController(t) 637 defer func() { 638 ctrl.Finish() 639 }() 640 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 641 642 nc := nacos_client.NacosClient{} 643 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 644 _ = nc.SetClientConfig(clientConfigTest) 645 _ = nc.SetHttpAgent(mockIHttpAgent) 646 client, _ := NewNamingClient(&nc) 647 instances, err := client.selectInstances(services, false) 648 assert.Nil(t, err) 649 assert.Equal(t, 1, len(instances)) 650 } 651 652 func TestNamingClient_SelectInstances_Empty(t *testing.T) { 653 services := model.Service(model.Service{ 654 Name: "DEFAULT_GROUP@@DEMO", 655 CacheMillis: 1000, 656 UseSpecifiedURL: false, 657 Hosts: []model.Instance{}, 658 Checksum: "3bbcf6dd1175203a8afdade0e77a27cd1528787794594", 659 LastRefTime: 1528787794594, Env: "", Clusters: "a", 660 Metadata: map[string]string(nil)}) 661 ctrl := gomock.NewController(t) 662 defer func() { 663 ctrl.Finish() 664 }() 665 mockIHttpAgent := mock.NewMockIHttpAgent(ctrl) 666 667 nc := nacos_client.NacosClient{} 668 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 669 _ = nc.SetClientConfig(clientConfigTest) 670 _ = nc.SetHttpAgent(mockIHttpAgent) 671 client, _ := NewNamingClient(&nc) 672 instances, err := client.selectInstances(services, false) 673 assert.NotNil(t, err) 674 assert.Equal(t, 0, len(instances)) 675 } 676 677 func TestNamingClient_GetAllServicesInfo(t *testing.T) { 678 nc := nacos_client.NacosClient{} 679 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 680 _ = nc.SetClientConfig(clientConfigTest) 681 _ = nc.SetHttpAgent(&http_agent.HttpAgent{}) 682 client, _ := NewNamingClient(&nc) 683 reslut, err := client.GetAllServicesInfo(vo.GetAllServiceInfoParam{ 684 GroupName: "DEFAULT_GROUP", 685 PageNo: 1, 686 PageSize: 20, 687 }) 688 689 assert.NotNil(t, reslut.Doms) 690 assert.Nil(t, err) 691 } 692 693 func TestNamingClient_selectOneHealthyInstanceResult(t *testing.T) { 694 services := model.Service(model.Service{ 695 Name: "DEFAULT_GROUP@@DEMO", 696 Hosts: []model.Instance{ 697 { 698 Ip: "127.0.0.1", 699 Weight: 1, 700 Enable: true, 701 Healthy: true, 702 }, 703 { 704 Ip: "127.0.0.2", 705 Weight: 9, 706 Enable: true, 707 Healthy: true, 708 }, 709 }}) 710 nc := nacos_client.NacosClient{} 711 _ = nc.SetServerConfig([]constant.ServerConfig{serverConfigTest}) 712 _ = nc.SetClientConfig(clientConfigTest) 713 client, _ := NewNamingClient(&nc) 714 for i := 0; i < 10; i++ { 715 _, _ = client.selectOneHealthyInstances(services) 716 } 717 }