com.alibaba.dubbo.rpc.cluster.router.script.ScriptRouterFactory Java Examples

The following examples show how to use com.alibaba.dubbo.rpc.cluster.router.script.ScriptRouterFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileRouterFactory.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
public Router getRouter(URL url) {
    try {
        // Transform File URL into Script Route URL, and Load
        // file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
        String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // Replace original protocol (maybe 'file') with 'script'
        String type = null; // Use file suffix to config script type, e.g., js, groovy ...
        String path = url.getPath();
        if (path != null) {
            int i = path.lastIndexOf('.');
            if (i > 0) {
                type = path.substring(i + 1);
            }
        }
        String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));

        boolean runtime = url.getParameter(Constants.RUNTIME_KEY, false);
        URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type).addParameter(Constants.RUNTIME_KEY, runtime).addParameterAndEncoded(Constants.RULE_KEY, rule);

        return routerFactory.getRouter(script);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #2
Source File: FileRouterFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Router getRouter(URL url) {
    try {
        // File URL 转换成 其它Route URL,然后Load
        // file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
        String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // 将原类型转为协议
        String type = null; // 使用文件后缀做为类型
        String path = url.getPath();
        if (path != null) {
            int i = path.lastIndexOf('.');
            if (i > 0) {
                type = path.substring(i + 1);
            }
        }
        String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));
        URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type).addParameterAndEncoded(Constants.RULE_KEY, rule);
        
        return routerFactory.getRouter(script);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #3
Source File: ScriptRouterTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #4
Source File: ScriptRouterTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #5
Source File: FileRouterFactory.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public Router getRouter(URL url) {
    try {
        // File URL 转换成 其它Route URL,然后Load
        // file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
        String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // 将原类型转为协议
        String type = null; // 使用文件后缀做为类型
        String path = url.getPath();
        if (path != null) {
            int i = path.lastIndexOf('.');
            if (i > 0) {
                type = path.substring(i + 1);
            }
        }
        String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));
        URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type).addParameterAndEncoded(Constants.RULE_KEY, rule);
        
        return routerFactory.getRouter(script);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #6
Source File: FileRouterFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Router getRouter(URL url) {
    try {
        // File URL 转换成 其它Route URL,然后Load
        // file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
        String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // 将原类型转为协议
        String type = null; // 使用文件后缀做为类型
        String path = url.getPath();
        if (path != null) {
            int i = path.lastIndexOf('.');
            if (i > 0) {
                type = path.substring(i + 1);
            }
        }
        String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));
        URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type).addParameterAndEncoded(Constants.RULE_KEY, rule);
        
        return routerFactory.getRouter(script);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #7
Source File: ScriptRouterTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #8
Source File: FileRouterFactory.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public Router getRouter(URL url) {
    try {
        // File URL 转换成 其它Route URL,然后Load
        // file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
        String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // 将原类型转为协议
        String type = null; // 使用文件后缀做为类型
        String path = url.getPath();
        if (path != null) {
            int i = path.lastIndexOf('.');
            if (i > 0) {
                type = path.substring(i + 1);
            }
        }
        String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));
        URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type).addParameterAndEncoded(Constants.RULE_KEY, rule);
        
        return routerFactory.getRouter(script);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #9
Source File: ScriptRouterTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #10
Source File: ScriptRouterTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #11
Source File: FileRouterFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Router getRouter(URL url) {
    try {
        // File URL 转换成 其它Route URL,然后Load
        // file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
        String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // 将原类型转为协议
        String type = null; // 使用文件后缀做为类型
        String path = url.getPath();
        if (path != null) {
            int i = path.lastIndexOf('.');
            if (i > 0) {
                type = path.substring(i + 1);
            }
        }
        String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));
        URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type).addParameterAndEncoded(Constants.RULE_KEY, rule);
        
        return routerFactory.getRouter(script);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #12
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 1. notify twice, the second time notified router rules should completely replace the former one. 2. notify with
 * no router url, do nothing to current routers 3. notify with only one router url, with router=clean, clear all
 * current routers
 */
