Skip to content

Commit

Permalink
add test, fix comment
Browse files Browse the repository at this point in the history
Signed-off-by: YarBor <[email protected]>
  • Loading branch information
YarBor committed Jul 26, 2024
1 parent 7512904 commit 672208a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cluster/router/script/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ func (s *ScriptRouter) Process(event *config_center.ConfigChangeEvent) {
in, err := ins.GetInstances(s.scriptType)
if err != nil {
logger.Errorf("GetInstances failed: %v", err)
s.enabled = false
return
}
if s.enabled {
err = in.Compile(s.rawScript)
Expand Down
79 changes: 73 additions & 6 deletions cluster/router/script/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var url3 = func() *common.URL {
return i
}

func getRouteArgs() ([]protocol.Invoker, protocol.Invocation, context.Context) {
func getRouteCheckArgs() ([]protocol.Invoker, protocol.Invocation, context.Context) {
return []protocol.Invoker{
protocol.NewBaseInvoker(url1()), protocol.NewBaseInvoker(url2()), protocol.NewBaseInvoker(url3()),
}, invocation.NewRPCInvocation("GetUser", nil, map[string]interface{}{
Expand Down Expand Up @@ -94,7 +94,7 @@ script: |
`},
args: func() args {
res := args{}
res.invokers, res.invocation, _ = getRouteArgs()
res.invokers, res.invocation, _ = getRouteCheckArgs()
return res
}(),
want: func(invokers []protocol.Invoker) bool {
Expand All @@ -105,6 +105,31 @@ script: |
}
return true
},
}, {
name: "disable test",
fields: fields{cfgContent: `configVersion: v3.0
key: dubbo.io
type: javascript
enabled: false
script: |
(function route(invokers,invocation,context) {
var result = [];
for (var i = 0; i < invokers.length; i++) {
invokers[i].GetURL().Port = "20001"
result.push(invokers[i]);
}
return result;
}(invokers,invocation,context));
`},
args: func() args {
res := args{}
res.invokers, res.invocation, _ = getRouteCheckArgs()
return res
}(),
want: func(invokers []protocol.Invoker) bool {
expect_invokers, _, _ := getRouteCheckArgs()
return checkInvokersSame(invokers, expect_invokers)
},
}, {
name: "bad input",
fields: fields{cfgContent: `configVersion: v3.0
Expand All @@ -123,11 +148,12 @@ script: |
`},
args: func() args {
res := args{}
res.invokers, res.invocation, _ = getRouteArgs()
res.invokers, res.invocation, _ = getRouteCheckArgs()
return res
}(),
want: func(invokers []protocol.Invoker) bool {
return true
expect_invokers, _, _ := getRouteCheckArgs()
return checkInvokersSame(invokers, expect_invokers)
},
}, {
name: "bad call and recover",
Expand All @@ -151,11 +177,37 @@ script: |
`},
args: func() args {
res := args{}
res.invokers, res.invocation, _ = getRouteArgs()
res.invokers, res.invocation, _ = getRouteCheckArgs()
return res
}(),
want: func(invokers []protocol.Invoker) bool {
return true
expect_invokers, _, _ := getRouteCheckArgs()
return checkInvokersSame(invokers, expect_invokers)
},
}, {
name: "bad type",
fields: fields{cfgContent: `configVersion: v3.0
key: dubbo.io
type: errorType # <---
enabled: true
script: |
(function route(invokers,invocation,context) {
var result = [];
for (var i = 0; i < invokers.length; i++) {
invokers[i].GetURL().Port = "20001"
result.push(invokers[i]);
}
return result;
}(invokers,invocation,context));
`},
args: func() args {
res := args{}
res.invokers, res.invocation, _ = getRouteCheckArgs()
return res
}(),
want: func(invokers []protocol.Invoker) bool {
expect_invokers, _, _ := getRouteCheckArgs()
return checkInvokersSame(invokers, expect_invokers)
},
},
}
Expand All @@ -168,3 +220,18 @@ script: |
})
}
}

func checkInvokersSame(invokers []protocol.Invoker, otherInvokers []protocol.Invoker) bool {
k := map[string]struct{}{}
for _, invoker := range otherInvokers {
k[invoker.GetURL().String()] = struct{}{}
}
for _, invoker := range invokers {
_, ok := k[invoker.GetURL().String()]
if !ok {
return false
}
delete(k, invoker.GetURL().String())
}
return len(k) == 0
}

0 comments on commit 672208a

Please sign in to comment.