From 2f494885a3cba295d951a82b1bfca342ceb5e866 Mon Sep 17 00:00:00 2001 From: Geng Zhang Date: Thu, 3 May 2018 11:53:18 +0800 Subject: [PATCH] Fix NPE when invoke asynchronously use JDKProxy. --- .../rpc/proxy/jdk/JDKInvocationHandler.java | 4 ++ .../sofa/rpc/codec/AllTypeServiceTest.java | 47 +++++++++++++++++++ .../test/resources/sofa-rpc/rpc-config.json | 4 ++ 3 files changed, 55 insertions(+) create mode 100644 test/test-integration/src/test/resources/sofa-rpc/rpc-config.json diff --git a/core-impl/proxy/src/main/java/com/alipay/sofa/rpc/proxy/jdk/JDKInvocationHandler.java b/core-impl/proxy/src/main/java/com/alipay/sofa/rpc/proxy/jdk/JDKInvocationHandler.java index 4b52c47c2..25acfab07 100644 --- a/core-impl/proxy/src/main/java/com/alipay/sofa/rpc/proxy/jdk/JDKInvocationHandler.java +++ b/core-impl/proxy/src/main/java/com/alipay/sofa/rpc/proxy/jdk/JDKInvocationHandler.java @@ -16,6 +16,7 @@ */ package com.alipay.sofa.rpc.proxy.jdk; +import com.alipay.sofa.rpc.common.utils.ClassUtils; import com.alipay.sofa.rpc.core.exception.RpcErrorType; import com.alipay.sofa.rpc.core.exception.SofaRpcException; import com.alipay.sofa.rpc.core.request.SofaRequest; @@ -79,6 +80,9 @@ public Object invoke(Object proxy, Method method, Object[] paramValues) if (ret instanceof Throwable) { throw (Throwable) ret; } else { + if (ret == null) { + return ClassUtils.getDefaultArg(method.getReturnType()); + } return ret; } } diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/codec/AllTypeServiceTest.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/codec/AllTypeServiceTest.java index 6bf85fcf8..d81be924e 100644 --- a/test/test-integration/src/test/java/com/alipay/sofa/rpc/codec/AllTypeServiceTest.java +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/codec/AllTypeServiceTest.java @@ -16,10 +16,12 @@ */ package com.alipay.sofa.rpc.codec; +import com.alipay.sofa.rpc.common.RpcConstants; import com.alipay.sofa.rpc.common.struct.ConcurrentHashSet; import com.alipay.sofa.rpc.config.ConsumerConfig; import com.alipay.sofa.rpc.config.ProviderConfig; import com.alipay.sofa.rpc.config.ServerConfig; +import com.alipay.sofa.rpc.context.RpcInvokeContext; import com.alipay.sofa.rpc.test.ActivelyDestroyTest; import org.junit.Assert; import org.junit.Test; @@ -55,6 +57,7 @@ public class AllTypeServiceTest extends ActivelyDestroyTest { public void testAll() { ServerConfig serverConfig2 = new ServerConfig() .setPort(22222) + .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT) .setDaemon(false); ProviderConfig CProvider = new ProviderConfig() @@ -184,5 +187,49 @@ public void testAll() { Assert.assertEquals(obj, helloService.echoSubObj(obj)); Assert.assertEquals(subObj, helloService.echoSubObj(subObj)); + + { + ConsumerConfig aConsumer = new ConsumerConfig() + .setInterfaceId(AllTypeService.class.getName()) + .setTimeout(50000) + .setSerialization("hessian") + .setRepeatedReferLimit(-1) + .setProxy("javassist") + .setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE) + .setDirectUrl("bolt://127.0.0.1:22222"); + AllTypeService helloService2 = aConsumer.refer(); + Assert.assertEquals(0, helloService2.echoInt(1)); + boolean error = false; + Integer v = null; + try { + v = (Integer) RpcInvokeContext.getContext().getFuture().get(); + } catch (Exception e) { + error = true; + } + Assert.assertFalse(error); + Assert.assertTrue(v == 1); + } + + { + ConsumerConfig aConsumer = new ConsumerConfig() + .setInterfaceId(AllTypeService.class.getName()) + .setTimeout(50000) + .setSerialization("hessian") + .setRepeatedReferLimit(-1) + .setProxy("jdk") + .setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE) + .setDirectUrl("bolt://127.0.0.1:22222"); + AllTypeService helloService2 = aConsumer.refer(); + Assert.assertEquals(0, helloService2.echoInt(1)); + boolean error = false; + Integer v = null; + try { + v = (Integer) RpcInvokeContext.getContext().getFuture().get(); + } catch (Exception e) { + error = true; + } + Assert.assertFalse(error); + Assert.assertTrue(v == 1); + } } } diff --git a/test/test-integration/src/test/resources/sofa-rpc/rpc-config.json b/test/test-integration/src/test/resources/sofa-rpc/rpc-config.json new file mode 100644 index 000000000..5b573549a --- /dev/null +++ b/test/test-integration/src/test/resources/sofa-rpc/rpc-config.json @@ -0,0 +1,4 @@ +{ + "rpc.config.order": 999, + "logger.impl": "com.alipay.sofa.rpc.log.SLF4JLoggerImpl" +} \ No newline at end of file