dubbo.apache.org/dubbo-go/v3@v3.1.1/config/registry_config_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 config 19 20 import ( 21 "testing" 22 ) 23 24 import ( 25 "github.com/stretchr/testify/assert" 26 ) 27 28 import ( 29 "dubbo.apache.org/dubbo-go/v3/common" 30 "dubbo.apache.org/dubbo-go/v3/common/constant" 31 ) 32 33 func TestLoadRegistries(t *testing.T) { 34 target := []string{"shanghai1"} 35 regs := map[string]*RegistryConfig{ 36 37 "shanghai1": { 38 Protocol: "mock", 39 Timeout: "2s", 40 Group: "shanghai_idc", 41 Address: "127.0.0.2:2181,128.0.0.1:2181", 42 Username: "user1", 43 Password: "pwd1", 44 }, 45 } 46 urls := loadRegistries(target, regs, common.CONSUMER) 47 t.Logf("loadRegistries() = urls:%v", urls) 48 assert.Equal(t, "127.0.0.2:2181,128.0.0.1:2181", urls[0].Location) 49 assert.Equal(t, "service-discovery-registry://127.0.0.2:2181,128.0.0.1:2181/shanghai_idc", urls[0].PrimitiveURL) 50 } 51 52 func TestLoadRegistries1(t *testing.T) { 53 target := []string{"shanghai1"} 54 regs := map[string]*RegistryConfig{ 55 56 "shanghai1": { 57 Protocol: "mock", 58 Timeout: "2s", 59 Group: "shanghai_idc", 60 Address: "127.0.0.2:2181", 61 Username: "user1", 62 Password: "pwd1", 63 }, 64 } 65 urls := loadRegistries(target, regs, common.CONSUMER) 66 t.Logf("loadRegistries() = urls:%v", urls) 67 assert.Equal(t, "127.0.0.2:2181", urls[0].Location) 68 assert.Equal(t, "service-discovery-registry://127.0.0.2:2181/shanghai_idc", urls[0].PrimitiveURL) 69 } 70 71 func TestRegistryTypeAll(t *testing.T) { 72 target := []string{"test"} 73 regs := map[string]*RegistryConfig{ 74 "test": { 75 Protocol: "mock", 76 Address: "127.0.0.2:2181", 77 RegistryType: constant.RegistryTypeAll, 78 }, 79 } 80 urls := loadRegistries(target, regs, common.PROVIDER) 81 assert.Equal(t, 2, len(urls)) 82 assert.Equal(t, "service-discovery-registry://127.0.0.2:2181", urls[0].PrimitiveURL) 83 } 84 85 func TestTranslateRegistryAddress(t *testing.T) { 86 reg := new(RegistryConfig) 87 reg.Address = "nacos://127.0.0.1:8848" 88 89 reg.translateRegistryAddress() 90 91 assert.Equal(t, "nacos", reg.Protocol) 92 assert.Equal(t, "127.0.0.1:8848", reg.Address) 93 } 94 95 func TestNewRegistryConfigBuilder(t *testing.T) { 96 97 config := NewRegistryConfigBuilder(). 98 SetProtocol("nacos"). 99 SetTimeout("10s"). 100 SetGroup("group"). 101 SetNamespace("public"). 102 SetTTL("10s"). 103 SetAddress("127.0.0.1:8848"). 104 SetUsername("nacos"). 105 SetPassword("123456"). 106 SetSimplified(true). 107 SetPreferred(true). 108 SetZone("zone"). 109 SetWeight(100). 110 SetParams(map[string]string{"timeout": "3s"}). 111 AddParam("timeout", "15s"). 112 SetRegistryType("local"). 113 Build() 114 115 config.DynamicUpdateProperties(config) 116 117 assert.Equal(t, config.Prefix(), constant.RegistryConfigPrefix) 118 119 values := config.getUrlMap(common.PROVIDER) 120 assert.Equal(t, values.Get("timeout"), "15s") 121 122 url, err := config.toMetadataReportUrl() 123 assert.NoError(t, err) 124 assert.Equal(t, url.GetParam("timeout", "3s"), "10s") 125 126 url, err = config.toURL(common.PROVIDER) 127 assert.NoError(t, err) 128 assert.Equal(t, url.GetParam("timeout", "3s"), "15s") 129 130 address := config.translateRegistryAddress() 131 assert.Equal(t, address, "127.0.0.1:8848") 132 } 133 134 func TestNewRegistryConfig(t *testing.T) { 135 config := NewRegistryConfig( 136 WithRegistryProtocol("nacos"), 137 WithRegistryAddress("127.0.0.1:8848"), 138 WithRegistryTimeOut("10s"), 139 WithRegistryGroup("group"), 140 WithRegistryTTL("10s"), 141 WithRegistryUserName("nacos"), 142 WithRegistryPassword("123456"), 143 WithRegistrySimplified(true), 144 WithRegistryPreferred(true), 145 WithRegistryWeight(100), 146 WithRegistryParams(map[string]string{"timeout": "3s"})) 147 148 assert.Equal(t, config.Timeout, "10s") 149 }