@Test
public void testNotifyRouterUrls() {
    if (isScriptUnsupported) return;
    RegistryDirectory registryDirectory = getRegistryDirectory();
    URL routerurl = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9096/");
    URL routerurl2 = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9097/");

    List<URL> serviceUrls = new ArrayList<URL>();
    // without ROUTER_KEY, the first router should not be created.
    serviceUrls.add(routerurl.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
                                                                                             "notsupported").addParameter(Constants.RULE_KEY,
                                                                                                                          "function test1(){}"));
    serviceUrls.add(routerurl2.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
                                                                                              ScriptRouterFactory.NAME).addParameter(Constants.RULE_KEY,
                                                                                                                                     "function test1(){}"));

    registryDirectory.notify(serviceUrls);
    List<Router> routers = registryDirectory.getRouters();
    //default invocation selector
    Assert.assertEquals(1+1, routers.size());
    Assert.assertEquals(ScriptRouter.class, routers.get(1).getClass());

    registryDirectory.notify(new ArrayList<URL>());
    routers = registryDirectory.getRouters();
    Assert.assertEquals(1 + 1, routers.size());
    Assert.assertEquals(ScriptRouter.class, routers.get(1).getClass());

    serviceUrls.clear();
    serviceUrls.add(routerurl.addParameter(Constants.ROUTER_KEY, Constants.ROUTER_TYPE_CLEAR));
    registryDirectory.notify(serviceUrls);
    routers = registryDirectory.getRouters();
    Assert.assertEquals(0 + 1, routers.size());
}
 
Example #13
Source File: ScriptRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #14
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 1. notify twice, the second time notified router rules should completely replace the former one. 2. notify with
 * no router url, do nothing to current routers 3. notify with only one router url, with router=clean, clear all
 * current routers
 */
@Test
public void testNotifyRouterUrls() {
    if (isScriptUnsupported) return;
    RegistryDirectory registryDirectory = getRegistryDirectory();
    URL routerurl = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9096/");
    URL routerurl2 = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9097/");

    List<URL> serviceUrls = new ArrayList<URL>();
    // without ROUTER_KEY, the first router should not be created.
    serviceUrls.add(routerurl.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
                                                                                             "notsupported").addParameter(Constants.RULE_KEY,
                                                                                                                          "function test1(){}"));
    serviceUrls.add(routerurl2.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
                                                                                              ScriptRouterFactory.NAME).addParameter(Constants.RULE_KEY,
                                                                                                                                     "function test1(){}"));

    registryDirectory.notify(serviceUrls);
    List<Router> routers = registryDirectory.getRouters();
    //default invocation selector
    Assert.assertEquals(1+1, routers.size());
    Assert.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass());

    registryDirectory.notify(new ArrayList<URL>());
    routers = registryDirectory.getRouters();
    Assert.assertEquals(1 + 1, routers.size());
    Assert.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass());

    serviceUrls.clear();
    serviceUrls.add(routerurl.addParameter(Constants.ROUTER_KEY, Constants.ROUTER_TYPE_CLEAR));
    registryDirectory.notify(serviceUrls);
    routers = registryDirectory.getRouters();
    Assert.assertEquals(0 + 1, routers.size());
}
 
Example #15
Source File: ScriptRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #16
Source File: ScriptRouterTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #17
Source File: RegistryDirectoryTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
/**
 * 1. notify twice, the second time notified router rules should completely replace the former one. 2. notify with
 * no router url, do nothing to current routers 3. notify with only one router url, with router=clean, clear all
 * current routers
 */
@Test
public void testNotifyRouterUrls() {
    if (isScriptUnsupported) return;
    RegistryDirectory registryDirectory = getRegistryDirectory();
    URL routerurl = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9096/");
    URL routerurl2 = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9097/");

    List<URL> serviceUrls = new ArrayList<URL>();
    // without ROUTER_KEY, the first router should not be created.
    serviceUrls.add(routerurl.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
                                                                                             "notsupported").addParameter(Constants.RULE_KEY,
                                                                                                                          "function test1(){}"));
    serviceUrls.add(routerurl2.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
                                                                                              ScriptRouterFactory.NAME).addParameter(Constants.RULE_KEY,
                                                                                                                                     "function test1(){}"));

    registryDirectory.notify(serviceUrls);
    List<Router> routers = registryDirectory.getRouters();
    //default invocation selector
    Assert.assertEquals(1+1, routers.size());
    Assert.assertEquals(ScriptRouter.class, routers.get(1).getClass());

    registryDirectory.notify(new ArrayList<URL>());
    routers = registryDirectory.getRouters();
    Assert.assertEquals(1 + 1, routers.size());
    Assert.assertEquals(ScriptRouter.class, routers.get(1).getClass());

    serviceUrls.clear();
    serviceUrls.add(routerurl.addParameter(Constants.ROUTER_KEY, Constants.ROUTER_TYPE_CLEAR));
    registryDirectory.notify(serviceUrls);
    routers = registryDirectory.getRouters();
    Assert.assertEquals(0 + 1, routers.size());
}
 
