Search in sources :

Example 66 with Result

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

the class MockClusterInvokerTest method testMockInvokerFromOverride_Invoke_Fock_WithForceDefault.

/**
 * Test if mock policy works fine: fail-mock
 */
@Test
public void testMockInvokerFromOverride_Invoke_Fock_WithForceDefault() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter("mock", "force:return z").addParameter("getSomething.mock", "fail:return x").addParameter("getSomething2.mock", "force:return y").addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())).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);
    Assertions.assertEquals("x", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("getSomething2");
    ret = cluster.invoke(invocation);
    Assertions.assertEquals("y", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("getSomething3");
    ret = cluster.invoke(invocation);
    Assertions.assertEquals("z", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("sayHello");
    ret = cluster.invoke(invocation);
    Assertions.assertEquals("z", ret.getValue());
}
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 67 with Result

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

the class MockClusterInvokerTest method testMockInvokerFromOverride_Invoke_check_ListString_empty.

@SuppressWarnings("unchecked")
@Test
public void testMockInvokerFromOverride_Invoke_check_ListString_empty() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter("getListString.mock", "force:return empty").addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())).addParameter("invoke_return_error", "true");
    Invoker<IHelloService> cluster = getClusterInvoker(url);
    // Configured with mock
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("getListString");
    Result ret = cluster.invoke(invocation);
    Assertions.assertEquals(0, ((List<String>) ret.getValue()).size());
}
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 68 with Result

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

the class MockClusterInvokerTest method testMockInvokerFromOverride_Invoke_Fock_Default.

/**
 * Test if mock policy works fine: fail-mock
 */
@Test
public void testMockInvokerFromOverride_Invoke_Fock_Default() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter("mock", "fail:return x").addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())).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);
    Assertions.assertEquals("x", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("getSomething2");
    ret = cluster.invoke(invocation);
    Assertions.assertEquals("x", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("sayHello");
    ret = cluster.invoke(invocation);
    Assertions.assertEquals("x", ret.getValue());
}
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 69 with Result

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

the class MockClusterInvokerTest method testMockInvokerFromOverride_Invoke_check_int.

@Test
public void testMockInvokerFromOverride_Invoke_check_int() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())).addParameter("getInt1.mock", "force:return 1688").addParameter("invoke_return_error", "true");
    Invoker<IHelloService> cluster = getClusterInvoker(url);
    // Configured with mock
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("getInt1");
    Result ret = cluster.invoke(invocation);
    Assertions.assertTrue(ret.getValue() instanceof Integer, "result type must be integer but was : " + ret.getValue().getClass());
    Assertions.assertEquals(new Integer(1688), (Integer) ret.getValue());
}
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 70 with Result

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

the class MockClusterInvokerTest method testMockInvokerFromOverride_Invoke_Fock_WithOutDefault.

/**
 * Test if mock policy works fine: fail-mock
 */
@Test
public void testMockInvokerFromOverride_Invoke_Fock_WithOutDefault() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter("getSomething.mock", "fail:return x").addParameter("getSomething2.mock", "force:return y").addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())).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);
    Assertions.assertEquals("x", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("getSomething2");
    ret = cluster.invoke(invocation);
    Assertions.assertEquals("y", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("getSomething3");
    try {
        ret = cluster.invoke(invocation);
        Assertions.fail();
    } catch (RpcException e) {
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) RpcException(org.apache.dubbo.rpc.RpcException) URL(org.apache.dubbo.common.URL) 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