You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by il...@apache.org on 2018/12/04 07:31:07 UTC

[incubator-dubbo] branch master updated: add some ScriptRoute test case (#2879)

This is an automated email from the ASF dual-hosted git repository.

iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 1f751a6  add some ScriptRoute test case (#2879)
1f751a6 is described below

commit 1f751a602654f52d1ea86488a66d56c2a8faced6
Author: XiaoJie Li <li...@outlook.com>
AuthorDate: Tue Dec 4 15:30:57 2018 +0800

    add some ScriptRoute test case (#2879)
    
    * add router test case
    
    * correct an error word
---
 .../cluster/router/script/ScriptRouterTest.java    | 53 +++++++++++++++++++---
 1 file changed, 47 insertions(+), 6 deletions(-)

diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouterTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouterTest.java
index 8a01a99..3d94b16 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouterTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouterTest.java
@@ -17,22 +17,20 @@
 package org.apache.dubbo.rpc.cluster.router.script;
 
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.dubbo.common.Constants;
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.rpc.Invoker;
 import org.apache.dubbo.rpc.RpcInvocation;
 import org.apache.dubbo.rpc.cluster.Router;
 import org.apache.dubbo.rpc.cluster.router.MockInvoker;
-
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-import javax.script.ScriptException;
-import java.util.ArrayList;
-import java.util.List;
-
 public class ScriptRouterTest {
 
     private URL SCRIPT_URL = URL.valueOf("script://javascript?type=javascript");
@@ -85,5 +83,48 @@ public class ScriptRouterTest {
         Assert.assertEquals(invoker3, filteredInvokers.get(1));
     }
 
-    //TODO Add tests for abnormal scene
+    @Test
+    public void testRouteHostFilter() {
+    	List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
+    	MockInvoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.1:20880/com.dubbo.HelloService"));
+    	MockInvoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.2:20880/com.dubbo.HelloService"));
+    	MockInvoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.3:20880/com.dubbo.HelloService"));
+        invokers.add(invoker1);
+        invokers.add(invoker2);
+        invokers.add(invoker3);
+        
+        String script = "function route(invokers, invocation, context){ " + 
+        		"	var result = new java.util.ArrayList(invokers.size()); " + 
+        		"	var targetHost = new java.util.ArrayList(); " + 
+        		"	targetHost.add(\"10.134.108.2\"); " + 
+        		"	for (var i = 0; i < invokers.length; i++) { " + 
+        		"		if(targetHost.contains(invokers[i].getUrl().getHost())){ " + 
+        		"			result.add(invokers[i]); " + 
+        		"		} " + 
+        		"	} " + 
+        		"	return result; " + 
+        		"} " + 
+        		"route(invokers, invocation, context) ";
+        
+        Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script));
+        List<Invoker<String>> routeResult = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation());
+        Assert.assertEquals(1, routeResult.size());
+        Assert.assertEquals(invoker2,routeResult.get(0));
+    }
+    
+    @Test
+    public void testRoute_throwException() {
+    	List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
+    	MockInvoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.1:20880/com.dubbo.HelloService"));
+    	MockInvoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.2:20880/com.dubbo.HelloService"));
+    	MockInvoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.3:20880/com.dubbo.HelloService"));
+        invokers.add(invoker1);
+        invokers.add(invoker2);
+        invokers.add(invoker3);
+        
+        String script = "/";
+        Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script));
+        List<Invoker<String>> routeResult = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation());
+        Assert.assertEquals(3, routeResult.size());
+    }
 }
\ No newline at end of file