Search in sources :

Example 36 with Result

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

the class MockClusterInvokerTest method testMockInvokerInvoke_forcemock_defaultreturn.

@Test
public void testMockInvokerInvoke_forcemock_defaultreturn() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName());
    url = url.addParameter(MOCK_KEY, "force").addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName()));
    Invoker<IHelloService> cluster = getClusterInvoker(url);
    URL mockUrl = URL.valueOf("mock://localhost/" + IHelloService.class.getName() + "?getSomething.mock=return aa&getSomething3xx.mock=return xx&sayHello.mock=return ").addParameters(url.getParameters());
    Protocol protocol = new MockProtocol();
    Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl);
    invokers.add(mInvoker1);
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("sayHello");
    Result ret = cluster.invoke(invocation);
    Assertions.assertNull(ret.getValue());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Protocol(org.apache.dubbo.rpc.Protocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 37 with Result

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

the class MockClusterInvokerTest method testMockInvokerFromOverride_Invoke_check_Boolean.

@Test
public void testMockInvokerFromOverride_Invoke_check_Boolean() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())).addParameter("getBoolean2.mock", "force:return true").addParameter("invoke_return_error", "true");
    Invoker<IHelloService> cluster = getClusterInvoker(url);
    // Configured with mock
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("getBoolean2");
    Result ret = cluster.invoke(invocation);
    Assertions.assertTrue(Boolean.parseBoolean(ret.getValue().toString()));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 38 with Result

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

the class AvailableClusterInvokerTest method testInvokeNoException.

@Test()
public void testInvokeNoException() {
    resetInvokerToNoException();
    AvailableClusterInvoker<AvailableClusterInvokerTest> invoker = new AvailableClusterInvoker<>(dic);
    Result ret = invoker.invoke(invocation);
    Assertions.assertSame(result, ret);
}
Also used : Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 39 with Result

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

the class FailoverClusterInvokerTest method testInvoke_retryTimes2.

@Test()
public void testInvoke_retryTimes2() {
    int finalRetries = 1;
    given(invoker1.invoke(invocation)).willThrow(new RpcException(RpcException.TIMEOUT_EXCEPTION));
    given(invoker1.isAvailable()).willReturn(false);
    given(invoker1.getUrl()).willReturn(url);
    given(invoker1.getInterface()).willReturn(FailoverClusterInvokerTest.class);
    given(invoker2.invoke(invocation)).willThrow(new RpcException());
    given(invoker2.isAvailable()).willReturn(false);
    given(invoker2.getUrl()).willReturn(url);
    given(invoker2.getInterface()).willReturn(FailoverClusterInvokerTest.class);
    RpcContext rpcContext = RpcContext.getContext();
    rpcContext.setAttachment("retries", finalRetries);
    FailoverClusterInvoker<FailoverClusterInvokerTest> invoker = new FailoverClusterInvoker<FailoverClusterInvokerTest>(dic);
    try {
        Result ret = invoker.invoke(invocation);
        assertSame(result, ret);
        fail();
    } catch (RpcException expected) {
        assertTrue((expected.isTimeout() || expected.getCode() == 0));
        assertTrue(expected.getMessage().indexOf((finalRetries + 1) + " times") > 0);
    }
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) RpcException(org.apache.dubbo.rpc.RpcException) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 40 with Result

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

the class FailoverClusterInvokerTest method testInvoke_retryTimes.

@Test()
public void testInvoke_retryTimes() {
    given(invoker1.invoke(invocation)).willThrow(new RpcException(RpcException.TIMEOUT_EXCEPTION));
    given(invoker1.isAvailable()).willReturn(false);
    given(invoker1.getUrl()).willReturn(url);
    given(invoker1.getInterface()).willReturn(FailoverClusterInvokerTest.class);
    given(invoker2.invoke(invocation)).willThrow(new RpcException());
    given(invoker2.isAvailable()).willReturn(false);
    given(invoker2.getUrl()).willReturn(url);
    given(invoker2.getInterface()).willReturn(FailoverClusterInvokerTest.class);
    FailoverClusterInvoker<FailoverClusterInvokerTest> invoker = new FailoverClusterInvoker<FailoverClusterInvokerTest>(dic);
    try {
        Result ret = invoker.invoke(invocation);
        assertSame(result, ret);
        fail();
    } catch (RpcException expected) {
        assertTrue((expected.isTimeout() || expected.getCode() == 0));
        assertTrue(expected.getMessage().indexOf((retries + 1) + " times") > 0);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Aggregations

Result (org.apache.dubbo.rpc.Result)97 Test (org.junit.jupiter.api.Test)74 URL (org.apache.dubbo.common.URL)55 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)39 Invocation (org.apache.dubbo.rpc.Invocation)33 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)31 AppResponse (org.apache.dubbo.rpc.AppResponse)28 Invoker (org.apache.dubbo.rpc.Invoker)27 RpcException (org.apache.dubbo.rpc.RpcException)22 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)7 RpcContext (org.apache.dubbo.rpc.RpcContext)7 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)6 DemoService (org.apache.dubbo.rpc.support.DemoService)6 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Protocol (org.apache.dubbo.rpc.Protocol)5 Person (org.apache.dubbo.rpc.support.Person)5 Method (java.lang.reflect.Method)4 MockProtocol (org.apache.dubbo.rpc.support.MockProtocol)4