Skip to content
Snippets Groups Projects
Commit 70c01a5b authored by watermelo's avatar watermelo
Browse files

Add: add unit test for dynamic tag

parent 8580ad07
No related branches found
No related tags found
No related merge requests found
......@@ -24,7 +24,6 @@ import (
)
import (
_ "github.com/apache/dubbo-go/config_center/zookeeper"
"github.com/stretchr/testify/assert"
)
......@@ -34,6 +33,7 @@ import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/config_center"
_ "github.com/apache/dubbo-go/config_center/zookeeper"
"github.com/apache/dubbo-go/remoting"
"github.com/apache/dubbo-go/remoting/zookeeper"
)
......
......@@ -57,8 +57,8 @@ func getRule(rawRule string) (*RouterRule, error) {
}
func (t *RouterRule) init() {
t.addressToTagNames = make(map[string][]string)
t.tagNameToAddresses = make(map[string][]string)
t.addressToTagNames = make(map[string][]string, 8)
t.tagNameToAddresses = make(map[string][]string, 8)
for _, tag := range t.Tags {
for _, address := range tag.Addresses {
t.addressToTagNames[address] = append(t.addressToTagNames[address], tag.Name)
......@@ -68,7 +68,7 @@ func (t *RouterRule) init() {
}
func (t *RouterRule) getAddresses() []string {
var result []string
var result = make([]string, 0, 8*len(t.Tags))
for _, tag := range t.Tags {
result = append(result, tag.Addresses...)
}
......@@ -76,7 +76,7 @@ func (t *RouterRule) getAddresses() []string {
}
func (t *RouterRule) getTagNames() []string {
var result []string
var result = make([]string, 0, len(t.Tags))
for _, tag := range t.Tags {
result = append(result, tag.Name)
}
......
......@@ -19,7 +19,6 @@ package tag
import (
"errors"
"fmt"
"net"
"strconv"
"strings"
......@@ -70,7 +69,6 @@ func (c *tagRouter) SetApplication(app string) {
}
func (c *tagRouter) tagRouterRuleCopy() RouterRule {
fmt.Println(c.tagRouterRule, "fuck")
routerRule := *c.tagRouterRule
return routerRule
}
......@@ -96,7 +94,8 @@ func (c *tagRouter) Route(invokers []protocol.Invoker, url *common.URL, invocati
result []protocol.Invoker
addresses []string
)
if tag != "" {
// if we are requesting for a Provider with a specific tag
if len(tag) > 0 {
addresses, _ = tagRouterRuleCopy.getTagNameToAddresses()[tag]
// filter by dynamic tag group first
if len(addresses) > 0 {
......@@ -159,9 +158,9 @@ func (c *tagRouter) Route(invokers []protocol.Invoker, url *common.URL, invocati
if len(result) == 0 {
return result
}
// 2. if there are some addresses that are not in any dynamic tag group, continue to filter using the
// static tag group.
}
// 2. if there are some addresses that are not in any dynamic tag group, continue to filter using the
// static tag group.
filter := func(invoker protocol.Invoker) bool {
localTag := invoker.GetUrl().GetParam(constant.Tagkey, "")
return localTag == "" || !(tagRouterRuleCopy.hasTag(localTag))
......@@ -178,8 +177,7 @@ func (c *tagRouter) Process(event *config_center.ConfigChangeEvent) {
} else {
content, ok := event.Value.(string)
if !ok {
msg := fmt.Sprintf("Convert event content fail,raw content:[%s] ", event.Value)
logger.Error(msg)
logger.Errorf("Convert event content fail,raw content:[%s] ", event.Value)
return
}
......@@ -282,13 +280,13 @@ OUTER:
return res
}
// TODO 需要搬到 dubbogo/gost, 可以先 review
// TODO: need move to dubbogo/gost
func checkAddressMatch(addresses []string, host, port string) bool {
for _, address := range addresses {
if matchIp(address, host, port) {
return true
}
if address == constant.ANYHOST_VALUE+":"+port {
if address == net.JoinHostPort(constant.ANYHOST_VALUE, port) {
return true
}
}
......@@ -332,7 +330,7 @@ func matchIpRange(pattern, host, port string) bool {
pattern = hostAndPort[0]
// TODO 常量化
splitCharacter := "\\."
splitCharacter := "."
if !isIpv4 {
splitCharacter = ":"
}
......
......@@ -19,27 +19,43 @@ package tag
import (
"context"
"fmt"
"github.com/stretchr/testify/suite"
"testing"
"time"
)
import (
"github.com/dubbogo/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
)
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/config"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/config_center"
_ "github.com/apache/dubbo-go/config_center/zookeeper"
"github.com/apache/dubbo-go/protocol"
"github.com/apache/dubbo-go/protocol/invocation"
"github.com/apache/dubbo-go/remoting"
"github.com/apache/dubbo-go/remoting/zookeeper"
)
const (
tagRouterTestHangZhouUrl = "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&enabled=true&dubbo.tag=hangzhou"
tagRouterTestShangHaiUrl = "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&enabled=true&dubbo.tag=shanghai"
tagRouterTestBeijingUrl = "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&enabled=true&dubbo.tag=beijing"
tagRouterTestEnabledBeijingUrl = "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&enabled=false&dubbo.tag=beijing"
tagRouterTestUserConsumer = "dubbo://127.0.0.1:20000/com.ikurento.user.UserConsumer?interface=com.ikurento.user.UserConsumer&group=&version=2.6.0&enabled=true"
tagRouterTestUserConsumerTag = "dubbo://127.0.0.1:20000/com.ikurento.user.UserConsumer?interface=com.ikurento.user.UserConsumer&group=&version=2.6.0&enabled=true&dubbo.force.tag=true"
tagRouterTestHangZhouUrl = "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&enabled=true&dubbo.tag=hangzhou&remote.application=test-tag"
tagRouterTestShangHaiUrl = "dubbo://127.0.0.1:20002/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&enabled=true&dubbo.tag=shanghai&remote.application=test-tag"
tagRouterTestBeijingUrl = "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&enabled=true&dubbo.tag=beijing&remote.application=test-tag"
tagRouterTestEnabledBeijingUrl = "dubbo://127.0.0.1:20004/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0&enabled=false&dubbo.tag=beijing&remote.application=test-tag"
tagRouterTestUserConsumer = "dubbo://127.0.0.1:20005/com.ikurento.user.UserConsumer?interface=com.ikurento.user.UserConsumer&group=&version=2.6.0&enabled=true&remote.application=test-tag"
tagRouterTestUserConsumerTag = "dubbo://127.0.0.1:20000/com.ikurento.user.UserConsumer?interface=com.ikurento.user.UserConsumer&group=&version=2.6.0&enabled=true&dubbo.force.tag=true&remote.application=test-tag"
tagRouterTestDynamicIpv4Provider1 = "dubbo://127.0.0.1:20001/com.ikurento.user.UserConsumer?interface=com.ikurento.user.UserConsumer&group=&version=2.6.0&enabled=true&remote.application=test-tag"
tagRouterTestDynamicIpv4Provider2 = "dubbo://127.0.0.1:20002/com.ikurento.user.UserConsumer?interface=com.ikurento.user.UserConsumer&group=&version=2.6.0&enabled=true&remote.application=test-tag"
tagRouterTestDynamicIpv4Provider3 = "dubbo://127.0.0.1:20003/com.ikurento.user.UserConsumer?interface=com.ikurento.user.UserConsumer&group=&version=2.6.0&enabled=true&remote.application=test-tag"
tagRouterTestDynamicIpv4Provider4 = "dubbo://127.0.0.1:20004/com.ikurento.user.UserConsumer?interface=com.ikurento.user.UserConsumer&group=&version=2.6.0&enabled=true&remote.application=test-tag&dubbo.tag=tag4"
tagRouterTestDynamicIpv4Provider5 = "dubbo://127.0.0.1:20005/com.ikurento.user.UserConsumer?interface=com.ikurento.user.UserConsumer&group=&version=2.6.0&enabled=true&remote.application=test-tag&dubbo.tag=tag5"
tagRouterTestDubboTag = "dubbo.tag"
tagRouterTestDubboForceTag = "dubbo.force.tag"
......@@ -47,6 +63,15 @@ const (
tagRouterTestGuangZhou = "guangzhou"
tagRouterTestFalse = "false"
tagRouterTestTrue = "true"
routerPath = "/dubbo/config/dubbo/test-tag.tag-router"
routerLocalIP = "127.0.0.1"
routerZk = "zookeeper"
)
var (
zkFormat = "zookeeper://%s:%d"
conditionFormat = "condition://%s/com.foo.BarService"
)
// MockInvoker is only mock the Invoker to support test tagRouter
......@@ -196,3 +221,177 @@ func TestFilterInvoker(t *testing.T) {
res2 := filterInvoker(invokers, filterTag, filterEnabled)
assert.Equal(t, []protocol.Invoker{inv4}, res2)
}
type DynamicTagRouter struct {
suite.Suite
rule *RouterRule
route *tagRouter
zkClient *zookeeper.ZookeeperClient
testCluster *zk.TestCluster
invokers []protocol.Invoker
url *common.URL
}
func TestDynamicTagRouter(t *testing.T) {
dtg := &DynamicTagRouter{}
u1, _ := common.NewURL(tagRouterTestDynamicIpv4Provider1)
u2, _ := common.NewURL(tagRouterTestDynamicIpv4Provider2)
u3, _ := common.NewURL(tagRouterTestDynamicIpv4Provider3)
u4, _ := common.NewURL(tagRouterTestDynamicIpv4Provider4)
u5, _ := common.NewURL(tagRouterTestDynamicIpv4Provider5)
inv1 := NewMockInvoker(u1)
inv2 := NewMockInvoker(u2)
inv3 := NewMockInvoker(u3)
inv4 := NewMockInvoker(u4)
inv5 := NewMockInvoker(u5)
dtg.invokers = append(dtg.invokers, inv1, inv2, inv3, inv4, inv5)
suite.Run(t, dtg)
}
func (suite *DynamicTagRouter) SetupTest() {
var err error
testYML := `enabled: true
scope: application
force: true
runtime: false
valid: true
priority: 1
key: demo-provider
tags:
- name: tag1
addresses: ["127.0.0.1:20001"]
- name: tag2
addresses: ["127.0.0.1:20002"]
- name: tag3
addresses: ["127.0.0.1:20003", "127.0.0.1:20004"]
`
ts, z, _, err := zookeeper.NewMockZookeeperClient("test", 15*time.Second)
suite.NoError(err)
err = z.Create(routerPath)
suite.NoError(err)
suite.zkClient = z
suite.testCluster = ts
_, err = z.Conn.Set(routerPath, []byte(testYML), 0)
suite.NoError(err)
zkUrl, _ := common.NewURL(fmt.Sprintf(zkFormat, routerLocalIP, suite.testCluster.Servers[0].Port))
configuration, err := extension.GetConfigCenterFactory(routerZk).GetDynamicConfiguration(&zkUrl)
config.GetEnvInstance().SetDynamicConfiguration(configuration)
suite.Nil(err)
suite.NotNil(configuration)
url, e1 := common.NewURL(tagRouterTestUserConsumerTag)
suite.Nil(e1)
tagRouter, err := NewTagRouter(&url)
suite.Nil(err)
suite.NotNil(tagRouter)
suite.route = tagRouter
suite.url = &url
}
func (suite *DynamicTagRouter) TearDownTest() {
suite.zkClient.Close()
suite.testCluster.Stop()
}
func (suite *DynamicTagRouter) TestDynamicTagRouterSetByIPv4() {
invokers := suite.invokers
suite.route.Notify(invokers)
suite.NotNil(suite.route.tagRouterRule)
consumer := &invocation.RPCInvocation{}
consumer.SetAttachments(tagRouterTestDubboTag, "tag1")
targetInvokers := suite.route.Route(invokers, suite.url, consumer)
suite.Equal(1, len(targetInvokers))
suite.Equal(targetInvokers[0], suite.invokers[0])
consumer.SetAttachments(tagRouterTestDubboTag, "tag3")
targetInvokers = suite.route.Route(invokers, suite.url, consumer)
suite.Equal(2, len(targetInvokers))
suite.Equal(targetInvokers, []protocol.Invoker{suite.invokers[2], suite.invokers[3]})
}
func (suite *DynamicTagRouter) TestDynamicTagRouterStaticTag() {
invokers := suite.invokers
consumer := &invocation.RPCInvocation{}
consumer.SetAttachments(tagRouterTestDubboTag, "tag4")
targetInvokers := suite.route.Route(invokers, suite.url, consumer)
suite.Equal(1, len(targetInvokers))
suite.Equal(targetInvokers[0], suite.invokers[3])
}
// Teas no tag and return a address are not in dynamic tag group
func (suite *DynamicTagRouter) TestDynamicTagRouterByNoTagAndAddressMatch() {
invokers := suite.invokers
suite.route.Notify(invokers)
suite.NotNil(suite.route.tagRouterRule)
consumer := &invocation.RPCInvocation{}
targetInvokers := suite.route.Route(invokers, suite.url, consumer)
suite.Equal(1, len(targetInvokers))
suite.Equal(targetInvokers[0], suite.invokers[4])
// test if there are some addresses that are not in any dynamic tag group, continue to filter using the static tag group.
consumer.SetAttachments(tagRouterTestDubboTag, "tag5")
targetInvokers = suite.route.Route(invokers, suite.url, consumer)
suite.Equal(1, len(targetInvokers))
suite.Equal(targetInvokers[0], suite.invokers[4])
}
func (suite *DynamicTagRouter) TestTODO() {
testYML := `enabled: true
scope: application
force: true
runtime: false
valid: true
priority: 1
key: demo-provider
tags:
- name: tag1
addresses: ["127.0.0.1:20001"]
- name: tag2
addresses: ["127.0.0.1:20002"]
- name: tag3
addresses: ["127.0.0.1:20003", "127.0.0.1:20004"]
`
_, err := suite.zkClient.Conn.Set(routerPath, []byte(testYML), 1)
suite.NoError(err)
zkUrl, _ := common.NewURL(fmt.Sprintf(zkFormat, routerLocalIP, suite.testCluster.Servers[0].Port))
configuration, err := extension.GetConfigCenterFactory(routerZk).GetDynamicConfiguration(&zkUrl)
config.GetEnvInstance().SetDynamicConfiguration(configuration)
}
func TestProcess(t *testing.T) {
u1, err := common.NewURL(tagRouterTestUserConsumerTag)
assert.Nil(t, err)
tagRouter, e := NewTagRouter(&u1)
assert.Nil(t, e)
assert.NotNil(t, tagRouter)
testYML := `
scope: application
force: true
runtime: false
enabled: true
valid: true
priority: 1
key: demo-provider
tags:
- name: beijing
addresses: [192.168.1.1, 192.168.1.2]
- name: hangzhou
addresses: [192.168.1.3, 192.168.1.4]
`
tagRouter.Process(&config_center.ConfigChangeEvent{Value: testYML, ConfigType: remoting.EventTypeAdd})
assert.NotNil(t, tagRouter.tagRouterRule)
assert.Equal(t, []string{"beijing", "hangzhou"}, tagRouter.tagRouterRule.getTagNames())
assert.Equal(t, []string{"192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4"}, tagRouter.tagRouterRule.getAddresses())
assert.Equal(t, []string{"192.168.1.3", "192.168.1.4"}, tagRouter.tagRouterRule.getTagNameToAddresses()["hangzhou"])
assert.Equal(t, []string{"beijing"}, tagRouter.tagRouterRule.getAddressToTagNames()["192.168.1.1"])
tagRouter.Process(&config_center.ConfigChangeEvent{ConfigType: remoting.EventTypeDel})
assert.Nil(t, tagRouter.tagRouterRule)
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment