Search in sources :

Example 11 with Invoker

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

the class RpcUtilsTest method testGetReturnType.

@Test
public void testGetReturnType() {
    Class<?> demoServiceClass = DemoService.class;
    String serviceName = demoServiceClass.getName();
    Invoker invoker = mock(Invoker.class);
    given(invoker.getUrl()).willReturn(URL.valueOf("test://127.0.0.1:1/org.apache.dubbo.rpc.support.DemoService?interface=org.apache.dubbo.rpc.support.DemoService"));
    // void sayHello(String name);
    RpcInvocation inv = new RpcInvocation("sayHello", serviceName, "", new Class<?>[] { String.class }, null, null, invoker, null);
    Class<?> returnType = RpcUtils.getReturnType(inv);
    Assertions.assertNull(returnType);
    // String echo(String text);
    RpcInvocation inv1 = new RpcInvocation("echo", serviceName, "", new Class<?>[] { String.class }, null, null, invoker, null);
    Class<?> returnType1 = RpcUtils.getReturnType(inv1);
    Assertions.assertNotNull(returnType1);
    Assertions.assertEquals(String.class, returnType1);
    // int getSize(String[] strs);
    RpcInvocation inv2 = new RpcInvocation("getSize", serviceName, "", new Class<?>[] { String[].class }, null, null, invoker, null);
    Class<?> returnType2 = RpcUtils.getReturnType(inv2);
    Assertions.assertNotNull(returnType2);
    Assertions.assertEquals(int.class, returnType2);
    // Person getPerson(Person person);
    RpcInvocation inv3 = new RpcInvocation("getPerson", serviceName, "", new Class<?>[] { Person.class }, null, null, invoker, null);
    Class<?> returnType3 = RpcUtils.getReturnType(inv3);
    Assertions.assertNotNull(returnType3);
    Assertions.assertEquals(Person.class, returnType3);
    // List<String> testReturnType1(String str);
    RpcInvocation inv4 = new RpcInvocation("testReturnType1", serviceName, "", new Class<?>[] { String.class }, null, null, invoker, null);
    Class<?> returnType4 = RpcUtils.getReturnType(inv4);
    Assertions.assertNotNull(returnType4);
    Assertions.assertEquals(List.class, returnType4);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with Invoker

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

the class RpcUtilsTest method testGetParameterTypes.

@Test
public void testGetParameterTypes() {
    Class<?> demoServiceClass = DemoService.class;
    String serviceName = demoServiceClass.getName();
    Invoker invoker = mock(Invoker.class);
    // void sayHello(String name);
    RpcInvocation inv1 = new RpcInvocation("sayHello", serviceName, "", new Class<?>[] { String.class }, null, null, invoker, null);
    Class<?>[] parameterTypes1 = RpcUtils.getParameterTypes(inv1);
    Assertions.assertNotNull(parameterTypes1);
    Assertions.assertEquals(1, parameterTypes1.length);
    Assertions.assertEquals(String.class, parameterTypes1[0]);
    // long timestamp();
    RpcInvocation inv2 = new RpcInvocation("timestamp", serviceName, "", null, null, null, invoker, null);
    Class<?>[] parameterTypes2 = RpcUtils.getParameterTypes(inv2);
    Assertions.assertEquals(0, parameterTypes2.length);
    // Type enumlength(Type... types);
    RpcInvocation inv3 = new RpcInvocation("enumlength", serviceName, "", new Class<?>[] { Type.class, Type.class }, null, null, invoker, null);
    Class<?>[] parameterTypes3 = RpcUtils.getParameterTypes(inv3);
    Assertions.assertNotNull(parameterTypes3);
    Assertions.assertEquals(2, parameterTypes3.length);
    Assertions.assertEquals(Type.class, parameterTypes3[0]);
    Assertions.assertEquals(Type.class, parameterTypes3[1]);
    // byte getbyte(byte arg);
    RpcInvocation inv4 = new RpcInvocation("getbyte", serviceName, "", new Class<?>[] { byte.class }, null, null, invoker, null);
    Class<?>[] parameterTypes4 = RpcUtils.getParameterTypes(inv4);
    Assertions.assertNotNull(parameterTypes4);
    Assertions.assertEquals(1, parameterTypes4.length);
    Assertions.assertEquals(byte.class, parameterTypes4[0]);
    // void $invoke(String s1, String s2);
    RpcInvocation inv5 = new RpcInvocation("$invoke", serviceName, "", new Class<?>[] { String.class, String[].class }, new Object[] { "method", new String[] { "java.lang.String", "void", "java.lang.Object" } }, null, invoker, null);
    Class<?>[] parameterTypes5 = RpcUtils.getParameterTypes(inv5);
    Assertions.assertNotNull(parameterTypes5);
    Assertions.assertEquals(3, parameterTypes5.length);
    Assertions.assertEquals(String.class, parameterTypes5[0]);
    Assertions.assertEquals(String.class, parameterTypes5[1]);
    Assertions.assertEquals(String.class, parameterTypes5[2]);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with Invoker

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

the class RpcUtilsTest method testGet_$invoke_Arguments.

@Test
public void testGet_$invoke_Arguments() {
    Object[] args = new Object[] { "hello", "dubbo", 520 };
    Class<?> demoServiceClass = DemoService.class;
    String serviceName = demoServiceClass.getName();
    Invoker invoker = mock(Invoker.class);
    RpcInvocation inv = new RpcInvocation("$invoke", serviceName, "", new Class<?>[] { String.class, String[].class, Object[].class }, new Object[] { "method", new String[] {}, args }, null, invoker, null);
    Object[] arguments = RpcUtils.getArguments(inv);
    for (int i = 0; i < args.length; i++) {
        Assertions.assertNotNull(arguments[i]);
        Assertions.assertEquals(args[i].getClass().getName(), arguments[i].getClass().getName());
        Assertions.assertEquals(args[i], arguments[i]);
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with Invoker

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

the class DubboRegistryTest method setUp.

@BeforeEach
public void setUp() {
    registryURL = new URL(REGISTRY_PROTOCOL, NetUtils.getLocalHost(), NetUtils.getAvailablePort()).addParameter(Constants.CHECK_KEY, false).setServiceInterface(RegistryService.class.getName());
    serviceURL = new URL(DubboProtocol.NAME, NetUtils.getLocalHost(), NetUtils.getAvailablePort()).addParameter(Constants.CHECK_KEY, false).setServiceInterface(RegistryService.class.getName());
    registryService = new MockDubboRegistry(registryURL);
    invoker = mock(Invoker.class);
    given(invoker.getUrl()).willReturn(serviceURL);
    given(invoker.getInterface()).willReturn(RegistryService.class);
    given(invoker.invoke(new RpcInvocation())).willReturn(null);
    dubboRegistry = new DubboRegistry(invoker, registryService);
    notifyListener = mock(NotifyListener.class);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 15 with Invoker

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

the class RegistryDirectoryTest method testParametersMerge.

@Test
public void testParametersMerge() {
    RegistryDirectory registryDirectory = getRegistryDirectory();
    URL regurl = noMeaningUrl.addParameter("test", "reg").addParameterAndEncoded(REFER_KEY, "key=query&" + LOADBALANCE_KEY + "=" + LeastActiveLoadBalance.NAME);
    RegistryDirectory<RegistryDirectoryTest> registryDirectory2 = new RegistryDirectory(RegistryDirectoryTest.class, regurl);
    registryDirectory2.setProtocol(protocol);
    List<URL> serviceUrls = new ArrayList<URL>();
    // The parameters of the inspection registry need to be cleared
    {
        serviceUrls.clear();
        serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
        registryDirectory.notify(serviceUrls);
        invocation = new RpcInvocation();
        List invokers = registryDirectory.list(invocation);
        Invoker invoker = (Invoker) invokers.get(0);
        URL url = invoker.getUrl();
        Assertions.assertNull(url.getParameter("key"));
    }
    // The parameters of the provider for the inspection service need merge
    {
        serviceUrls.clear();
        serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX2").addParameter("key", "provider"));
        registryDirectory.notify(serviceUrls);
        invocation = new RpcInvocation();
        List invokers = registryDirectory.list(invocation);
        Invoker invoker = (Invoker) invokers.get(0);
        URL url = invoker.getUrl();
        Assertions.assertEquals("provider", url.getParameter("key"));
    }
    // The parameters of the test service query need to be with the providermerge.
    {
        serviceUrls.clear();
        serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX3").addParameter("key", "provider"));
        registryDirectory2.setRegistry(registry);
        registryDirectory2.setRouterChain(RouterChain.buildChain(noMeaningUrl));
        registryDirectory2.subscribe(noMeaningUrl);
        registryDirectory2.notify(serviceUrls);
        invocation = new RpcInvocation();
        List invokers = registryDirectory2.list(invocation);
        Invoker invoker = (Invoker) invokers.get(0);
        URL url = invoker.getUrl();
        Assertions.assertEquals("query", url.getParameter("key"));
    }
    {
        serviceUrls.clear();
        serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
        registryDirectory.notify(serviceUrls);
        invocation = new RpcInvocation();
        List invokers = registryDirectory.list(invocation);
        Invoker invoker = (Invoker) invokers.get(0);
        URL url = invoker.getUrl();
        Assertions.assertFalse(url.getParameter(Constants.CHECK_KEY, false));
    }
    {
        serviceUrls.clear();
        serviceUrls.add(SERVICEURL.addParameter(LOADBALANCE_KEY, RoundRobinLoadBalance.NAME));
        registryDirectory2.notify(serviceUrls);
        invocation = new RpcInvocation();
        invocation.setMethodName("get");
        List invokers = registryDirectory2.list(invocation);
        Invoker invoker = (Invoker) invokers.get(0);
        URL url = invoker.getUrl();
        Assertions.assertEquals(LeastActiveLoadBalance.NAME, url.getMethodParameter("get", LOADBALANCE_KEY));
    }
    // test geturl
    {
        Assertions.assertNull(registryDirectory2.getUrl().getParameter("mock"));
        serviceUrls.clear();
        serviceUrls.add(SERVICEURL.addParameter(MOCK_KEY, "true"));
        registryDirectory2.notify(serviceUrls);
        Assertions.assertEquals("true", ((InvokerWrapper<?>) registryDirectory2.getInvokers().get(0)).getUrl().getParameter("mock"));
    }
}
Also used : RegistryDirectory(org.apache.dubbo.registry.integration.RegistryDirectory) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MockClusterInvoker(org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker) Invoker(org.apache.dubbo.rpc.Invoker) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

Invoker (org.apache.dubbo.rpc.Invoker)128 Test (org.junit.jupiter.api.Test)104 URL (org.apache.dubbo.common.URL)74 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)66 ArrayList (java.util.ArrayList)48 Invocation (org.apache.dubbo.rpc.Invocation)42 Result (org.apache.dubbo.rpc.Result)26 AppResponse (org.apache.dubbo.rpc.AppResponse)21 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)21 RegistryDirectory (org.apache.dubbo.registry.integration.RegistryDirectory)20 Router (org.apache.dubbo.rpc.cluster.Router)18 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 HashMap (java.util.HashMap)13 LoadBalance (org.apache.dubbo.rpc.cluster.LoadBalance)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)12 RpcException (org.apache.dubbo.rpc.RpcException)12 LeastActiveLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)8 RandomLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)8 RoundRobinLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8