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);
}
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]);
}
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]);
}
}
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);
}
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"));
}
}
Aggregations