Skip to content

fix: fix gitee sonar code check(issue #616) #627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/config/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (conf *InmemoryConfiguration) GetSubProperty(subKey string) map[string]stru
}

properties := make(map[string]struct{})
conf.store.Range(func(key, value interface{}) bool {
conf.store.Range(func(key, _ interface{}) bool {
if idx := strings.Index(key.(string), subKey); idx >= 0 {
after := key.(string)[idx+len(subKey):]
if i := strings.Index(after, "."); i >= 0 {
Expand Down
6 changes: 3 additions & 3 deletions common/config/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ func TestGetEnvInstance(t *testing.T) {
assert.NotNil(t, instance)
}

func TestEnvironment_UpdateExternalConfigMap(t *testing.T) {
func TestEnvironmentUpdateExternalConfigMap(t *testing.T) {
GetEnvInstance().UpdateExternalConfigMap(map[string]string{"1": "2"})
v, ok := GetEnvInstance().externalConfigMap.Load("1")
assert.True(t, ok)
assert.Equal(t, "2", v)
}

func TestEnvironment_ConfigurationAndGetProperty(t *testing.T) {
func TestEnvironmentConfigurationAndGetProperty(t *testing.T) {
GetEnvInstance().UpdateExternalConfigMap(map[string]string{"1": "2"})
list := GetEnvInstance().Configuration()
ok, v := list.Back().Value.(*InmemoryConfiguration).GetProperty("1")
assert.True(t, ok)
assert.Equal(t, "2", v)
}

func TestInmemoryConfiguration_GetSubProperty(t *testing.T) {
func TestInmemoryConfigurationGetSubProperty(t *testing.T) {
GetEnvInstance().UpdateExternalConfigMap(map[string]string{"123": "2"})
list := GetEnvInstance().Configuration()
m := list.Front().Value.(*InmemoryConfiguration).GetSubProperty("1")
Expand Down
2 changes: 1 addition & 1 deletion common/extension/health_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
)

// SethealthChecker sets the HealthChecker with @name
func SethealthChecker(name string, fcn func(url *common.URL) router.HealthChecker) {
func SethealthChecker(name string, fcn func(_ *common.URL) router.HealthChecker) {
healthCheckers[name] = fcn
}

Expand Down
2 changes: 1 addition & 1 deletion common/extension/health_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ func (m mockHealthChecker) IsHealthy(invoker protocol.Invoker) bool {
return true
}

func newMockhealthCheck(url *common.URL) router.HealthChecker {
func newMockhealthCheck(_ *common.URL) router.HealthChecker {
return &mockHealthChecker{}
}
1 change: 1 addition & 0 deletions common/extension/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ func TestGetMetricReporter(t *testing.T) {
type mockReporter struct {
}

// Report method for feature expansion
func (m mockReporter) Report(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation, cost time.Duration, res protocol.Result) {
}
2 changes: 1 addition & 1 deletion common/extension/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
)

// SetRegistry sets the registry extension with @name
func SetRegistry(name string, v func(config *common.URL) (registry.Registry, error)) {
func SetRegistry(name string, v func(_ *common.URL) (registry.Registry, error)) {
registrys[name] = v
}

Expand Down
2 changes: 1 addition & 1 deletion common/extension/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
)

// SetRestClient sets the RestClient with @name
func SetRestClient(name string, fun func(restOptions *client.RestOptions) client.RestClient) {
func SetRestClient(name string, fun func(_ *client.RestOptions) client.RestClient) {
restClients[name] = fun
}

Expand Down
2 changes: 1 addition & 1 deletion common/extension/service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
)

// SetServiceDiscovery will store the @creator and @name
func SetServiceDiscovery(name string, creator func(url *common.URL) (registry.ServiceDiscovery, error)) {
func SetServiceDiscovery(name string, creator func(_ *common.URL) (registry.ServiceDiscovery, error)) {
discoveryCreatorMap[name] = creator
}

Expand Down
2 changes: 1 addition & 1 deletion common/proxy/proxy_factory/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type DefaultProxyFactory struct {
//}

// NewDefaultProxyFactory returns a proxy factory instance
func NewDefaultProxyFactory(options ...proxy.Option) proxy.ProxyFactory {
func NewDefaultProxyFactory(_ ...proxy.Option) proxy.ProxyFactory {
return &DefaultProxyFactory{}
}

Expand Down
6 changes: 3 additions & 3 deletions common/proxy/proxy_factory/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/apache/dubbo-go/protocol"
)

func Test_GetProxy(t *testing.T) {
func TestGetProxy(t *testing.T) {
proxyFactory := NewDefaultProxyFactory()
url := common.NewURLWithOptions()
proxy := proxyFactory.GetProxy(protocol.NewBaseInvoker(*url), url)
Expand All @@ -45,15 +45,15 @@ func (u *TestAsync) CallBack(res common.CallbackResponse) {
fmt.Println("CallBack res:", res)
}

func Test_GetAsyncProxy(t *testing.T) {
func TestGetAsyncProxy(t *testing.T) {
proxyFactory := NewDefaultProxyFactory()
url := common.NewURLWithOptions()
async := &TestAsync{}
proxy := proxyFactory.GetAsyncProxy(protocol.NewBaseInvoker(*url), async.CallBack, url)
assert.NotNil(t, proxy)
}

func Test_GetInvoker(t *testing.T) {
func TestGetInvoker(t *testing.T) {
proxyFactory := NewDefaultProxyFactory()
url := common.NewURLWithOptions()
invoker := proxyFactory.GetInvoker(*url)
Expand Down
4 changes: 2 additions & 2 deletions common/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *TestServiceInt) Reference() string {
return "com.test.TestServiceInt"
}

func TestProxy_Implement(t *testing.T) {
func TestProxyImplement(t *testing.T) {

invoker := protocol.NewBaseInvoker(common.URL{})
p := NewProxy(invoker, nil, map[string]string{constant.ASYNC_KEY: "false"})
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestProxy_Implement(t *testing.T) {
TestService
methodOne func(context.Context, interface{}, *struct{}) error
}
s1 := &S1{TestService: *s, methodOne: func(i context.Context, i2 interface{}, i3 *struct{}) error {
s1 := &S1{TestService: *s, methodOne: func(_ context.Context, _ interface{}, _ *struct{}) error {
return perrors.New("errors")
}}
p.Implement(s1)
Expand Down
1 change: 0 additions & 1 deletion common/rpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ var (
typeOfError = reflect.TypeOf((*error)(nil)).Elem()

// ServiceMap store description of service.
// todo: lowerecas?
ServiceMap = &serviceMap{
serviceMap: make(map[string]map[string]*Service),
interfaceMap: make(map[string][]*Service),
Expand Down
54 changes: 31 additions & 23 deletions common/rpc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ import (
"github.com/stretchr/testify/assert"
)

const (
referenceTestPath = "com.test.Path"
referenceTestPathDistinct = "com.test.Path1"
testInterfaceName = "testService"
testProtocol = "testprotocol"
testSuiteMethodExpectedString = "interface {}"
)

type TestService struct {
}

Expand All @@ -40,7 +48,7 @@ func (s *TestService) MethodThree() error {
return nil
}
func (s *TestService) Reference() string {
return "com.test.Path"
return referenceTestPath
}
func (s *TestService) MethodMapper() map[string]string {
return map[string]string{
Expand All @@ -63,36 +71,36 @@ func (s *testService) Method4(ctx context.Context, args []interface{}, rsp *stru
return nil
}
func (s *testService) Reference() string {
return "com.test.Path"
return referenceTestPath
}

type TestService1 struct {
}

func (s *TestService1) Reference() string {
return "com.test.Path1"
return referenceTestPathDistinct
}

func TestServiceMap_Register(t *testing.T) {
func TestServiceMapRegister(t *testing.T) {
// lowercase
s0 := &testService{}
// methods, err := ServiceMap.Register("testporotocol", s0)
_, err := ServiceMap.Register("testService", "testporotocol", s0)
_, err := ServiceMap.Register(testInterfaceName, "testporotocol", s0)
assert.EqualError(t, err, "type testService is not exported")

// succ
s := &TestService{}
methods, err := ServiceMap.Register("testService", "testporotocol", s)
methods, err := ServiceMap.Register(testInterfaceName, "testporotocol", s)
assert.NoError(t, err)
assert.Equal(t, "MethodOne,MethodThree,methodTwo", methods)

// repeat
_, err = ServiceMap.Register("testService", "testporotocol", s)
_, err = ServiceMap.Register(testInterfaceName, "testporotocol", s)
assert.EqualError(t, err, "service already defined: com.test.Path")

// no method
s1 := &TestService1{}
_, err = ServiceMap.Register("testService", "testporotocol", s1)
_, err = ServiceMap.Register(testInterfaceName, "testporotocol", s1)
assert.EqualError(t, err, "type com.test.Path1 has no exported methods of suitable type")

ServiceMap = &serviceMap{
Expand All @@ -101,28 +109,28 @@ func TestServiceMap_Register(t *testing.T) {
}
}

func TestServiceMap_UnRegister(t *testing.T) {
func TestServiceMapUnRegister(t *testing.T) {
s := &TestService{}
_, err := ServiceMap.Register("TestService", "testprotocol", s)
_, err := ServiceMap.Register("TestService", testProtocol, s)
assert.NoError(t, err)
assert.NotNil(t, ServiceMap.GetService("testprotocol", "com.test.Path"))
assert.NotNil(t, ServiceMap.GetService(testProtocol, referenceTestPath))
assert.Equal(t, 1, len(ServiceMap.GetInterface("TestService")))

err = ServiceMap.UnRegister("", "", "com.test.Path")
err = ServiceMap.UnRegister("", "", referenceTestPath)
assert.EqualError(t, err, "protocol or serviceName is nil")

err = ServiceMap.UnRegister("", "protocol", "com.test.Path")
err = ServiceMap.UnRegister("", "protocol", referenceTestPath)
assert.EqualError(t, err, "no services for protocol")

err = ServiceMap.UnRegister("", "testprotocol", "com.test.Path1")
err = ServiceMap.UnRegister("", testProtocol, referenceTestPathDistinct)
assert.EqualError(t, err, "no service for com.test.Path1")

// succ
err = ServiceMap.UnRegister("TestService", "testprotocol", "com.test.Path")
err = ServiceMap.UnRegister("TestService", testProtocol, referenceTestPath)
assert.NoError(t, err)
}

func TestMethodType_SuiteContext(t *testing.T) {
func TestMethodTypeSuiteContext(t *testing.T) {
mt := &MethodType{ctxType: reflect.TypeOf(context.TODO())}
ctx := context.WithValue(context.Background(), "key", "value")
assert.Equal(t, reflect.ValueOf(ctx), mt.SuiteContext(ctx))
Expand All @@ -139,9 +147,9 @@ func TestSuiteMethod(t *testing.T) {
method = methodType.Method()
assert.Equal(t, "func(*common.TestService, context.Context, interface {}, interface {}, interface {}) error", method.Type.String())
at := methodType.ArgsType()
assert.Equal(t, "interface {}", at[0].String())
assert.Equal(t, "interface {}", at[1].String())
assert.Equal(t, "interface {}", at[2].String())
assert.Equal(t, testSuiteMethodExpectedString, at[0].String())
assert.Equal(t, testSuiteMethodExpectedString, at[1].String())
assert.Equal(t, testSuiteMethodExpectedString, at[2].String())
ct := methodType.CtxType()
assert.Equal(t, "context.Context", ct.String())
rt := methodType.ReplyType()
Expand All @@ -153,12 +161,12 @@ func TestSuiteMethod(t *testing.T) {
method = methodType.Method()
assert.Equal(t, "func(*common.TestService, interface {}, interface {}, interface {}) (interface {}, error)", method.Type.String())
at = methodType.ArgsType()
assert.Equal(t, "interface {}", at[0].String())
assert.Equal(t, "interface {}", at[1].String())
assert.Equal(t, "interface {}", at[2].String())
assert.Equal(t, testSuiteMethodExpectedString, at[0].String())
assert.Equal(t, testSuiteMethodExpectedString, at[1].String())
assert.Equal(t, testSuiteMethodExpectedString, at[2].String())
assert.Nil(t, methodType.CtxType())
rt = methodType.ReplyType()
assert.Equal(t, "interface {}", rt.String())
assert.Equal(t, testSuiteMethodExpectedString, rt.String())

method, ok = reflect.TypeOf(s).MethodByName("MethodThree")
assert.True(t, ok)
Expand Down
7 changes: 4 additions & 3 deletions common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
ROUTER
// PROVIDER is provider role
PROVIDER
PROTOCOL = "protocol"
)

var (
Expand Down Expand Up @@ -431,7 +432,7 @@ func (c URL) GetParamAndDecoded(key string) (string, error) {
// GetRawParam gets raw param
func (c URL) GetRawParam(key string) string {
switch key {
case "protocol":
case PROTOCOL:
return c.Protocol
case "username":
return c.Username
Expand Down Expand Up @@ -519,7 +520,7 @@ func (c URL) ToMap() map[string]string {
})

if c.Protocol != "" {
paramsMap["protocol"] = c.Protocol
paramsMap[PROTOCOL] = c.Protocol
}
if c.Username != "" {
paramsMap["username"] = c.Username
Expand All @@ -538,7 +539,7 @@ func (c URL) ToMap() map[string]string {
paramsMap["port"] = port
}
if c.Protocol != "" {
paramsMap["protocol"] = c.Protocol
paramsMap[PROTOCOL] = c.Protocol
}
if c.Path != "" {
paramsMap["path"] = c.Path
Expand Down
Loading