From b74cf5be3f427867e69832e74f83d6aaa9276708 Mon Sep 17 00:00:00 2001 From: TomlongTK Date: Wed, 15 Nov 2023 14:05:17 +0800 Subject: [PATCH] Fix async generic method (#13336) --- .../apache/dubbo/rpc/support/RpcUtils.java | 11 +-- .../dubbo/rpc/support/RpcUtilsTest.java | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java index 94d48c72fb..53cd36894d 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java @@ -130,15 +130,12 @@ public class RpcUtils { if (value == null) { // add invocationid in async operation by default return isAsync(url, invocation); - } else if (Boolean.TRUE.toString().equalsIgnoreCase(value)) { - return true; - } else { - return false; } + return Boolean.TRUE.toString().equalsIgnoreCase(value); } public static String getMethodName(Invocation invocation) { - if ($INVOKE.equals(invocation.getMethodName()) + if (($INVOKE.equals(invocation.getMethodName()) || $INVOKE_ASYNC.equals(invocation.getMethodName())) && invocation.getArguments() != null && invocation.getArguments().length > 0 && invocation.getArguments()[0] instanceof String) { @@ -148,7 +145,7 @@ public class RpcUtils { } public static Object[] getArguments(Invocation invocation) { - if ($INVOKE.equals(invocation.getMethodName()) + if (($INVOKE.equals(invocation.getMethodName()) || $INVOKE_ASYNC.equals(invocation.getMethodName())) && invocation.getArguments() != null && invocation.getArguments().length > 2 && invocation.getArguments()[2] instanceof Object[]) { @@ -158,7 +155,7 @@ public class RpcUtils { } public static Class[] getParameterTypes(Invocation invocation) { - if ($INVOKE.equals(invocation.getMethodName()) + if (($INVOKE.equals(invocation.getMethodName()) || $INVOKE_ASYNC.equals(invocation.getMethodName())) && invocation.getArguments() != null && invocation.getArguments().length > 1 && invocation.getArguments()[1] instanceof String[]) { 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 c3b1fb143f..f996c06ec0 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 @@ -401,6 +401,32 @@ class RpcUtilsTest { Assertions.assertEquals(Object.class, parameterTypes5[2]); } + @Test + void testGet_$invokeAsync_ParameterTypes() { + Object[] args = new Object[] {"hello", true, 520}; + Class demoServiceClass = DemoService.class; + String serviceName = demoServiceClass.getName(); + Invoker invoker = createMockInvoker(); + + RpcInvocation inv = new RpcInvocation( + "$invokeAsync", + serviceName, + "", + new Class[] {String.class, String[].class, Object[].class}, + new Object[] { + "method", new String[] {"java.lang.String", "java.lang.Boolean", "java.lang.Integer"}, args + }, + null, + invoker, + null); + + Class[] parameterTypes = RpcUtils.getParameterTypes(inv); + for (int i = 0; i < args.length; i++) { + Assertions.assertNotNull(parameterTypes[i]); + Assertions.assertEquals(args[i].getClass(), parameterTypes[i]); + } + } + @ParameterizedTest @CsvSource({"echo", "stringLength", "testReturnType"}) public void testGetMethodName(String methodName) { @@ -436,6 +462,27 @@ class RpcUtilsTest { Assertions.assertEquals(method, actual); } + @ParameterizedTest + @CsvSource({"hello", "apache", "dubbo"}) + public void testGet_$invokeAsync_MethodName(String method) { + Class demoServiceClass = DemoService.class; + String serviceName = demoServiceClass.getName(); + Invoker invoker = createMockInvoker(); + + RpcInvocation inv = new RpcInvocation( + "$invokeAsync", + serviceName, + "", + 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); + Assertions.assertEquals(method, actual); + } + @Test void testGet_$invoke_Arguments() { Object[] args = new Object[] {"hello", "dubbo", 520}; @@ -462,6 +509,32 @@ class RpcUtilsTest { } } + @Test + void testGet_$invokeAsync_Arguments() { + Object[] args = new Object[] {"hello", "dubbo", 520}; + Class demoServiceClass = DemoService.class; + String serviceName = demoServiceClass.getName(); + Invoker invoker = createMockInvoker(); + + RpcInvocation inv = new RpcInvocation( + "$invokeAsync", + 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]); + } + } + @Test void testIsAsync() { Object[] args = new Object[] {"hello", "dubbo", 520};