Search in sources :

Example 1 with ScriptRouterFactory

use of com.alibaba.dubbo.rpc.cluster.router.script.ScriptRouterFactory in project dubbo by alibaba.

the class ScriptRouterTest method testRoute_ReturnAll.

@Test
public void testRoute_ReturnAll() {
    Router router = new ScriptRouterFactory().getRouter(getRouteUrl("function route(op1,op2){return op1} route(invokers)"));
    List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
    invokers.add(new MockInvoker<String>());
    invokers.add(new MockInvoker<String>());
    invokers.add(new MockInvoker<String>());
    List<Invoker<String>> fileredInvokers = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation());
    Assert.assertEquals(invokers, fileredInvokers);
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) ScriptRouterFactory(com.alibaba.dubbo.rpc.cluster.router.script.ScriptRouterFactory) Invoker(com.alibaba.dubbo.rpc.Invoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(com.alibaba.dubbo.rpc.cluster.Router) Test(org.junit.Test)

Example 2 with ScriptRouterFactory

use of com.alibaba.dubbo.rpc.cluster.router.script.ScriptRouterFactory in project dubbo by alibaba.

the class ScriptRouterTest method testRoute_PickInvokers.

@Test
public void testRoute_PickInvokers() {
    String rule = "var result = new java.util.ArrayList(invokers.size());" + "for (i=0;i<invokers.size(); i++){ " + "if (invokers.get(i).isAvailable()) {" + "result.add(invokers.get(i)) ;" + "}" + "} ; " + "return result;";
    String script = "function route(invokers,invocation,context){" + rule + "} route(invokers,invocation,context)";
    Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script));
    List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
    Invoker<String> invoker1 = new MockInvoker<String>(false);
    Invoker<String> invoker2 = new MockInvoker<String>(true);
    Invoker<String> invoker3 = new MockInvoker<String>(true);
    invokers.add(invoker1);
    invokers.add(invoker2);
    invokers.add(invoker3);
    List<Invoker<String>> fileredInvokers = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation());
    Assert.assertEquals(2, fileredInvokers.size());
    Assert.assertEquals(invoker2, fileredInvokers.get(0));
    Assert.assertEquals(invoker3, fileredInvokers.get(1));
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) ScriptRouterFactory(com.alibaba.dubbo.rpc.cluster.router.script.ScriptRouterFactory) Invoker(com.alibaba.dubbo.rpc.Invoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(com.alibaba.dubbo.rpc.cluster.Router) Test(org.junit.Test)

Aggregations

Invoker (com.alibaba.dubbo.rpc.Invoker)2 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)2 Router (com.alibaba.dubbo.rpc.cluster.Router)2 MockInvoker (com.alibaba.dubbo.rpc.cluster.router.MockInvoker)2 ScriptRouterFactory (com.alibaba.dubbo.rpc.cluster.router.script.ScriptRouterFactory)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2