Newer
Older
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/config"
"github.com/apache/dubbo-go/common/extension"
_ "github.com/apache/dubbo-go/config_center/zookeeper"
"github.com/apache/dubbo-go/remoting/zookeeper"
)
func TestNewRouterChain(t *testing.T) {
ts, z, _, err := zookeeper.NewMockZookeeperClient("test", 15*time.Second)
assert.NoError(t, err)
err = z.Create("/dubbo/config/dubbo/test-condition.condition-router")
assert.NoError(t, err)
force: true
runtime: false
conditions:
- => host != 172.22.3.91
`
_, err = z.Conn.Set("/dubbo/config/dubbo/test-condition.condition-router", []byte(testyml), 0)
zkUrl, _ := common.NewURL("zookeeper://127.0.0.1:" + strconv.Itoa(ts.Servers[0].Port))
configuration, err := extension.GetConfigCenterFactory("zookeeper").GetDynamicConfiguration(&zkUrl)
chain, err := NewRouterChain(getRouteUrl("test-condition"))
assert.Nil(t, err)
assert.Equal(t, 1, len(chain.routers))
appRouter := chain.routers[0].(*condition.AppRouter)
assert.NotNil(t, appRouter)
assert.NotNil(t, appRouter.RouterRule())
rule := appRouter.RouterRule()
assert.Equal(t, "", rule.Scope)
assert.True(t, rule.Force)
assert.True(t, rule.Enabled)
assert.True(t, rule.Valid)
assert.Equal(t, testyml, rule.RawRule)
assert.Equal(t, false, rule.Runtime)
assert.Equal(t, false, rule.Dynamic)
assert.Equal(t, "", rule.Key)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
}
url, _ := common.NewURL("condition://0.0.0.0/com.foo.BarService")