Skip to content
Snippets Groups Projects
Commit 9af1ce84 authored by 邹毅贤's avatar 邹毅贤
Browse files

add test case

parent 6b06459f
No related branches found
No related tags found
No related merge requests found
......@@ -80,9 +80,12 @@ func (c *RouterChain) AddRouters(routers []router.Router) {
// NewRouterChain Use url to init router chain
// Loop routerFactories and call NewRouter method
func NewRouterChain(url *common.URL) (*RouterChain, error) {
if url == nil {
return nil, perrors.Errorf("No route URL for create router chain!")
}
routerFactories := extension.GetRouterFactories()
if len(routerFactories) == 0 {
return nil, perrors.Errorf("Illegal route rule!")
return nil, perrors.Errorf("No routerFactory exits , create one please")
}
routers := make([]router.Router, 0, len(routerFactories))
for key, routerFactory := range routerFactories {
......
......@@ -18,6 +18,7 @@
package chain
import (
"encoding/base64"
"strconv"
"testing"
"time"
......@@ -28,10 +29,12 @@ import (
)
import (
"github.com/apache/dubbo-go/cluster/router"
"github.com/apache/dubbo-go/cluster/router/condition"
_ "github.com/apache/dubbo-go/cluster/router/condition"
"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/zookeeper"
"github.com/apache/dubbo-go/remoting/zookeeper"
......@@ -81,6 +84,39 @@ conditions:
assert.Equal(t, "", rule.Key)
}
func TestNewRouterChainURLNil(t *testing.T) {
chain, err := NewRouterChain(nil)
assert.Error(t, err)
assert.Nil(t, chain)
}
func TestRouterChain_AddRouters(t *testing.T) {
chain, err := NewRouterChain(getConditionRouteUrl("test-condition"))
assert.Nil(t, err)
assert.Equal(t, 1, len(chain.routers))
url := getConditionRouteUrl("test-condition")
assert.NotNil(t, url)
factory := extension.GetRouterFactory(url.Protocol)
r, err := factory.NewRouter(url)
assert.Nil(t, err)
assert.NotNil(t, r)
routers := make([]router.Router, 0)
routers = append(routers, r)
chain.AddRouters(routers)
assert.Equal(t, 2, len(chain.routers))
}
func getConditionRouteUrl(applicationKey string) *common.URL {
url, _ := common.NewURL("condition://0.0.0.0/com.foo.BarService")
url.AddParam("application", applicationKey)
url.AddParam("force", "true")
rule := base64.URLEncoding.EncodeToString([]byte("host = 127.0.0.1 => "))
url.AddParam(constant.RULE_KEY, rule)
return &url
}
func getRouteUrl(applicationKey string) *common.URL {
url, _ := common.NewURL("condition://0.0.0.0/com.foo.BarService")
url.AddParam("application", applicationKey)
......
......@@ -104,7 +104,9 @@ func NewConditionRouterWithRule(rule string) (*ConditionRouter, error) {
// NewConditionRouter Init condition router by URL
func NewConditionRouter(url *common.URL) (*ConditionRouter, error) {
if url == nil {
return nil, perrors.Errorf("Illegal route URL!")
}
rule, err := url.GetParamAndDecoded(constant.RULE_KEY)
if err != nil || len(rule) == 0 {
return nil, perrors.Errorf("Illegal route rule!")
......
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