dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/protocol/protocol_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 protocol 19 20 import ( 21 "testing" 22 "time" 23 ) 24 25 import ( 26 gxset "github.com/dubbogo/gost/container/set" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 import ( 32 "dubbo.apache.org/dubbo-go/v3/cluster/cluster" 33 "dubbo.apache.org/dubbo-go/v3/common" 34 common_cfg "dubbo.apache.org/dubbo-go/v3/common/config" 35 "dubbo.apache.org/dubbo-go/v3/common/constant" 36 "dubbo.apache.org/dubbo-go/v3/common/extension" 37 "dubbo.apache.org/dubbo-go/v3/config" 38 "dubbo.apache.org/dubbo-go/v3/config_center" 39 "dubbo.apache.org/dubbo-go/v3/config_center/configurator" 40 "dubbo.apache.org/dubbo-go/v3/protocol" 41 "dubbo.apache.org/dubbo-go/v3/protocol/protocolwrapper" 42 "dubbo.apache.org/dubbo-go/v3/registry" 43 "dubbo.apache.org/dubbo-go/v3/registry/directory" 44 "dubbo.apache.org/dubbo-go/v3/remoting" 45 ) 46 47 func init() { 48 config.SetRootConfig(config.RootConfig{ 49 Application: &config.ApplicationConfig{Name: "test-application"}, 50 Shutdown: &config.ShutdownConfig{StepTimeout: "0s"}, 51 }) 52 } 53 54 func referNormal(t *testing.T, regProtocol *registryProtocol) { 55 extension.SetProtocol("registry", GetProtocol) 56 extension.SetRegistry("mock", registry.NewMockRegistry) 57 extension.SetProtocol(protocolwrapper.FILTER, protocolwrapper.NewMockProtocolFilter) 58 extension.SetCluster("mock", cluster.NewMockCluster) 59 extension.SetDirectory("mock", directory.NewRegistryDirectory) 60 61 url, _ := common.NewURL("mock://127.0.0.1:1111") 62 suburl, _ := common.NewURL( 63 "dubbo://127.0.0.1:20000//", 64 common.WithParamsValue(constant.ClusterKey, "mock"), 65 ) 66 67 url.SubURL = suburl 68 69 invoker := regProtocol.Refer(url) 70 assert.IsType(t, &protocol.BaseInvoker{}, invoker) 71 assert.Equal(t, invoker.GetURL().String(), url.String()) 72 } 73 74 func TestRefer(t *testing.T) { 75 config.SetRootConfig(config.RootConfig{ 76 Application: &config.ApplicationConfig{Name: "test-application"}, 77 }) 78 regProtocol := newRegistryProtocol() 79 referNormal(t, regProtocol) 80 } 81 82 func TestMultiRegRefer(t *testing.T) { 83 regProtocol := newRegistryProtocol() 84 referNormal(t, regProtocol) 85 url2, _ := common.NewURL("mock://127.0.0.1:2222") 86 suburl2, _ := common.NewURL( 87 "dubbo://127.0.0.1:20000//", 88 common.WithParamsValue(constant.ClusterKey, "mock"), 89 ) 90 91 url2.SubURL = suburl2 92 93 regProtocol.Refer(url2) 94 var count int 95 regProtocol.registries.Range(func(key, value interface{}) bool { 96 count++ 97 return true 98 }) 99 assert.Equal(t, count, 2) 100 } 101 102 func TestOneRegRefer(t *testing.T) { 103 regProtocol := newRegistryProtocol() 104 referNormal(t, regProtocol) 105 106 url2, _ := common.NewURL("mock://127.0.0.1:1111") 107 suburl2, _ := common.NewURL( 108 "dubbo://127.0.0.1:20000//", 109 common.WithParamsValue(constant.ClusterKey, "mock"), 110 ) 111 112 url2.SubURL = suburl2 113 114 regProtocol.Refer(url2) 115 var count int 116 regProtocol.registries.Range(func(key, value interface{}) bool { 117 count++ 118 return true 119 }) 120 assert.Equal(t, count, 1) 121 } 122 123 func exporterNormal(t *testing.T, regProtocol *registryProtocol) *common.URL { 124 extension.SetProtocol("registry", GetProtocol) 125 extension.SetRegistry("mock", registry.NewMockRegistry) 126 extension.SetProtocol(protocolwrapper.FILTER, protocolwrapper.NewMockProtocolFilter) 127 url, _ := common.NewURL("mock://127.0.0.1:1111") 128 suburl, _ := common.NewURL( 129 "dubbo://127.0.0.1:20000/org.apache.dubbo-go.mockService", 130 common.WithParamsValue(constant.ClusterKey, "mock"), 131 common.WithParamsValue(constant.GroupKey, "group"), 132 common.WithParamsValue(constant.VersionKey, "1.0.0"), 133 ) 134 135 url.SubURL = suburl 136 invoker := protocol.NewBaseInvoker(url) 137 exporter := regProtocol.Export(invoker) 138 139 assert.IsType(t, &exporterChangeableWrapper{}, exporter) 140 assert.Equal(t, exporter.GetInvoker().GetURL().String(), suburl.String()) 141 return url 142 } 143 144 func TestExporter(t *testing.T) { 145 regProtocol := newRegistryProtocol() 146 exporterNormal(t, regProtocol) 147 } 148 149 func TestMultiRegAndMultiProtoExporter(t *testing.T) { 150 regProtocol := newRegistryProtocol() 151 exporterNormal(t, regProtocol) 152 153 url2, _ := common.NewURL("mock://127.0.0.1:2222") 154 suburl2, _ := common.NewURL( 155 "jsonrpc://127.0.0.1:20000//", 156 common.WithParamsValue(constant.ClusterKey, "mock"), 157 ) 158 159 url2.SubURL = suburl2 160 invoker2 := protocol.NewBaseInvoker(url2) 161 regProtocol.Export(invoker2) 162 163 var count int 164 regProtocol.registries.Range(func(key, value interface{}) bool { 165 count++ 166 return true 167 }) 168 assert.Equal(t, count, 2) 169 170 var count2 int 171 regProtocol.bounds.Range(func(key, value interface{}) bool { 172 count2++ 173 return true 174 }) 175 assert.Equal(t, count2, 2) 176 } 177 178 func TestOneRegAndProtoExporter(t *testing.T) { 179 regProtocol := newRegistryProtocol() 180 exporterNormal(t, regProtocol) 181 182 url2, _ := common.NewURL("mock://127.0.0.1:1111") 183 suburl2, _ := common.NewURL( 184 "dubbo://127.0.0.1:20000/org.apache.dubbo-go.mockService", 185 common.WithParamsValue(constant.ClusterKey, "mock"), 186 common.WithParamsValue(constant.GroupKey, "group"), 187 common.WithParamsValue(constant.VersionKey, "1.0.0"), 188 ) 189 190 url2.SubURL = suburl2 191 invoker2 := protocol.NewBaseInvoker(url2) 192 regProtocol.Export(invoker2) 193 194 var count int 195 regProtocol.registries.Range(func(key, value interface{}) bool { 196 count++ 197 return true 198 }) 199 assert.Equal(t, count, 1) 200 201 var count2 int 202 regProtocol.bounds.Range(func(key, value interface{}) bool { 203 count2++ 204 return true 205 }) 206 assert.Equal(t, count2, 1) 207 } 208 209 func TestDestroy(t *testing.T) { 210 regProtocol := newRegistryProtocol() 211 referNormal(t, regProtocol) 212 exporterNormal(t, regProtocol) 213 214 regProtocol.Destroy() 215 216 var count int 217 regProtocol.registries.Range(func(key, value interface{}) bool { 218 count++ 219 return true 220 }) 221 assert.Equal(t, 0, count) 222 } 223 224 func TestExportWithOverrideListener(t *testing.T) { 225 extension.SetDefaultConfigurator(configurator.NewMockConfigurator) 226 227 regProtocol := newRegistryProtocol() 228 url := exporterNormal(t, regProtocol) 229 var reg *registry.MockRegistry 230 if regI, loaded := regProtocol.registries.Load(url.PrimitiveURL); loaded { 231 reg = regI.(*registry.MockRegistry) 232 } else { 233 assert.Fail(t, "regProtocol.registries.Load can not be loaded") 234 return 235 } 236 overrideUrl, _ := common.NewURL( 237 "override://0:0:0:0/org.apache.dubbo-go.mockService?cluster=mock1&&group=group&&version=1.0.0", 238 ) 239 event := ®istry.ServiceEvent{Action: remoting.EventTypeAdd, Service: overrideUrl} 240 reg.MockEvent(event) 241 time.Sleep(1e9) 242 newUrl := url.SubURL.Clone() 243 newUrl.SetParam(constant.ClusterKey, "mock1") 244 delKeys := gxset.NewSet("dynamic", "enabled") 245 key := newUrl.CloneExceptParams(delKeys).String() 246 v2, _ := regProtocol.bounds.Load(key) 247 assert.NotNil(t, v2) 248 } 249 250 func TestExportWithServiceConfig(t *testing.T) { 251 extension.SetDefaultConfigurator(configurator.NewMockConfigurator) 252 ccUrl, _ := common.NewURL("mock://127.0.0.1:1111") 253 dc, _ := (&config_center.MockDynamicConfigurationFactory{}).GetDynamicConfiguration(ccUrl) 254 common_cfg.GetEnvInstance().SetDynamicConfiguration(dc) 255 regProtocol := newRegistryProtocol() 256 url := exporterNormal(t, regProtocol) 257 if _, loaded := regProtocol.registries.Load(url.PrimitiveURL); !loaded { 258 assert.Fail(t, "regProtocol.registries.Load can not be loaded") 259 return 260 } 261 dc.(*config_center.MockDynamicConfiguration).MockServiceConfigEvent() 262 263 newUrl := url.SubURL.Clone() 264 newUrl.SetParam(constant.ClusterKey, "mock1") 265 266 delKeys := gxset.NewSet("dynamic", "enabled") 267 key := newUrl.CloneExceptParams(delKeys).String() 268 v2, _ := regProtocol.bounds.Load(key) 269 270 assert.NotNil(t, v2) 271 } 272 273 func TestExportWithApplicationConfig(t *testing.T) { 274 extension.SetDefaultConfigurator(configurator.NewMockConfigurator) 275 ccUrl, _ := common.NewURL("mock://127.0.0.1:1111") 276 dc, _ := (&config_center.MockDynamicConfigurationFactory{}).GetDynamicConfiguration(ccUrl) 277 common_cfg.GetEnvInstance().SetDynamicConfiguration(dc) 278 regProtocol := newRegistryProtocol() 279 url := exporterNormal(t, regProtocol) 280 if _, loaded := regProtocol.registries.Load(url.PrimitiveURL); !loaded { 281 assert.Fail(t, "regProtocol.registries.Load can not be loaded") 282 return 283 } 284 dc.(*config_center.MockDynamicConfiguration).MockApplicationConfigEvent() 285 286 newUrl := url.SubURL.Clone() 287 newUrl.SetParam(constant.ClusterKey, "mock1") 288 delKeys := gxset.NewSet("dynamic", "enabled") 289 key := newUrl.CloneExceptParams(delKeys).String() 290 v2, _ := regProtocol.bounds.Load(key) 291 assert.NotNil(t, v2) 292 } 293 294 func TestGetProviderUrlWithHideKey(t *testing.T) { 295 url, _ := common.NewURL("dubbo://127.0.0.1:1111?a=a1&b=b1&.c=c1&.d=d1&e=e1&protocol=registry") 296 providerUrl := getUrlToRegistry(url, url) 297 assert.NotContains(t, providerUrl.GetParams(), ".c") 298 assert.NotContains(t, providerUrl.GetParams(), ".d") 299 assert.Contains(t, providerUrl.GetParams(), "a") 300 }