From 6ee83c77835b6a4e6f2bf375abaa6d6af7233d53 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Thu, 19 Mar 2020 20:55:25 +0800 Subject: [PATCH] repair according to code review --- cluster/router/tag/tag_router.go | 7 ++++--- cluster/router/tag/tag_router_test.go | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cluster/router/tag/tag_router.go b/cluster/router/tag/tag_router.go index 42ffb4c93..85afd9410 100644 --- a/cluster/router/tag/tag_router.go +++ b/cluster/router/tag/tag_router.go @@ -19,6 +19,7 @@ package tag import ( perrors "github.com/pkg/errors" + "strconv" ) import ( @@ -67,7 +68,7 @@ func (c *tagRouter) Priority() int64 { func filterUsingStaticTag(invokers []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker { if tag, ok := invocation.Attachments()[constant.Tagkey]; ok { - result := make([]protocol.Invoker, 0) + result := make([]protocol.Invoker, 0, 8) for _, v := range invokers { if v.GetUrl().GetParam(constant.Tagkey, "") == tag { result = append(result, v) @@ -82,8 +83,8 @@ func filterUsingStaticTag(invokers []protocol.Invoker, url *common.URL, invocati } } func isForceUseTag(url *common.URL, invocation protocol.Invocation) bool { - if invocation.AttachmentsByKey(constant.ForceUseTag, url.GetParam(constant.ForceUseTag, "false")) == "true" { - return true + if b, e := strconv.ParseBool(invocation.AttachmentsByKey(constant.ForceUseTag, url.GetParam(constant.ForceUseTag, "false"))); e == nil { + return b } return false } diff --git a/cluster/router/tag/tag_router_test.go b/cluster/router/tag/tag_router_test.go index bf2324a12..d042aae74 100644 --- a/cluster/router/tag/tag_router_test.go +++ b/cluster/router/tag/tag_router_test.go @@ -19,11 +19,17 @@ package tag import ( "context" + "testing" +) + +import ( + "github.com/stretchr/testify/assert" +) + +import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/protocol" "github.com/apache/dubbo-go/protocol/invocation" - "github.com/stretchr/testify/assert" - "testing" ) type MockInvoker struct { @@ -100,6 +106,10 @@ func TestTagRouter_Route_force(t *testing.T) { inv.SetAttachments("dubbo.tag", "guangzhou") invRst2 := tagRouter.Route(invokers, &u1, inv) assert.Equal(t, 0, len(invRst2)) + inv.SetAttachments("dubbo.force.tag", "false") + inv.SetAttachments("dubbo.tag", "guangzhou") + invRst3 := tagRouter.Route(invokers, &u1, inv) + assert.Equal(t, 3, len(invRst3)) } func TestTagRouter_Route_noForce(t *testing.T) { @@ -129,4 +139,7 @@ func TestTagRouter_Route_noForce(t *testing.T) { inv.SetAttachments("dubbo.force.tag", "true") invRst1 := tagRouter.Route(invokers, &u1, inv) assert.Equal(t, 0, len(invRst1)) + inv.SetAttachments("dubbo.force.tag", "false") + invRst2 := tagRouter.Route(invokers, &u1, inv) + assert.Equal(t, 3, len(invRst2)) } -- GitLab