Example #18
Source File: ScriptRouterTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #19
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 1. notify twice, the second time notified router rules should completely replace the former one. 2. notify with
 * no router url, do nothing to current routers 3. notify with only one router url, with router=clean, clear all
 * current routers
 */
@Test
public void testNotifyRouterUrls() {
    if (isScriptUnsupported) return;
    RegistryDirectory registryDirectory = getRegistryDirectory();
    URL routerurl = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9096/");
    URL routerurl2 = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9097/");

    List<URL> serviceUrls = new ArrayList<URL>();
    // without ROUTER_KEY, the first router should not be created.
    serviceUrls.add(routerurl.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
                                                                                             "notsupported").addParameter(Constants.RULE_KEY,
                                                                                                                          "function test1(){}"));
    serviceUrls.add(routerurl2.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
                                                                                              ScriptRouterFactory.NAME).addParameter(Constants.RULE_KEY,
                                                                                                                                     "function test1(){}"));

    registryDirectory.notify(serviceUrls);
    List<Router> routers = registryDirectory.getRouters();
    //default invocation selector
    Assert.assertEquals(1+1, routers.size());
    Assert.assertEquals(ScriptRouter.class, routers.get(1).getClass());

    registryDirectory.notify(new ArrayList<URL>());
    routers = registryDirectory.getRouters();
    Assert.assertEquals(1 + 1, routers.size());
    Assert.assertEquals(ScriptRouter.class, routers.get(1).getClass());

    serviceUrls.clear();
    serviceUrls.add(routerurl.addParameter(Constants.ROUTER_KEY, Constants.ROUTER_TYPE_CLEAR));
    registryDirectory.notify(serviceUrls);
    routers = registryDirectory.getRouters();
    Assert.assertEquals(0 + 1, routers.size());
}
 
Example #20
Source File: ScriptRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: RegistryDirectoryTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
/**
 * 1. notify twice, the second time notified router rules should completely replace the former one. 2. notify with
 * no router url, do nothing to current routers 3. notify with only one router url, with router=clean, clear all
 * current routers
 */
@Test
public void testNotifyRouterUrls() {
    if (isScriptUnsupported) return;
    RegistryDirectory registryDirectory = getRegistryDirectory();
    URL routerurl = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9096/");
    URL routerurl2 = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9097/");

    List<URL> serviceUrls = new ArrayList<URL>();
    // without ROUTER_KEY, the first router should not be created.
    serviceUrls.add(routerurl.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
            "notsupported").addParameter(Constants.RULE_KEY,
            "function test1(){}"));
    serviceUrls.add(routerurl2.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
            ScriptRouterFactory.NAME).addParameter(Constants.RULE_KEY,
            "function test1(){}"));

    registryDirectory.notify(serviceUrls);
    List<Router> routers = registryDirectory.getRouters();
    //default invocation selector
    Assert.assertEquals(1 + 1, routers.size());
    Assert.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass());

    registryDirectory.notify(new ArrayList<URL>());
    routers = registryDirectory.getRouters();
    Assert.assertEquals(1 + 1, routers.size());
    Assert.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass());

    serviceUrls.clear();
    serviceUrls.add(routerurl.addParameter(Constants.ROUTER_KEY, Constants.ROUTER_TYPE_CLEAR));
    registryDirectory.notify(serviceUrls);
    routers = registryDirectory.getRouters();
    Assert.assertEquals(0 + 1, routers.size());
}