| /*
|
| * Copyright 1999-2011 Alibaba Group.
|
| *
|
| * Licensed under the Apache License, Version 2.0 (the "License");
|
| * you may not use this file except in compliance with the License.
|
| * You may obtain a copy of the License at
|
| *
|
| * http://www.apache.org/licenses/LICENSE-2.0
|
| *
|
| * Unless required by applicable law or agreed to in writing, software
|
| * distributed under the License is distributed on an "AS IS" BASIS,
|
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| * See the License for the specific language governing permissions and
|
| * limitations under the License.
|
| */
|
| package com.alibaba.dubbo.rpc.cluster.router.condition; |
| |
| |
| import java.util.ArrayList;
|
| import java.util.List;
|
|
|
| import junit.framework.Assert;
|
|
|
| import org.junit.Before;
|
| import org.junit.BeforeClass;
|
| import org.junit.Test;
|
|
|
| import com.alibaba.dubbo.common.Constants;
|
| import com.alibaba.dubbo.common.URL;
|
| import com.alibaba.dubbo.common.utils.NetUtils;
|
| import com.alibaba.dubbo.rpc.Invoker;
|
| import com.alibaba.dubbo.rpc.RpcInvocation;
|
| import com.alibaba.dubbo.rpc.cluster.Router;
|
| import com.alibaba.dubbo.rpc.cluster.router.MockInvoker;
|
| |
| public class ConditionRouterTest { |
| |
| @BeforeClass |
| public static void setUpBeforeClass() throws Exception { |
| } |
| |
| @Before |
| public void setUp() throws Exception { |
| } |
| |
| private URL SCRIPT_URL = URL.valueOf("condition://0.0.0.0/com.foo.BarService"); |
| |
| private URL getRouteUrl(String rule) { |
| return SCRIPT_URL.addParameterAndEncoded(Constants.RULE_KEY, rule); |
| } |
|
|
| @Test
|
| public void testRoute_ReturnFalse(){
|
| Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => false"));
|
| 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, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
|
| Assert.assertEquals(0, fileredInvokers.size());
|
| }
|
|
|
| @Test
|
| public void testRoute_ReturnEmpty(){
|
| Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => "));
|
| 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, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
|
| Assert.assertEquals(0, fileredInvokers.size());
|
| }
|
| |
| @Test |
| public void testRoute_ReturnAll(){ |
| Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = " + NetUtils.getLocalHost())); |
| 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, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); |
| Assert.assertEquals(invokers, fileredInvokers); |
| } |
| |
| @Test |
| public void testRoute_HostFilter(){ |
| Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = " + NetUtils.getLocalHost())); |
| List<Invoker<String>> invokers = new ArrayList<Invoker<String>>(); |
| Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")) ; |
| Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ; |
| Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ; |
| invokers.add(invoker1); |
| invokers.add(invoker2); |
| invokers.add(invoker3); |
| List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); |
| Assert.assertEquals(2, fileredInvokers.size()); |
| Assert.assertEquals(invoker2, fileredInvokers.get(0)); |
| Assert.assertEquals(invoker3, fileredInvokers.get(1)); |
| }
|
|
|
| @Test
|
| public void testRoute_Empty_HostFilter(){
|
| Router router = new ConditionRouterFactory().getRouter(getRouteUrl(" => " + " host = " + NetUtils.getLocalHost()));
|
| List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
|
| Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")) ;
|
| Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ;
|
| Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ;
|
| invokers.add(invoker1);
|
| invokers.add(invoker2);
|
| invokers.add(invoker3);
|
| List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
|
| Assert.assertEquals(2, fileredInvokers.size());
|
| Assert.assertEquals(invoker2, fileredInvokers.get(0));
|
| Assert.assertEquals(invoker3, fileredInvokers.get(1));
|
| }
|
|
|
| @Test
|
| public void testRoute_False_HostFilter(){
|
| Router router = new ConditionRouterFactory().getRouter(getRouteUrl("true => " + " host = " + NetUtils.getLocalHost()));
|
| List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
|
| Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")) ;
|
| Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ;
|
| Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ;
|
| invokers.add(invoker1);
|
| invokers.add(invoker2);
|
| invokers.add(invoker3);
|
| List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
|
| Assert.assertEquals(2, fileredInvokers.size());
|
| Assert.assertEquals(invoker2, fileredInvokers.get(0));
|
| Assert.assertEquals(invoker3, fileredInvokers.get(1));
|
| }
|
|
|
| @Test
|
| public void testRoute_Placeholder(){
|
| Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = $host"));
|
| List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
|
| Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")) ;
|
| Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ;
|
| Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ;
|
| invokers.add(invoker1);
|
| invokers.add(invoker2);
|
| invokers.add(invoker3);
|
| List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
|
| Assert.assertEquals(2, fileredInvokers.size());
|
| Assert.assertEquals(invoker2, fileredInvokers.get(0));
|
| Assert.assertEquals(invoker3, fileredInvokers.get(1));
|
| }
|
|
|
| @Test
|
| public void testRoute_NoForce(){
|
| Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = 1.2.3.4"));
|
| List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
|
| Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")) ;
|
| Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ;
|
| Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ;
|
| invokers.add(invoker1);
|
| invokers.add(invoker2);
|
| invokers.add(invoker3);
|
| List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
|
| Assert.assertEquals(invokers, fileredInvokers);
|
| }
|
|
|
| @Test
|
| public void testRoute_Force(){
|
| Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = 1.2.3.4").addParameter(Constants.FORCE_KEY, String.valueOf(true)));
|
| List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
|
| Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService")) ;
|
| Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ;
|
| Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService")) ;
|
| invokers.add(invoker1);
|
| invokers.add(invoker2);
|
| invokers.add(invoker3);
|
| List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
|
| Assert.assertEquals(0, fileredInvokers.size());
|
| }
|
| |
| } |