Skip to content

Commit

Permalink
Merge pull request #624 from cch123/fix-config-center-linter-warnings
Browse files Browse the repository at this point in the history
Imp: fix various linter warnings
  • Loading branch information
AlexStocks committed Jun 24, 2020
2 parents 0b1ad3c + c83a814 commit b847637
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 87 deletions.
10 changes: 5 additions & 5 deletions config_center/apollo/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func initApollo() *httptest.Server {
return runMockConfigServer(handlerMap, notifyResponse)
}

func configResponse(rw http.ResponseWriter, req *http.Request) {
func configResponse(rw http.ResponseWriter, _ *http.Request) {
result := fmt.Sprintf(mockConfigRes)
fmt.Fprintf(rw, "%s", result)
}
Expand All @@ -135,7 +135,7 @@ func notifyResponse(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintf(rw, "%s", result)
}

func serviceConfigResponse(rw http.ResponseWriter, req *http.Request) {
func serviceConfigResponse(rw http.ResponseWriter, _ *http.Request) {
result := fmt.Sprintf(mockServiceConfigRes)
fmt.Fprintf(rw, "%s", result)
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func runMockConfigServer(handlerMap map[string]func(http.ResponseWriter, *http.R
return ts
}

func Test_GetConfig(t *testing.T) {
func TestGetConfig(t *testing.T) {
configuration := initMockApollo(t)
configs, err := configuration.GetProperties(mockNamespace, config_center.WithGroup("dubbo"))
assert.NoError(t, err)
Expand All @@ -175,7 +175,7 @@ func Test_GetConfig(t *testing.T) {
deleteMockJson(t)
}

func Test_GetConfigItem(t *testing.T) {
func TestGetConfigItem(t *testing.T) {
configuration := initMockApollo(t)
configs, err := configuration.GetInternalProperty("application.organization")
assert.NoError(t, err)
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestRemoveListener(t *testing.T) {
apollo.RemoveListener(mockNamespace, listener)
assert.Equal(t, "", listener.event)
listenerCount := 0
apollo.listeners.Range(func(key, value interface{}) bool {
apollo.listeners.Range(func(_, value interface{}) bool {
apolloListener := value.(*apolloListener)
for e := range apolloListener.listeners {
fmt.Println(e)
Expand Down
66 changes: 35 additions & 31 deletions config_center/configurator/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,42 +72,46 @@ func (c *overrideConfigurator) Configure(url *common.URL) {
}
}

func (c *overrideConfigurator) configureIfMatchInternal(url *common.URL) {
configApp := c.configuratorUrl.GetParam(constant.APPLICATION_KEY, c.configuratorUrl.Username)
currentApp := url.GetParam(constant.APPLICATION_KEY, url.Username)
if len(configApp) == 0 || constant.ANY_VALUE == configApp || configApp == currentApp {
conditionKeys := gxset.NewSet()
conditionKeys.Add(constant.CATEGORY_KEY)
conditionKeys.Add(constant.CHECK_KEY)
conditionKeys.Add(constant.ENABLED_KEY)
conditionKeys.Add(constant.GROUP_KEY)
conditionKeys.Add(constant.VERSION_KEY)
conditionKeys.Add(constant.APPLICATION_KEY)
conditionKeys.Add(constant.SIDE_KEY)
conditionKeys.Add(constant.CONFIG_VERSION_KEY)
conditionKeys.Add(constant.COMPATIBLE_CONFIG_KEY)
returnUrl := false
c.configuratorUrl.RangeParams(func(k, _ string) bool {
value := c.configuratorUrl.GetParam(k, "")
if strings.HasPrefix(k, "~") || k == constant.APPLICATION_KEY || k == constant.SIDE_KEY {
conditionKeys.Add(k)
if len(value) != 0 && value != constant.ANY_VALUE && value != url.GetParam(strings.TrimPrefix(k, "~"), "") {
returnUrl = true
return false
}
}
return true
})
if returnUrl {
return
}
configUrl := c.configuratorUrl.CloneExceptParams(conditionKeys)
url.SetParams(configUrl.GetParams())
}
}

// configureIfMatch translate from java, compatible rules in java
func (c *overrideConfigurator) configureIfMatch(host string, url *common.URL) {
if constant.ANYHOST_VALUE == c.configuratorUrl.Ip || host == c.configuratorUrl.Ip {
providers := c.configuratorUrl.GetParam(constant.OVERRIDE_PROVIDERS_KEY, "")
if len(providers) == 0 || strings.Index(providers, url.Location) >= 0 || strings.Index(providers, constant.ANYHOST_VALUE) >= 0 {
configApp := c.configuratorUrl.GetParam(constant.APPLICATION_KEY, c.configuratorUrl.Username)
currentApp := url.GetParam(constant.APPLICATION_KEY, url.Username)
if len(configApp) == 0 || constant.ANY_VALUE == configApp || configApp == currentApp {
conditionKeys := gxset.NewSet()
conditionKeys.Add(constant.CATEGORY_KEY)
conditionKeys.Add(constant.CHECK_KEY)
conditionKeys.Add(constant.ENABLED_KEY)
conditionKeys.Add(constant.GROUP_KEY)
conditionKeys.Add(constant.VERSION_KEY)
conditionKeys.Add(constant.APPLICATION_KEY)
conditionKeys.Add(constant.SIDE_KEY)
conditionKeys.Add(constant.CONFIG_VERSION_KEY)
conditionKeys.Add(constant.COMPATIBLE_CONFIG_KEY)
returnUrl := false
c.configuratorUrl.RangeParams(func(k, v string) bool {
value := c.configuratorUrl.GetParam(k, "")
if strings.HasPrefix(k, "~") || k == constant.APPLICATION_KEY || k == constant.SIDE_KEY {
conditionKeys.Add(k)
if len(value) != 0 && value != constant.ANY_VALUE && value != url.GetParam(strings.TrimPrefix(k, "~"), "") {
returnUrl = true
return false
}
}
return true
})
if returnUrl {
return
}
configUrl := c.configuratorUrl.CloneExceptParams(conditionKeys)
url.SetParams(configUrl.GetParams())
}
c.configureIfMatchInternal(url)
}
}
}
Expand Down
37 changes: 22 additions & 15 deletions config_center/configurator/override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,58 @@ import (
"github.com/apache/dubbo-go/common/extension"
)

func Test_configureVerison2p6(t *testing.T) {
const (
defaults = "default"
override = "override"
failfast = "failfast"
failover = "failover"
)

func TestConfigureVerison2p6(t *testing.T) {
url, err := common.NewURL("override://0.0.0.0:0/com.xxx.mock.userProvider?group=1&version=1&cluster=failfast&application=BDTService")
assert.NoError(t, err)
configurator := extension.GetConfigurator("default", &url)
assert.Equal(t, "override", configurator.GetUrl().Protocol)
configurator := extension.GetConfigurator(defaults, &url)
assert.Equal(t, override, configurator.GetUrl().Protocol)

providerUrl, err := common.NewURL("jsonrpc://127.0.0.1:20001/com.ikurento.user.UserProvider?anyhost=true&app.version=0.0.1&application=BDTService&category=providers&cluster=failover&dubbo=dubbo-provider-golang-2.6.0&environment=dev&group=&interface=com.ikurento.user.UserProvider&ip=10.32.20.124&loadbalance=random&methods.GetUser.loadbalance=random&methods.GetUser.retries=1&methods.GetUser.weight=0&module=dubbogo+user-info+server&name=BDTService&organization=ikurento.com&owner=ZX&pid=64225&retries=0&service.filter=echo&side=provider&timestamp=1562076628&version=&warmup=100")
assert.NoError(t, err)
configurator.Configure(&providerUrl)
assert.Equal(t, "failfast", providerUrl.GetParam(constant.CLUSTER_KEY, ""))
assert.Equal(t, failfast, providerUrl.GetParam(constant.CLUSTER_KEY, ""))
}

func Test_configureVerisonOverrideAddr(t *testing.T) {
func TestConfigureVerisonOverrideAddr(t *testing.T) {
url, err := common.NewURL("override://0.0.0.0:0/com.xxx.mock.userProvider?group=1&version=1&cluster=failfast&application=BDTService&providerAddresses=127.0.0.2:20001|127.0.0.3:20001")
assert.NoError(t, err)
configurator := extension.GetConfigurator("default", &url)
assert.Equal(t, "override", configurator.GetUrl().Protocol)
configurator := extension.GetConfigurator(defaults, &url)
assert.Equal(t, override, configurator.GetUrl().Protocol)

providerUrl, err := common.NewURL("jsonrpc://127.0.0.1:20001/com.ikurento.user.UserProvider?anyhost=true&app.version=0.0.1&application=BDTService&category=providers&cluster=failover&dubbo=dubbo-provider-golang-2.6.0&environment=dev&group=&interface=com.ikurento.user.UserProvider&ip=10.32.20.124&loadbalance=random&methods.GetUser.loadbalance=random&methods.GetUser.retries=1&methods.GetUser.weight=0&module=dubbogo+user-info+server&name=BDTService&organization=ikurento.com&owner=ZX&pid=64225&retries=0&service.filter=echo&side=provider&timestamp=1562076628&version=&warmup=100")
assert.NoError(t, err)
configurator.Configure(&providerUrl)
assert.Equal(t, "failover", providerUrl.GetParam(constant.CLUSTER_KEY, ""))
assert.Equal(t, failover, providerUrl.GetParam(constant.CLUSTER_KEY, ""))
}

func Test_configureVerison2p6WithIp(t *testing.T) {
func TestConfigureVerison2p6WithIp(t *testing.T) {
url, err := common.NewURL("override://127.0.0.1:20001/com.xxx.mock.userProvider?group=1&version=1&cluster=failfast&application=BDTService")
assert.NoError(t, err)
configurator := extension.GetConfigurator("default", &url)
assert.Equal(t, "override", configurator.GetUrl().Protocol)
configurator := extension.GetConfigurator(defaults, &url)
assert.Equal(t, override, configurator.GetUrl().Protocol)

providerUrl, err := common.NewURL("jsonrpc://127.0.0.1:20001/com.ikurento.user.UserProvider?anyhost=true&app.version=0.0.1&application=BDTService&category=providers&cluster=failover&dubbo=dubbo-provider-golang-2.6.0&environment=dev&group=&interface=com.ikurento.user.UserProvider&ip=10.32.20.124&loadbalance=random&methods.GetUser.loadbalance=random&methods.GetUser.retries=1&methods.GetUser.weight=0&module=dubbogo+user-info+server&name=BDTService&organization=ikurento.com&owner=ZX&pid=64225&retries=0&service.filter=echo&side=provider&timestamp=1562076628&version=&warmup=100")
assert.NoError(t, err)
configurator.Configure(&providerUrl)
assert.Equal(t, "failfast", providerUrl.GetParam(constant.CLUSTER_KEY, ""))
assert.Equal(t, failfast, providerUrl.GetParam(constant.CLUSTER_KEY, ""))

}

func Test_configureVerison2p7(t *testing.T) {
func TestConfigureVerison2p7(t *testing.T) {
url, err := common.NewURL("jsonrpc://0.0.0.0:20001/com.xxx.mock.userProvider?group=1&version=1&cluster=failfast&application=BDTService&configVersion=1.0&side=provider")
assert.NoError(t, err)
configurator := extension.GetConfigurator("default", &url)
configurator := extension.GetConfigurator(defaults, &url)

providerUrl, err := common.NewURL("jsonrpc://127.0.0.1:20001/com.ikurento.user.UserProvider?anyhost=true&app.version=0.0.1&application=BDTService&category=providers&cluster=failover&dubbo=dubbo-provider-golang-2.6.0&environment=dev&group=&interface=com.ikurento.user.UserProvider&ip=10.32.20.124&loadbalance=random&methods.GetUser.loadbalance=random&methods.GetUser.retries=1&methods.GetUser.weight=0&module=dubbogo+user-info+server&name=BDTService&organization=ikurento.com&owner=ZX&pid=64225&retries=0&service.filter=echo&side=provider&timestamp=1562076628&version=&warmup=100")
assert.NoError(t, err)
configurator.Configure(&providerUrl)
assert.Equal(t, "failfast", providerUrl.GetParam(constant.CLUSTER_KEY, ""))
assert.Equal(t, failfast, providerUrl.GetParam(constant.CLUSTER_KEY, ""))

}
15 changes: 10 additions & 5 deletions config_center/mock_dynamic_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type MockDynamicConfigurationFactory struct {
Content string
}

const (
mockServiceName = "org.apache.dubbo-go.mockService"
)

var (
once sync.Once
dynamicConfiguration *MockDynamicConfiguration
Expand Down Expand Up @@ -106,6 +110,7 @@ func (c *MockDynamicConfiguration) AddListener(key string, listener Configuratio

// RemoveListener removes the listener for MockDynamicConfiguration
func (c *MockDynamicConfiguration) RemoveListener(_ string, _ ConfigurationListener, _ ...Option) {
// mock remove
}

// GetConfig returns content of MockDynamicConfiguration
Expand Down Expand Up @@ -149,20 +154,20 @@ func (c *MockDynamicConfiguration) MockServiceConfigEvent() {
config := &parser.ConfiguratorConfig{
ConfigVersion: "2.7.1",
Scope: parser.GeneralType,
Key: "org.apache.dubbo-go.mockService",
Key: mockServiceName,
Enabled: true,
Configs: []parser.ConfigItem{
{Type: parser.GeneralType,
Enabled: true,
Addresses: []string{"0.0.0.0"},
Services: []string{"org.apache.dubbo-go.mockService"},
Services: []string{mockServiceName},
Side: "provider",
Parameters: map[string]string{"cluster": "mock1"},
},
},
}
value, _ := yaml.Marshal(config)
key := "group*org.apache.dubbo-go.mockService:1.0.0" + constant.CONFIGURATORS_SUFFIX
key := "group*" + mockServiceName + ":1.0.0" + constant.CONFIGURATORS_SUFFIX
c.listener[key].Process(&ConfigChangeEvent{Key: key, Value: string(value), ConfigType: remoting.EventTypeAdd})
}

Expand All @@ -171,13 +176,13 @@ func (c *MockDynamicConfiguration) MockApplicationConfigEvent() {
config := &parser.ConfiguratorConfig{
ConfigVersion: "2.7.1",
Scope: parser.ScopeApplication,
Key: "org.apache.dubbo-go.mockService",
Key: mockServiceName,
Enabled: true,
Configs: []parser.ConfigItem{
{Type: parser.ScopeApplication,
Enabled: true,
Addresses: []string{"0.0.0.0"},
Services: []string{"org.apache.dubbo-go.mockService"},
Services: []string{mockServiceName},
Side: "provider",
Parameters: map[string]string{"cluster": "mock1"},
},
Expand Down
6 changes: 3 additions & 3 deletions config_center/nacos/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/apache/dubbo-go/common"
)

func Test_newNacosClient(t *testing.T) {
func TestNewNacosClient(t *testing.T) {
server := mockCommonNacosServer()
nacosURL := strings.ReplaceAll(server.URL, "http", "registry")
registryUrl, _ := common.NewURL(nacosURL)
Expand All @@ -54,7 +54,7 @@ func Test_newNacosClient(t *testing.T) {
c.Destroy()
}

func Test_setNacosClient(t *testing.T) {
func TestSetNacosClient(t *testing.T) {
server := mockCommonNacosServer()
nacosURL := server.Listener.Addr().String()
registryUrl, _ := common.NewURL(nacosURL)
Expand Down Expand Up @@ -88,7 +88,7 @@ func Test_setNacosClient(t *testing.T) {
c.Destroy()
}

func Test_newNacosClient_connectError(t *testing.T) {
func TestNewNacosClient_connectError(t *testing.T) {
nacosURL := "registry://127.0.0.1:8888"
registryUrl, err := common.NewURL(nacosURL)
assert.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions config_center/nacos/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func runMockConfigServer(configHandler func(http.ResponseWriter, *http.Request),
}

func mockCommonNacosServer() *httptest.Server {
return runMockConfigServer(func(writer http.ResponseWriter, request *http.Request) {
return runMockConfigServer(func(writer http.ResponseWriter, _ *http.Request) {
data := "true"
fmt.Fprintf(writer, "%s", data)
}, func(writer http.ResponseWriter, request *http.Request) {
}, func(writer http.ResponseWriter, _ *http.Request) {
data := `dubbo.properties%02dubbo%02dubbo.service.com.ikurento.user.UserProvider.cluster=failback`
fmt.Fprintf(writer, "%s", data)
})
Expand All @@ -81,15 +81,15 @@ func initNacosData(t *testing.T) (*nacosDynamicConfiguration, error) {
return nacosConfiguration.(*nacosDynamicConfiguration), err
}

func Test_GetConfig(t *testing.T) {
func TestGetConfig(t *testing.T) {
nacos, err := initNacosData(t)
assert.NoError(t, err)
configs, err := nacos.GetProperties("dubbo.properties", config_center.WithGroup("dubbo"))
_, err = nacos.Parser().Parse(configs)
assert.NoError(t, err)
}

func TestNacosDynamicConfiguration_PublishConfig(t *testing.T) {
func TestNacosDynamicConfigurationPublishConfig(t *testing.T) {
nacos, err := initNacosData(t)
assert.Nil(t, err)
key := "myKey"
Expand All @@ -99,7 +99,7 @@ func TestNacosDynamicConfiguration_PublishConfig(t *testing.T) {
assert.Nil(t, err)
}

func Test_AddListener(t *testing.T) {
func TestAddListener(t *testing.T) {
nacos, err := initNacosData(t)
assert.NoError(t, err)
listener := &mockDataListener{}
Expand All @@ -109,7 +109,7 @@ func Test_AddListener(t *testing.T) {
listener.wg.Wait()
}

func Test_RemoveListener(t *testing.T) {
func TestRemoveListener(_ *testing.T) {
//TODO not supported in current go_nacos_sdk version
}

Expand Down
2 changes: 1 addition & 1 deletion config_center/nacos/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/apache/dubbo-go/remoting"
)

func callback(listener config_center.ConfigurationListener, namespace, group, dataId, data string) {
func callback(listener config_center.ConfigurationListener, _, _, dataId, data string) {
listener.Process(&config_center.ConfigChangeEvent{Key: dataId, Value: data, ConfigType: remoting.EventTypeUpdate})
}

Expand Down
6 changes: 3 additions & 3 deletions config_center/parser/configuration_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import (
"github.com/stretchr/testify/assert"
)

func TestDefaultConfigurationParser_Parser(t *testing.T) {
func TestDefaultConfigurationParserParser(t *testing.T) {
parser := &DefaultConfigurationParser{}
m, err := parser.Parse("dubbo.registry.address=172.0.0.1\ndubbo.registry.name=test")
assert.NoError(t, err)
assert.Equal(t, 2, len(m))
assert.Equal(t, "172.0.0.1", m["dubbo.registry.address"])
}

func TestDefaultConfigurationParser_appItemToUrls_ParserToUrls(t *testing.T) {
func TestDefaultConfigurationParserAppItemToUrls_ParserToUrls(t *testing.T) {
parser := &DefaultConfigurationParser{}
content := `configVersion: 2.7.1
scope: application
Expand All @@ -60,7 +60,7 @@ configs:
assert.Equal(t, "0.0.0.0", urls[0].Location)
}

func TestDefaultConfigurationParser_serviceItemToUrls_ParserToUrls(t *testing.T) {
func TestDefaultConfigurationParserServiceItemToUrls_ParserToUrls(t *testing.T) {
parser := &DefaultConfigurationParser{}
content := `configVersion: 2.7.1
scope: notApplication
Expand Down
Loading

0 comments on commit b847637

Please sign in to comment.