This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 77abdb4 Add test case for RpcUtils. (#7707)
77abdb4 is described below
commit 77abdb4ab0eb6c011d9fc437881a94289ffb4e50
Author: xiaoheng1 <[email protected]>
AuthorDate: Tue May 11 10:39:45 2021 +0800
Add test case for RpcUtils. (#7707)
---
.../org/apache/dubbo/rpc/support/RpcUtilsTest.java | 100 +++++++++++++++------
1 file changed, 71 insertions(+), 29 deletions(-)
diff --git
a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java
b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java
index 00deb91..f6111df 100644
---
a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java
+++
b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/RpcUtilsTest.java
@@ -18,6 +18,7 @@ package org.apache.dubbo.rpc.support;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.InvokeMode;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcInvocation;
@@ -51,7 +52,7 @@ public class RpcUtilsTest {
URL url = URL.valueOf("dubbo://localhost/?test.async=true");
Map<String, Object> attachments = new HashMap<>();
attachments.put("aa", "bb");
- Invocation inv = new RpcInvocation("test", "DemoService", "", new
Class[]{}, new String[]{}, attachments);
+ Invocation inv = new RpcInvocation("test", "DemoService", "", new
Class[] {}, new String[] {}, attachments);
RpcUtils.attachInvocationIdIfAsync(url, inv);
long id1 = RpcUtils.getInvocationId(inv);
RpcUtils.attachInvocationIdIfAsync(url, inv);
@@ -68,7 +69,7 @@ public class RpcUtilsTest {
@Test
public void testAttachInvocationIdIfAsync_sync() {
URL url = URL.valueOf("dubbo://localhost/");
- Invocation inv = new RpcInvocation("test", "DemoService", "", new
Class[]{}, new String[]{});
+ Invocation inv = new RpcInvocation("test", "DemoService", "", new
Class[] {}, new String[] {});
RpcUtils.attachInvocationIdIfAsync(url, inv);
assertNull(RpcUtils.getInvocationId(inv));
}
@@ -80,7 +81,7 @@ public class RpcUtilsTest {
@Test
public void testAttachInvocationIdIfAsync_nullAttachments() {
URL url = URL.valueOf("dubbo://localhost/?test.async=true");
- Invocation inv = new RpcInvocation("test", "DemoService", "", new
Class[]{}, new String[]{});
+ Invocation inv = new RpcInvocation("test", "DemoService", "", new
Class[] {}, new String[] {});
RpcUtils.attachInvocationIdIfAsync(url, inv);
assertTrue(RpcUtils.getInvocationId(inv) >= 0L);
}
@@ -92,7 +93,7 @@ public class RpcUtilsTest {
@Test
public void testAttachInvocationIdIfAsync_forceNotAttache() {
URL url = URL.valueOf("dubbo://localhost/?test.async=true&" +
AUTO_ATTACH_INVOCATIONID_KEY + "=false");
- Invocation inv = new RpcInvocation("test", "DemoService", "", new
Class[]{}, new String[]{});
+ Invocation inv = new RpcInvocation("test", "DemoService", "", new
Class[] {}, new String[] {});
RpcUtils.attachInvocationIdIfAsync(url, inv);
assertNull(RpcUtils.getInvocationId(inv));
}
@@ -104,7 +105,7 @@ public class RpcUtilsTest {
@Test
public void testAttachInvocationIdIfAsync_forceAttache() {
URL url = URL.valueOf("dubbo://localhost/?" +
AUTO_ATTACH_INVOCATIONID_KEY + "=true");
- Invocation inv = new RpcInvocation("test", "DemoService", "", new
Class[]{}, new String[]{});
+ Invocation inv = new RpcInvocation("test", "DemoService", "", new
Class[] {}, new String[] {});
RpcUtils.attachInvocationIdIfAsync(url, inv);
assertNotNull(RpcUtils.getInvocationId(inv));
}
@@ -114,33 +115,35 @@ public class RpcUtilsTest {
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"));
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
@@ -152,9 +155,10 @@ public class RpcUtilsTest {
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"));
+ 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"));
- RpcInvocation inv = new RpcInvocation("testReturnType", serviceName,
"", new Class<?>[]{String.class}, null, null, invoker, null);
+ RpcInvocation inv = new RpcInvocation("testReturnType", serviceName,
"", new Class<?>[] {String.class}, null, null, invoker, null);
Type[] types = RpcUtils.getReturnTypes(inv);
Assertions.assertNotNull(types);
Assertions.assertEquals(2, types.length);
@@ -162,7 +166,8 @@ public class RpcUtilsTest {
Assertions.assertEquals(String.class, types[1]);
Assertions.assertArrayEquals(types, inv.getReturnTypes());
- RpcInvocation inv1 = new RpcInvocation("testReturnType1", serviceName,
"", new Class<?>[]{String.class}, null, null, invoker, null);
+ RpcInvocation inv1 =
+ new RpcInvocation("testReturnType1", serviceName, "", new
Class<?>[] {String.class}, null, null, invoker, null);
java.lang.reflect.Type[] types1 = RpcUtils.getReturnTypes(inv1);
Assertions.assertNotNull(types1);
Assertions.assertEquals(2, types1.length);
@@ -170,7 +175,8 @@ public class RpcUtilsTest {
Assertions.assertEquals(demoServiceClass.getMethod("testReturnType1",
String.class).getGenericReturnType(), types1[1]);
Assertions.assertArrayEquals(types1, inv1.getReturnTypes());
- RpcInvocation inv2 = new RpcInvocation("testReturnType2", serviceName,
"", new Class<?>[]{String.class}, null, null, invoker, null);
+ RpcInvocation inv2 =
+ new RpcInvocation("testReturnType2", serviceName, "", new
Class<?>[] {String.class}, null, null, invoker, null);
java.lang.reflect.Type[] types2 = RpcUtils.getReturnTypes(inv2);
Assertions.assertNotNull(types2);
Assertions.assertEquals(2, types2.length);
@@ -178,7 +184,8 @@ public class RpcUtilsTest {
Assertions.assertEquals(String.class, types2[1]);
Assertions.assertArrayEquals(types2, inv2.getReturnTypes());
- RpcInvocation inv3 = new RpcInvocation("testReturnType3", serviceName,
"", new Class<?>[]{String.class}, null, null, invoker, null);
+ RpcInvocation inv3 =
+ new RpcInvocation("testReturnType3", serviceName, "", new
Class<?>[] {String.class}, null, null, invoker, null);
java.lang.reflect.Type[] types3 = RpcUtils.getReturnTypes(inv3);
Assertions.assertNotNull(types3);
Assertions.assertEquals(2, types3.length);
@@ -187,7 +194,8 @@ public class RpcUtilsTest {
Assertions.assertEquals(((ParameterizedType)
genericReturnType3).getActualTypeArguments()[0], types3[1]);
Assertions.assertArrayEquals(types3, inv3.getReturnTypes());
- RpcInvocation inv4 = new RpcInvocation("testReturnType4", serviceName,
"", new Class<?>[]{String.class}, null, null, invoker, null);
+ RpcInvocation inv4 =
+ new RpcInvocation("testReturnType4", serviceName, "", new
Class<?>[] {String.class}, null, null, invoker, null);
java.lang.reflect.Type[] types4 = RpcUtils.getReturnTypes(inv4);
Assertions.assertNotNull(types4);
Assertions.assertEquals(2, types4.length);
@@ -195,7 +203,8 @@ public class RpcUtilsTest {
Assertions.assertNull(types4[1]);
Assertions.assertArrayEquals(types4, inv4.getReturnTypes());
- RpcInvocation inv5 = new RpcInvocation("testReturnType5", serviceName,
"", new Class<?>[]{String.class}, null, null, invoker, null);
+ RpcInvocation inv5 =
+ new RpcInvocation("testReturnType5", serviceName, "", new
Class<?>[] {String.class}, null, null, invoker, null);
java.lang.reflect.Type[] types5 = RpcUtils.getReturnTypes(inv5);
Assertions.assertNotNull(types5);
Assertions.assertEquals(2, types5.length);
@@ -213,7 +222,7 @@ public class RpcUtilsTest {
// void sayHello(String name);
RpcInvocation inv1 = new RpcInvocation("sayHello", serviceName, "",
- new Class<?>[]{String.class}, null, null, invoker, null);
+ new Class<?>[] {String.class}, null, null, invoker, null);
Class<?>[] parameterTypes1 = RpcUtils.getParameterTypes(inv1);
Assertions.assertNotNull(parameterTypes1);
Assertions.assertEquals(1, parameterTypes1.length);
@@ -226,7 +235,7 @@ public class RpcUtilsTest {
//Type enumlength(Type... types);
RpcInvocation inv3 = new RpcInvocation("enumlength", serviceName, "",
- new Class<?>[]{Type.class, Type.class}, null, null, invoker,
null);
+ new Class<?>[] {Type.class, Type.class}, null, null, invoker,
null);
Class<?>[] parameterTypes3 = RpcUtils.getParameterTypes(inv3);
Assertions.assertNotNull(parameterTypes3);
Assertions.assertEquals(2, parameterTypes3.length);
@@ -235,7 +244,7 @@ public class RpcUtilsTest {
//byte getbyte(byte arg);
RpcInvocation inv4 = new RpcInvocation("getbyte", serviceName, "",
- new Class<?>[]{byte.class}, null, null, invoker, null);
+ new Class<?>[] {byte.class}, null, null, invoker, null);
Class<?>[] parameterTypes4 = RpcUtils.getParameterTypes(inv4);
Assertions.assertNotNull(parameterTypes4);
Assertions.assertEquals(1, parameterTypes4.length);
@@ -243,8 +252,8 @@ public class RpcUtilsTest {
//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"}},
+ 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);
@@ -266,7 +275,7 @@ public class RpcUtilsTest {
Invoker invoker = mock(Invoker.class);
RpcInvocation inv1 = new RpcInvocation(methodName, serviceName, "",
- new Class<?>[]{String.class}, null, null, invoker, null);
+ new Class<?>[] {String.class}, null, null, invoker, null);
String actual = RpcUtils.getMethodName(inv1);
Assertions.assertNotNull(actual);
Assertions.assertEquals(methodName, actual);
@@ -284,8 +293,8 @@ public class RpcUtilsTest {
Invoker invoker = mock(Invoker.class);
RpcInvocation inv = new RpcInvocation("$invoke", serviceName, "",
- new Class<?>[]{String.class, String[].class},
- new Object[]{method, new String[]{"java.lang.String", "void",
"java.lang.Object"}},
+ new Class<?>[] {String.class, String[].class},
+ new Object[] {method, new String[] {"java.lang.String",
"void", "java.lang.Object"}},
null, invoker, null);
String actual = RpcUtils.getMethodName(inv);
Assertions.assertNotNull(actual);
@@ -295,14 +304,14 @@ public class RpcUtilsTest {
@Test
public void testGet_$invoke_Arguments() {
- Object[] args = new Object[]{"hello", "dubbo", 520};
+ 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},
+ new Class<?>[] {String.class, String[].class, Object[].class},
+ new Object[] {"method", new String[] {}, args},
null, invoker, null);
Object[] arguments = RpcUtils.getArguments(inv);
@@ -313,4 +322,37 @@ public class RpcUtilsTest {
}
}
+ @Test
+ public void testIsAsync() {
+ Object[] args = new Object[] {"hello", "dubbo", 520};
+ Class<?> demoServiceClass = DemoService.class;
+ String serviceName = demoServiceClass.getName();
+ Invoker invoker = mock(Invoker.class);
+
+ URL url = URL.valueOf(
+
"test://127.0.0.1:1/org.apache.dubbo.rpc.support.DemoService?interface=org.apache.dubbo.rpc.support.DemoService");
+
+ RpcInvocation inv = new RpcInvocation("test", serviceName, "",
+ new Class<?>[] {String.class, String[].class, Object[].class},
+ new Object[] {"method", new String[] {}, args},
+ null, invoker, null);
+
+ Assertions.assertFalse(RpcUtils.isAsync(url, inv));
+ inv.setInvokeMode(InvokeMode.ASYNC);
+ Assertions.assertTrue(RpcUtils.isAsync(url, inv));
+ }
+
+ @Test
+ public void testIsGenericCall() {
+
Assertions.assertTrue(RpcUtils.isGenericCall("Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;",
"$invoke"));
+
Assertions.assertTrue(RpcUtils.isGenericCall("Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;",
"$invokeAsync"));
+
Assertions.assertFalse(RpcUtils.isGenericCall("Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;",
"testMethod"));
+ }
+
+ @Test
+ public void testIsEcho() {
+ Assertions.assertTrue(RpcUtils.isEcho("Ljava/lang/Object;", "$echo"));
+ Assertions.assertFalse(RpcUtils.isEcho("Ljava/lang/Object;",
"testMethod"));
+ Assertions.assertFalse(RpcUtils.isEcho("Ljava/lang/String;", "$echo"));
+ }
}