Search in sources :

Example 41 with RpcException

use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.

the class MockClusterInvoker method doMockInvoke.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Result doMockInvoke(Invocation invocation, RpcException e) {
    Result result = null;
    Invoker<T> minvoker;
    List<Invoker<T>> mockInvokers = selectMockInvoker(invocation);
    if (mockInvokers == null || mockInvokers.isEmpty()) {
        minvoker = (Invoker<T>) new MockInvoker(directory.getUrl());
    } else {
        minvoker = mockInvokers.get(0);
    }
    try {
        result = minvoker.invoke(invocation);
    } catch (RpcException me) {
        if (me.isBiz()) {
            result = new RpcResult(me.getCause());
        } else {
            throw new RpcException(me.getCode(), getMockExceptionMessage(e, me), me.getCause());
        }
    } catch (Throwable me) {
        throw new RpcException(getMockExceptionMessage(e, me), me.getCause());
    }
    return result;
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) MockInvoker(com.alibaba.dubbo.rpc.support.MockInvoker) MockInvoker(com.alibaba.dubbo.rpc.support.MockInvoker) RpcException(com.alibaba.dubbo.rpc.RpcException) RpcResult(com.alibaba.dubbo.rpc.RpcResult) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult)

Example 42 with RpcException

use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.

the class FailoverClusterInvokerTest method testInvokerDestoryAndReList.

/**
 * When invokers in directory changes after a failed request but just before a retry effort,
 * then we should reselect from the latest invokers before retry.
 */
@Test
public void testInvokerDestoryAndReList() {
    final URL url = URL.valueOf("test://localhost/" + Demo.class.getName() + "?loadbalance=roundrobin&retries=" + retries);
    RpcException exception = new RpcException(RpcException.TIMEOUT_EXCEPTION);
    MockInvoker<Demo> invoker1 = new MockInvoker<Demo>(Demo.class, url);
    invoker1.setException(exception);
    MockInvoker<Demo> invoker2 = new MockInvoker<Demo>(Demo.class, url);
    invoker2.setException(exception);
    final List<Invoker<Demo>> invokers = new ArrayList<Invoker<Demo>>();
    invokers.add(invoker1);
    invokers.add(invoker2);
    Callable<Object> callable = new Callable<Object>() {

        public Object call() throws Exception {
            // Simulation: all invokers are destroyed
            for (Invoker<Demo> invoker : invokers) {
                invoker.destroy();
            }
            invokers.clear();
            MockInvoker<Demo> invoker3 = new MockInvoker<Demo>(Demo.class, url);
            invokers.add(invoker3);
            return null;
        }
    };
    invoker1.setCallable(callable);
    invoker2.setCallable(callable);
    RpcInvocation inv = new RpcInvocation();
    inv.setMethodName("test");
    Directory<Demo> dic = new MockDirectory<Demo>(url, invokers);
    FailoverClusterInvoker<Demo> clusterinvoker = new FailoverClusterInvoker<Demo>(dic);
    clusterinvoker.invoke(inv);
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) ArrayList(java.util.ArrayList) URL(com.alibaba.dubbo.common.URL) Callable(java.util.concurrent.Callable) Invoker(com.alibaba.dubbo.rpc.Invoker) AbstractInvoker(com.alibaba.dubbo.rpc.protocol.AbstractInvoker) RpcException(com.alibaba.dubbo.rpc.RpcException) Test(org.junit.Test)

Example 43 with RpcException

use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.

the class FailoverClusterInvokerTest method testInvoke_retryTimes.

