dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/nacos/service_discovery_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 "context" 22 "fmt" 23 "sync" 24 "testing" 25 ) 26 27 import ( 28 gxset "github.com/dubbogo/gost/container/set" 29 30 "github.com/nacos-group/nacos-sdk-go/v2/model" 31 "github.com/nacos-group/nacos-sdk-go/v2/vo" 32 33 perrors "github.com/pkg/errors" 34 35 "github.com/stretchr/testify/assert" 36 ) 37 38 import ( 39 "dubbo.apache.org/dubbo-go/v3/common" 40 "dubbo.apache.org/dubbo-go/v3/common/constant" 41 "dubbo.apache.org/dubbo-go/v3/common/extension" 42 "dubbo.apache.org/dubbo-go/v3/protocol" 43 "dubbo.apache.org/dubbo-go/v3/registry" 44 "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery" 45 "dubbo.apache.org/dubbo-go/v3/remoting/nacos" 46 ) 47 48 const testName = "test" 49 50 func TestNewNacosServiceDiscovery(t *testing.T) { 51 url, _ := common.NewURL("dubbo://127.0.0.1:8848", 52 common.WithParamsValue(constant.ClientNameKey, "nacos-client")) 53 sd, err := newNacosServiceDiscovery(url) 54 assert.Nil(t, err) 55 err = sd.Destroy() 56 assert.Nil(t, err) 57 } 58 59 func TestNacosServiceDiscoveryGetDefaultPageSize(t *testing.T) { 60 serviceDiscovery := &nacosServiceDiscovery{} 61 assert.Equal(t, registry.DefaultPageSize, serviceDiscovery.GetDefaultPageSize()) 62 } 63 64 func TestFunction(t *testing.T) { 65 66 extension.SetProtocol("mock", func() protocol.Protocol { 67 return &mockProtocol{} 68 }) 69 70 url, _ := common.NewURL("dubbo://127.0.0.1:8848") 71 sd, _ := newMockNacosServiceDiscovery(url) 72 defer func() { 73 _ = sd.Destroy() 74 }() 75 76 ins := ®istry.DefaultServiceInstance{ 77 ID: "testID", 78 ServiceName: testName, 79 Host: "127.0.0.1", 80 Port: 2233, 81 Enable: true, 82 Healthy: true, 83 Metadata: nil, 84 } 85 ins.Metadata = map[string]string{"t1": "test12", constant.MetadataServiceURLParamsPropertyName: `{"protocol":"mock","timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"2233"}`} 86 err := sd.Register(ins) 87 assert.Nil(t, err) 88 89 wg := &sync.WaitGroup{} 90 wg.Add(1) 91 tn := &testNotify{ 92 wg: wg, 93 t: t, 94 } 95 hs := gxset.NewSet() 96 hs.Add(testName) 97 98 sicl := servicediscovery.NewServiceInstancesChangedListener("test_app", hs) 99 sicl.AddListenerAndNotify(testName, tn) 100 err = sd.AddListener(sicl) 101 assert.NoError(t, err) 102 103 ins = ®istry.DefaultServiceInstance{ 104 ID: "testID", 105 ServiceName: testName, 106 Host: "127.0.0.1", 107 Port: 2233, 108 Enable: true, 109 Healthy: true, 110 Metadata: nil, 111 } 112 ins.Metadata = map[string]string{"t1": "test12", constant.MetadataServiceURLParamsPropertyName: `{"protocol":"mock","timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"2233"}`} 113 err = sd.Update(ins) 114 assert.NoError(t, err) 115 err = sd.Unregister(ins) 116 assert.Nil(t, err) 117 } 118 119 func newMockNacosServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, error) { 120 discoveryURL := common.NewURLWithOptions( 121 common.WithParams(url.GetParams()), 122 common.WithParamsValue(constant.TimeoutKey, url.GetParam(constant.RegistryTimeoutKey, constant.DefaultRegTimeout)), 123 common.WithParamsValue(constant.NacosGroupKey, url.GetParam(constant.RegistryGroupKey, defaultGroup)), 124 common.WithParamsValue(constant.NacosUsername, url.Username), 125 common.WithParamsValue(constant.NacosPassword, url.Password), 126 common.WithParamsValue(constant.ClientNameKey, "nacos-client"), 127 common.WithParamsValue(constant.NacosNamespaceID, url.GetParam(constant.RegistryNamespaceKey, ""))) 128 discoveryURL.Location = url.Location 129 discoveryURL.Username = url.Username 130 discoveryURL.Password = url.Password 131 client, err := nacos.NewNacosClientByURL(discoveryURL) 132 mc := mockClient{} 133 client.SetClient(mc) 134 if err != nil { 135 return nil, perrors.WithMessage(err, "create nacos namingClient failed.") 136 } 137 138 descriptor := fmt.Sprintf("nacos-service-discovery[%s]", discoveryURL.Location) 139 140 group := url.GetParam(constant.RegistryGroupKey, defaultGroup) 141 newInstance := &nacosServiceDiscovery{ 142 group: group, 143 namingClient: client, 144 descriptor: descriptor, 145 registryInstances: []registry.ServiceInstance{}, 146 instanceListenerMap: make(map[string]*gxset.HashSet), 147 } 148 return newInstance, nil 149 } 150 151 type testNotify struct { 152 wg *sync.WaitGroup 153 t *testing.T 154 } 155 156 func (tn *testNotify) Notify(e *registry.ServiceEvent) { 157 assert.Equal(tn.t, "2233", e.Service.Port) 158 tn.wg.Done() 159 } 160 161 func (tn *testNotify) NotifyAll([]*registry.ServiceEvent, func()) {} 162 163 type mockClient struct { 164 instance []interface{} 165 } 166 167 func (c mockClient) RegisterInstance(param vo.RegisterInstanceParam) (bool, error) { 168 return true, nil 169 } 170 171 func (c mockClient) BatchRegisterInstance(param vo.BatchRegisterInstanceParam) (bool, error) { 172 return true, nil 173 } 174 175 func (c mockClient) DeregisterInstance(param vo.DeregisterInstanceParam) (bool, error) { 176 return true, nil 177 } 178 179 func (c mockClient) UpdateInstance(param vo.UpdateInstanceParam) (bool, error) { 180 return true, nil 181 } 182 183 func (c mockClient) GetService(param vo.GetServiceParam) (model.Service, error) { 184 panic("implement me") 185 } 186 187 func (c mockClient) SelectInstances(param vo.SelectInstancesParam) ([]model.Instance, error) { 188 panic("implement me") 189 } 190 191 func (c mockClient) SelectAllInstances(param vo.SelectAllInstancesParam) ([]model.Instance, error) { 192 panic("implement me") 193 } 194 195 func (c mockClient) SelectOneHealthyInstance(param vo.SelectOneHealthInstanceParam) (*model.Instance, error) { 196 panic("implement me") 197 } 198 199 func (c mockClient) Subscribe(param *vo.SubscribeParam) error { 200 return nil 201 } 202 203 func (c mockClient) Unsubscribe(param *vo.SubscribeParam) error { 204 panic("implement me") 205 } 206 207 func (c mockClient) GetAllServicesInfo(param vo.GetAllServiceInfoParam) (model.ServiceList, error) { 208 panic("implement me") 209 } 210 211 func (c mockClient) CloseClient() { 212 } 213 214 type mockProtocol struct{} 215 216 func (m mockProtocol) Export(protocol.Invoker) protocol.Exporter { 217 panic("implement me") 218 } 219 220 func (m mockProtocol) Refer(*common.URL) protocol.Invoker { 221 return &mockInvoker{} 222 } 223 224 func (m mockProtocol) Destroy() { 225 panic("implement me") 226 } 227 228 type mockInvoker struct{} 229 230 func (m *mockInvoker) GetURL() *common.URL { 231 panic("implement me") 232 } 233 234 func (m *mockInvoker) IsAvailable() bool { 235 panic("implement me") 236 } 237 238 func (m *mockInvoker) Destroy() { 239 panic("implement me") 240 } 241 242 func (m *mockInvoker) Invoke(context.Context, protocol.Invocation) protocol.Result { 243 // for getMetadataInfo and ServiceInstancesChangedListenerImpl onEvent 244 serviceInfo := &common.ServiceInfo{ServiceKey: "test", MatchKey: "test"} 245 services := make(map[string]*common.ServiceInfo) 246 services["test"] = serviceInfo 247 return &protocol.RPCResult{ 248 Rest: &common.MetadataInfo{ 249 Services: services, 250 }, 251 } 252 }