Search in sources :

Example 56 with Invocation

use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testTimeoutExceptionCode.

@Test()
public void testTimeoutExceptionCode() {
    List<Invoker<DemoService>> invokers = new ArrayList<Invoker<DemoService>>();
    invokers.add(new Invoker<DemoService>() {

        @Override
        public Class<DemoService> getInterface() {
            return DemoService.class;
        }

        public URL getUrl() {
            return URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/" + DemoService.class.getName());
        }

        @Override
        public boolean isAvailable() {
            return false;
        }

        @Override
        public Result invoke(Invocation invocation) throws RpcException {
            throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test timeout");
        }

        @Override
        public void destroy() {
        }
    });
    Directory<DemoService> directory = new StaticDirectory<DemoService>(invokers);
    FailoverClusterInvoker<DemoService> failoverClusterInvoker = new FailoverClusterInvoker<DemoService>(directory);
    try {
        failoverClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[0], new Object[0]));
        Assertions.fail();
    } catch (RpcException e) {
        Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
    ForkingClusterInvoker<DemoService> forkingClusterInvoker = new ForkingClusterInvoker<DemoService>(directory);
    try {
        forkingClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[0], new Object[0]));
        Assertions.fail();
    } catch (RpcException e) {
        Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
    FailfastClusterInvoker<DemoService> failfastClusterInvoker = new FailfastClusterInvoker<DemoService>(directory);
    try {
        failfastClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[0], new Object[0]));
        Assertions.fail();
    } catch (RpcException e) {
        Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ArrayList(java.util.ArrayList) DemoService(org.apache.dubbo.rpc.cluster.filter.DemoService) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) StaticDirectory(org.apache.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(org.apache.dubbo.rpc.Invoker) RpcException(org.apache.dubbo.rpc.RpcException) Test(org.junit.jupiter.api.Test)

Example 57 with Invocation

use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.

the class AbstractClusterInvokerTest method setUp.

@SuppressWarnings({ "unchecked" })
@BeforeEach
public void setUp() throws Exception {
    invocation.setMethodName("sayHello");
    invoker1 = mock(Invoker.class);
    invoker2 = mock(Invoker.class);
    invoker3 = mock(Invoker.class);
    invoker4 = mock(Invoker.class);
    invoker5 = mock(Invoker.class);
    mockedInvoker1 = mock(Invoker.class);
    URL turl = URL.valueOf("test://test:11/test");
    given(invoker1.isAvailable()).willReturn(false);
    given(invoker1.getInterface()).willReturn(IHelloService.class);
    given(invoker1.getUrl()).willReturn(turl.setPort(1).addParameter("name", "invoker1"));
    given(invoker2.isAvailable()).willReturn(true);
    given(invoker2.getInterface()).willReturn(IHelloService.class);
    given(invoker2.getUrl()).willReturn(turl.setPort(2).addParameter("name", "invoker2"));
    given(invoker3.isAvailable()).willReturn(false);
    given(invoker3.getInterface()).willReturn(IHelloService.class);
    given(invoker3.getUrl()).willReturn(turl.setPort(3).addParameter("name", "invoker3"));
    given(invoker4.isAvailable()).willReturn(true);
    given(invoker4.getInterface()).willReturn(IHelloService.class);
    given(invoker4.getUrl()).willReturn(turl.setPort(4).addParameter("name", "invoker4"));
    given(invoker5.isAvailable()).willReturn(false);
    given(invoker5.getInterface()).willReturn(IHelloService.class);
    given(invoker5.getUrl()).willReturn(turl.setPort(5).addParameter("name", "invoker5"));
    given(mockedInvoker1.isAvailable()).willReturn(false);
    given(mockedInvoker1.getInterface()).willReturn(IHelloService.class);
    given(mockedInvoker1.getUrl()).willReturn(turl.setPort(999).setProtocol("mock"));
    invokers.add(invoker1);
    dic = new StaticDirectory<>(url, invokers, null);
    dic.setConsumerUrl(consumerUrl);
    cluster = new AbstractClusterInvoker(dic) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            return null;
        }
    };
    cluster_nocheck = new AbstractClusterInvoker(dic, url.addParameterIfAbsent(CLUSTER_AVAILABLE_CHECK_KEY, Boolean.FALSE.toString())) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            return null;
        }
    };
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RoundRobinLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) RpcException(org.apache.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 58 with Invocation

use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.

the class ConditionRouterTest method testRoute_methodRoute.

@Test
public void testRoute_methodRoute() {
    Invocation invocation = new RpcInvocation("getFoo", "com.foo.BarService", "", new Class<?>[0], new Object[0]);
    // More than one methods, mismatch
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl("methods=getFoo => host = 1.2.3.4"));
    boolean matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=setFoo,getFoo,findFoo"), invocation);
    Assertions.assertTrue(matchWhen);
    // Exactly one method, match
    matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=getFoo"), invocation);
    Assertions.assertTrue(matchWhen);
    // Method routing and Other condition routing can work together
    Router router2 = new ConditionRouterFactory().getRouter(getRouteUrl("methods=getFoo & host!=1.1.1.1 => host = 1.2.3.4"));
    matchWhen = ((ConditionRouter) router2).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=getFoo"), invocation);
    Assertions.assertFalse(matchWhen);
    Router router3 = new ConditionRouterFactory().getRouter(getRouteUrl("methods=getFoo & host=1.1.1.1 => host = 1.2.3.4"));
    matchWhen = ((ConditionRouter) router3).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=getFoo"), invocation);
    Assertions.assertTrue(matchWhen);
    // Test filter condition
    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://" + LOCAL_HOST + ":20880/com.foo.BarService"));
    Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + LOCAL_HOST + ":20880/com.foo.BarService"));
    invokers.add(invoker1);
    invokers.add(invoker2);
    invokers.add(invoker3);
    Router router4 = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + LOCAL_HOST + " & methods = getFoo => " + " host = 10.20.3.3").addParameter(FORCE_KEY, String.valueOf(true)));
    List<Invoker<String>> filteredInvokers1 = router4.route(invokers, URL.valueOf("consumer://" + LOCAL_HOST + "/com.foo.BarService"), invocation);
    Assertions.assertEquals(1, filteredInvokers1.size());
    Router router5 = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + LOCAL_HOST + " & methods = unvalidmethod => " + " host = 10.20.3.3").addParameter(FORCE_KEY, String.valueOf(true)));
    List<Invoker<String>> filteredInvokers2 = router5.route(invokers, URL.valueOf("consumer://" + LOCAL_HOST + "/com.foo.BarService"), invocation);
    Assertions.assertEquals(3, filteredInvokers2.size());
// Request a non-exists method
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(org.apache.dubbo.rpc.cluster.Router) Test(org.junit.jupiter.api.Test)

Example 59 with Invocation

use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.

the class LoadBalanceBaseTest method setUp.

/**
 * @throws java.lang.Exception
 */
@BeforeEach
public void setUp() throws Exception {
    invocation = mock(Invocation.class);
    given(invocation.getMethodName()).willReturn("method1");
    given(invocation.getArguments()).willReturn(new Object[] { "arg1", "arg2", "arg3" });
    invoker1 = mock(Invoker.class);
    invoker2 = mock(Invoker.class);
    invoker3 = mock(Invoker.class);
    invoker4 = mock(Invoker.class);
    invoker5 = mock(Invoker.class);
    URL url1 = URL.valueOf("test://127.0.0.1:1/DemoService");
    URL url2 = URL.valueOf("test://127.0.0.1:2/DemoService");
    URL url3 = URL.valueOf("test://127.0.0.1:3/DemoService");
    URL url4 = URL.valueOf("test://127.0.0.1:4/DemoService");
    URL url5 = URL.valueOf("test://127.0.0.1:5/DemoService");
    given(invoker1.isAvailable()).willReturn(true);
    given(invoker1.getInterface()).willReturn(LoadBalanceBaseTest.class);
    given(invoker1.getUrl()).willReturn(url1);
    given(invoker2.isAvailable()).willReturn(true);
    given(invoker2.getInterface()).willReturn(LoadBalanceBaseTest.class);
    given(invoker2.getUrl()).willReturn(url2);
    given(invoker3.isAvailable()).willReturn(true);
    given(invoker3.getInterface()).willReturn(LoadBalanceBaseTest.class);
    given(invoker3.getUrl()).willReturn(url3);
    given(invoker4.isAvailable()).willReturn(true);
    given(invoker4.getInterface()).willReturn(LoadBalanceBaseTest.class);
    given(invoker4.getUrl()).willReturn(url4);
    given(invoker5.isAvailable()).willReturn(true);
    given(invoker5.getInterface()).willReturn(LoadBalanceBaseTest.class);
    given(invoker5.getUrl()).willReturn(url5);
    invokers.add(invoker1);
    invokers.add(invoker2);
    invokers.add(invoker3);
    invokers.add(invoker4);
    invokers.add(invoker5);
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) URL(org.apache.dubbo.common.URL) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 60 with Invocation

use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.

the class MockClusterInvokerTest method getClusterInvokerMock.

private Invoker<IHelloService> getClusterInvokerMock(URL url, Invoker<IHelloService> mockInvoker) {
    // As `javassist` have a strict restriction of argument types, request will fail if Invocation do not contains complete parameter type information
    final URL durl = url.addParameter("proxy", "jdk");
    invokers.clear();
    ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension("jdk");
    Invoker<IHelloService> invoker1 = proxy.getInvoker(new HelloService(), IHelloService.class, durl);
    invokers.add(invoker1);
    if (mockInvoker != null) {
        invokers.add(mockInvoker);
    }
    StaticDirectory<IHelloService> dic = new StaticDirectory<IHelloService>(durl, invokers, null);
    dic.buildRouterChain();
    AbstractClusterInvoker<IHelloService> cluster = new AbstractClusterInvoker(dic) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            if (durl.getParameter("invoke_return_error", false)) {
                throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test rpc exception");
            } else {
                return ((Invoker<?>) invokers.get(0)).invoke(invocation);
            }
        }
    };
    return new MockClusterInvoker<IHelloService>(dic, cluster);
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) URL(org.apache.dubbo.common.URL) StaticDirectory(org.apache.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(org.apache.dubbo.rpc.Invoker) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RpcException(org.apache.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Invocation (org.apache.dubbo.rpc.Invocation)98 Test (org.junit.jupiter.api.Test)78 URL (org.apache.dubbo.common.URL)77 Invoker (org.apache.dubbo.rpc.Invoker)44 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)44 Result (org.apache.dubbo.rpc.Result)33 AppResponse (org.apache.dubbo.rpc.AppResponse)29 RpcException (org.apache.dubbo.rpc.RpcException)18 MockInvocation (org.apache.dubbo.rpc.support.MockInvocation)17 HashMap (java.util.HashMap)16 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)13 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)11 MyInvoker (org.apache.dubbo.rpc.support.MyInvoker)10 ArrayList (java.util.ArrayList)8 Person (org.apache.dubbo.rpc.support.Person)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 IMetricManager (com.alibaba.metrics.IMetricManager)5 Method (java.lang.reflect.Method)5 DemoService (org.apache.dubbo.monitor.dubbo.service.DemoService)5 FastCompass (com.alibaba.metrics.FastCompass)4