@Test()
public void testInvoke_retryTimes() {
    EasyMock.reset(invoker1);
    EasyMock.expect(invoker1.invoke(invocation)).andThrow(new RpcException(RpcException.TIMEOUT_EXCEPTION)).anyTimes();
    EasyMock.expect(invoker1.isAvailable()).andReturn(false).anyTimes();
    EasyMock.expect(invoker1.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(invoker1.getInterface()).andReturn(FailoverClusterInvokerTest.class).anyTimes();
    EasyMock.replay(invoker1);
    EasyMock.reset(invoker2);
    EasyMock.expect(invoker2.invoke(invocation)).andThrow(new RpcException()).anyTimes();
    EasyMock.expect(invoker2.isAvailable()).andReturn(false).anyTimes();
    EasyMock.expect(invoker2.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(invoker2.getInterface()).andReturn(FailoverClusterInvokerTest.class).anyTimes();
    EasyMock.replay(invoker2);
    FailoverClusterInvoker<FailoverClusterInvokerTest> invoker = new FailoverClusterInvoker<FailoverClusterInvokerTest>(dic);
    try {
        Result ret = invoker.invoke(invocation);
        assertSame(result, ret);
        fail();
    } catch (RpcException expected) {
        assertTrue(expected.isTimeout());
        assertTrue(expected.getMessage().indexOf((retries + 1) + " times") > 0);
    }
}
Also used : RpcException(com.alibaba.dubbo.rpc.RpcException) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult) Test(org.junit.Test)

Example 44 with RpcException

use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.

the class FailoverClusterInvokerTest method testInvokeWithRPCException.

@Test()
public void testInvokeWithRPCException() {
    EasyMock.reset(invoker1);
    EasyMock.expect(invoker1.invoke(invocation)).andThrow(new RpcException()).anyTimes();
    EasyMock.expect(invoker1.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker1.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(invoker1.getInterface()).andReturn(FailoverClusterInvokerTest.class).anyTimes();
    EasyMock.replay(invoker1);
    EasyMock.reset(invoker2);
    EasyMock.expect(invoker2.invoke(invocation)).andReturn(result).anyTimes();
    EasyMock.expect(invoker2.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker2.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(invoker2.getInterface()).andReturn(FailoverClusterInvokerTest.class).anyTimes();
    EasyMock.replay(invoker2);
    FailoverClusterInvoker<FailoverClusterInvokerTest> invoker = new FailoverClusterInvoker<FailoverClusterInvokerTest>(dic);
    for (int i = 0; i < 100; i++) {
        Result ret = invoker.invoke(invocation);
        assertSame(result, ret);
    }
}
Also used : RpcException(com.alibaba.dubbo.rpc.RpcException) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult) Test(org.junit.Test)

Example 45 with RpcException

use of com.alibaba.dubbo.rpc.RpcException in project dubbo by alibaba.

the class MockClusterInvokerTest method testMockInvokerFromOverride_Invoke_checkCompatible_return.

/**
 * Test if mock policy works fine: fail-mock
 */
@Test
public void testMockInvokerFromOverride_Invoke_checkCompatible_return() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter("getSomething.mock", "return x").addParameter("invoke_return_error", "true");
    Invoker<IHelloService> cluster = getClusterInvoker(url);
    // Configured with mock
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("getSomething");
    Result ret = cluster.invoke(invocation);
    Assert.assertEquals("x", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("getSomething3");
    try {
        ret = cluster.invoke(invocation);
        Assert.fail("fail invoke");
    } catch (RpcException e) {
    }
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) RpcException(com.alibaba.dubbo.rpc.RpcException) URL(com.alibaba.dubbo.common.URL) Result(com.alibaba.dubbo.rpc.Result) Test(org.junit.Test)

Aggregations

RpcException (com.alibaba.dubbo.rpc.RpcException)72 Test (org.junit.Test)25 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)23 Result (com.alibaba.dubbo.rpc.Result)22 URL (com.alibaba.dubbo.common.URL)20 RpcResult (com.alibaba.dubbo.rpc.RpcResult)17 ArrayList (java.util.ArrayList)11 Invoker (com.alibaba.dubbo.rpc.Invoker)10 Method (java.lang.reflect.Method)10 Invocation (com.alibaba.dubbo.rpc.Invocation)8 RemotingException (com.alibaba.dubbo.remoting.RemotingException)7 IOException (java.io.IOException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 ExchangeClient (com.alibaba.dubbo.remoting.exchange.ExchangeClient)3 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)3 TApplicationException (org.apache.thrift.TApplicationException)3 TMessage (org.apache.thrift.protocol.TMessage)3