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

add test case

parent 99e755cc
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,6 @@ import (
import (
"github.com/dubbogo/gost/container/set"
perrors "github.com/pkg/errors"
"go.uber.org/atomic"
)
......@@ -34,7 +33,6 @@ import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/protocol"
)
var routerURLSet = gxset.NewSet()
......@@ -48,6 +46,15 @@ type BaseDirectory struct {
routerChain router.Chain
}
// NewBaseDirectory Create BaseDirectory with URL
func NewBaseDirectory(url *common.URL) BaseDirectory {
return BaseDirectory{
url: url,
destroyed: atomic.NewBool(false),
routerChain: &chain.RouterChain{},
}
}
// RouterChain Return router chain in directory
func (dir *BaseDirectory) RouterChain() router.Chain {
return dir.routerChain
......@@ -60,15 +67,6 @@ func (dir *BaseDirectory) SetRouterChain(routerChain router.Chain) {
dir.routerChain = routerChain
}
// NewBaseDirectory Create BaseDirectory with URL
func NewBaseDirectory(url *common.URL) BaseDirectory {
return BaseDirectory{
url: url,
destroyed: atomic.NewBool(false),
routerChain: &chain.RouterChain{},
}
}
// GetUrl Get URL
func (dir *BaseDirectory) GetUrl() common.URL {
return *dir.url
......@@ -133,17 +131,3 @@ func GetRouterURLSet() *gxset.HashSet {
func AddRouterURLSet(url *common.URL) {
routerURLSet.Add(url)
}
// BuildRouterChain build router chain by invokers
func (dir *staticDirectory) BuildRouterChain(invokers []protocol.Invoker) error {
if len(invokers) == 0 {
return perrors.Errorf("invokers == null")
}
url := invokers[0].GetUrl()
routerChain, e := chain.NewRouterChain(&url)
if e != nil {
return e
}
dir.SetRouterChain(routerChain)
return nil
}
/*
* 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.
*/
package directory
import (
"encoding/base64"
"fmt"
"testing"
)
import (
gxnet "github.com/dubbogo/gost/net"
"github.com/stretchr/testify/assert"
)
import (
_ "github.com/apache/dubbo-go/cluster/router/condition"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
)
func TestNewBaseDirectory(t *testing.T) {
url, _ := common.NewURL(fmt.Sprintf("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider"))
directory := NewBaseDirectory(&url)
assert.NotNil(t, directory)
assert.Equal(t, url, directory.GetUrl())
assert.Equal(t, &url, directory.GetDirectoryUrl())
}
func TestBuildRouterChain(t *testing.T) {
url, _ := common.NewURL(fmt.Sprintf("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider"))
directory := NewBaseDirectory(&url)
assert.NotNil(t, directory)
localIP, _ := gxnet.GetLocalIP()
rule := base64.URLEncoding.EncodeToString([]byte("true => " + " host = " + localIP))
routeURL := getRouteUrl(rule)
routerURLs := make([]*common.URL, 0)
routerURLs = append(routerURLs, routeURL)
directory.SetRouters(routerURLs)
chain := directory.RouterChain()
assert.NotNil(t, chain)
}
func getRouteUrl(rule string) *common.URL {
url, _ := common.NewURL("condition://0.0.0.0/com.foo.BarService")
url.AddParam("rule", rule)
url.AddParam("force", "true")
url.AddParam(constant.ROUTER_KEY, "router")
return &url
}
......@@ -18,6 +18,11 @@
package directory
import (
perrors "github.com/pkg/errors"
)
import (
"github.com/apache/dubbo-go/cluster/router/chain"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/protocol"
)
......@@ -76,3 +81,17 @@ func (dir *staticDirectory) Destroy() {
dir.invokers = []protocol.Invoker{}
})
}
// BuildRouterChain build router chain by invokers
func (dir *staticDirectory) BuildRouterChain(invokers []protocol.Invoker) error {
if len(invokers) == 0 {
return perrors.Errorf("invokers == null")
}
url := invokers[0].GetUrl()
routerChain, e := chain.NewRouterChain(&url)
if e != nil {
return e
}
dir.SetRouterChain(routerChain)
return nil
}
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