Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
22a7f0099
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Summer2022
22a7f0099
Commits
86e35ced
Commit
86e35ced
authored
5 years ago
by
邹毅贤
Browse files
Options
Downloads
Patches
Plain Diff
add test case
parent
8df93da6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
cluster/router/chain/chain.go
+0
-3
0 additions, 3 deletions
cluster/router/chain/chain.go
cluster/router/chain/chain_test.go
+106
-4
106 additions, 4 deletions
cluster/router/chain/chain_test.go
cluster/router/condition/app_router.go
+7
-0
7 additions, 0 deletions
cluster/router/condition/app_router.go
with
113 additions
and
7 deletions
cluster/router/chain/chain.go
+
0
−
3
View file @
86e35ced
...
@@ -80,9 +80,6 @@ func (c *RouterChain) AddRouters(routers []router.Router) {
...
@@ -80,9 +80,6 @@ func (c *RouterChain) AddRouters(routers []router.Router) {
// NewRouterChain Use url to init router chain
// NewRouterChain Use url to init router chain
// Loop routerFactories and call NewRouter method
// Loop routerFactories and call NewRouter method
func
NewRouterChain
(
url
*
common
.
URL
)
(
*
RouterChain
,
error
)
{
func
NewRouterChain
(
url
*
common
.
URL
)
(
*
RouterChain
,
error
)
{
if
url
==
nil
{
return
nil
,
perrors
.
Errorf
(
"No route URL for create router chain!"
)
}
routerFactories
:=
extension
.
GetRouterFactories
()
routerFactories
:=
extension
.
GetRouterFactories
()
if
len
(
routerFactories
)
==
0
{
if
len
(
routerFactories
)
==
0
{
return
nil
,
perrors
.
Errorf
(
"No routerFactory exits , create one please"
)
return
nil
,
perrors
.
Errorf
(
"No routerFactory exits , create one please"
)
...
...
This diff is collapsed.
Click to expand it.
cluster/router/chain/chain_test.go
+
106
−
4
View file @
86e35ced
...
@@ -19,6 +19,7 @@ package chain
...
@@ -19,6 +19,7 @@ package chain
import
(
import
(
"encoding/base64"
"encoding/base64"
"fmt"
"strconv"
"strconv"
"testing"
"testing"
"time"
"time"
...
@@ -31,12 +32,13 @@ import (
...
@@ -31,12 +32,13 @@ import (
import
(
import
(
"github.com/apache/dubbo-go/cluster/router"
"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/cluster/router/condition"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/config"
"github.com/apache/dubbo-go/common/config"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/extension"
_
"github.com/apache/dubbo-go/config_center/zookeeper"
_
"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/zookeeper"
"github.com/apache/dubbo-go/remoting/zookeeper"
)
)
...
@@ -86,8 +88,8 @@ conditions:
...
@@ -86,8 +88,8 @@ conditions:
func
TestNewRouterChainURLNil
(
t
*
testing
.
T
)
{
func
TestNewRouterChainURLNil
(
t
*
testing
.
T
)
{
chain
,
err
:=
NewRouterChain
(
nil
)
chain
,
err
:=
NewRouterChain
(
nil
)
assert
.
Error
(
t
,
err
)
assert
.
No
Error
(
t
,
err
)
assert
.
Nil
(
t
,
chain
)
assert
.
Not
Nil
(
t
,
chain
)
}
}
func
TestRouterChain_AddRouters
(
t
*
testing
.
T
)
{
func
TestRouterChain_AddRouters
(
t
*
testing
.
T
)
{
...
@@ -129,11 +131,111 @@ conditions:
...
@@ -129,11 +131,111 @@ conditions:
assert
.
Equal
(
t
,
3
,
len
(
chain
.
routers
))
assert
.
Equal
(
t
,
3
,
len
(
chain
.
routers
))
}
}
func
TestRouterChain_Route
(
t
*
testing
.
T
)
{
ts
,
z
,
_
,
err
:=
zookeeper
.
NewMockZookeeperClient
(
"test"
,
15
*
time
.
Second
)
defer
ts
.
Stop
()
defer
z
.
Close
()
zkUrl
,
_
:=
common
.
NewURL
(
"zookeeper://127.0.0.1:"
+
strconv
.
Itoa
(
ts
.
Servers
[
0
]
.
Port
))
configuration
,
err
:=
extension
.
GetConfigCenterFactory
(
"zookeeper"
)
.
GetDynamicConfiguration
(
&
zkUrl
)
config
.
GetEnvInstance
()
.
SetDynamicConfiguration
(
configuration
)
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
)
invokers
:=
[]
protocol
.
Invoker
{}
dubboURL
,
_
:=
common
.
NewURL
(
fmt
.
Sprintf
(
"dubbo://1.2.3.4:20000/com.foo.BarService"
))
invokers
=
append
(
invokers
,
protocol
.
NewBaseInvoker
(
dubboURL
))
targetURL
,
_
:=
common
.
NewURL
(
fmt
.
Sprintf
(
"consumer://1.1.1.1/com.foo.BarService"
))
inv
:=
&
invocation
.
RPCInvocation
{}
finalInvokers
:=
chain
.
Route
(
invokers
,
&
targetURL
,
inv
)
assert
.
Equal
(
t
,
1
,
len
(
finalInvokers
))
}
func
TestRouterChain_Route_AppRouter
(
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
)
testyml
:=
`enabled: true
force: true
runtime: false
conditions:
- => host = 1.1.1.1 => host != 1.2.3.4
`
_
,
err
=
z
.
Conn
.
Set
(
"/dubbo/config/dubbo/test-condition.condition-router"
,
[]
byte
(
testyml
),
0
)
assert
.
NoError
(
t
,
err
)
defer
ts
.
Stop
()
defer
z
.
Close
()
zkUrl
,
_
:=
common
.
NewURL
(
"zookeeper://127.0.0.1:"
+
strconv
.
Itoa
(
ts
.
Servers
[
0
]
.
Port
))
configuration
,
err
:=
extension
.
GetConfigCenterFactory
(
"zookeeper"
)
.
GetDynamicConfiguration
(
&
zkUrl
)
config
.
GetEnvInstance
()
.
SetDynamicConfiguration
(
configuration
)
chain
,
err
:=
NewRouterChain
(
getConditionRouteUrl
(
"test-condition"
))
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
2
,
len
(
chain
.
routers
))
invokers
:=
[]
protocol
.
Invoker
{}
dubboURL
,
_
:=
common
.
NewURL
(
fmt
.
Sprintf
(
"dubbo://1.2.3.4:20000/com.foo.BarService"
))
invokers
=
append
(
invokers
,
protocol
.
NewBaseInvoker
(
dubboURL
))
targetURL
,
_
:=
common
.
NewURL
(
fmt
.
Sprintf
(
"consumer://1.1.1.1/com.foo.BarService"
))
inv
:=
&
invocation
.
RPCInvocation
{}
finalInvokers
:=
chain
.
Route
(
invokers
,
&
targetURL
,
inv
)
assert
.
Equal
(
t
,
0
,
len
(
finalInvokers
))
}
func
TestRouterChain_Route_NoRoute
(
t
*
testing
.
T
)
{
ts
,
z
,
_
,
err
:=
zookeeper
.
NewMockZookeeperClient
(
"test"
,
15
*
time
.
Second
)
defer
ts
.
Stop
()
defer
z
.
Close
()
zkUrl
,
_
:=
common
.
NewURL
(
"zookeeper://127.0.0.1:"
+
strconv
.
Itoa
(
ts
.
Servers
[
0
]
.
Port
))
configuration
,
err
:=
extension
.
GetConfigCenterFactory
(
"zookeeper"
)
.
GetDynamicConfiguration
(
&
zkUrl
)
config
.
GetEnvInstance
()
.
SetDynamicConfiguration
(
configuration
)
chain
,
err
:=
NewRouterChain
(
getConditionNoRouteUrl
(
"test-condition"
))
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
1
,
len
(
chain
.
routers
))
url
:=
getConditionRouteUrl
(
"test-condition"
)
assert
.
NotNil
(
t
,
url
)
invokers
:=
[]
protocol
.
Invoker
{}
dubboURL
,
_
:=
common
.
NewURL
(
fmt
.
Sprintf
(
"dubbo://1.2.3.4:20000/com.foo.BarService"
))
invokers
=
append
(
invokers
,
protocol
.
NewBaseInvoker
(
dubboURL
))
targetURL
,
_
:=
common
.
NewURL
(
fmt
.
Sprintf
(
"consumer://1.1.1.1/com.foo.BarService"
))
inv
:=
&
invocation
.
RPCInvocation
{}
finalInvokers
:=
chain
.
Route
(
invokers
,
&
targetURL
,
inv
)
assert
.
Equal
(
t
,
0
,
len
(
finalInvokers
))
}
func
getConditionNoRouteUrl
(
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 = 1.1.1.1 => host != 1.2.3.4"
))
url
.
AddParam
(
constant
.
RULE_KEY
,
rule
)
return
&
url
}
func
getConditionRouteUrl
(
applicationKey
string
)
*
common
.
URL
{
func
getConditionRouteUrl
(
applicationKey
string
)
*
common
.
URL
{
url
,
_
:=
common
.
NewURL
(
"condition://0.0.0.0/com.foo.BarService"
)
url
,
_
:=
common
.
NewURL
(
"condition://0.0.0.0/com.foo.BarService"
)
url
.
AddParam
(
"application"
,
applicationKey
)
url
.
AddParam
(
"application"
,
applicationKey
)
url
.
AddParam
(
"force"
,
"true"
)
url
.
AddParam
(
"force"
,
"true"
)
rule
:=
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
"host = 1
27.0.0.1 =>
"
))
rule
:=
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
"host = 1
.1.1.1 => host = 1.2.3.4
"
))
url
.
AddParam
(
constant
.
RULE_KEY
,
rule
)
url
.
AddParam
(
constant
.
RULE_KEY
,
rule
)
return
&
url
return
&
url
}
}
...
...
This diff is collapsed.
Click to expand it.
cluster/router/condition/app_router.go
+
7
−
0
View file @
86e35ced
...
@@ -17,6 +17,10 @@
...
@@ -17,6 +17,10 @@
package
condition
package
condition
import
(
perrors
"github.com/pkg/errors"
)
import
(
import
(
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/constant"
...
@@ -34,6 +38,9 @@ type AppRouter struct {
...
@@ -34,6 +38,9 @@ type AppRouter struct {
// NewAppRouter Init AppRouter by url
// NewAppRouter Init AppRouter by url
func
NewAppRouter
(
url
*
common
.
URL
)
(
*
AppRouter
,
error
)
{
func
NewAppRouter
(
url
*
common
.
URL
)
(
*
AppRouter
,
error
)
{
if
url
==
nil
{
return
nil
,
perrors
.
Errorf
(
"No route URL for create app router!"
)
}
appRouter
,
err
:=
newListenableRouter
(
url
,
url
.
GetParam
(
constant
.
APPLICATION_KEY
,
""
))
appRouter
,
err
:=
newListenableRouter
(
url
,
url
.
GetParam
(
constant
.
APPLICATION_KEY
,
""
))
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment