From ff2471e8b9d78ea0de852a3793535601b26187ed Mon Sep 17 00:00:00 2001 From: lo1nt Date: Mon, 29 Jul 2024 14:46:32 +0800 Subject: [PATCH] fix: set default 0 when get method timeout config (#1422) * fix: default 0 when get method timeout config * format --------- Co-authored-by: Lo1nt --- .../com/alipay/sofa/rpc/config/MethodConfig.java | 2 +- .../std/config/AbstractInterfaceConfigTest.java | 16 ++++++++++++++++ .../registry/sofa/SofaRegistryHelperTest.java | 11 +++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/config/MethodConfig.java b/core/api/src/main/java/com/alipay/sofa/rpc/config/MethodConfig.java index 611795c49..d8ab50aee 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/config/MethodConfig.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/config/MethodConfig.java @@ -137,7 +137,7 @@ public MethodConfig setParameters(Map parameters) { * @return the timeout */ public Integer getTimeout() { - return timeout; + return timeout == null ? 0 : timeout; } /** diff --git a/core/api/src/test/java/com/alipay/sofa/rpc/std/config/AbstractInterfaceConfigTest.java b/core/api/src/test/java/com/alipay/sofa/rpc/std/config/AbstractInterfaceConfigTest.java index f04724e24..881d310f9 100644 --- a/core/api/src/test/java/com/alipay/sofa/rpc/std/config/AbstractInterfaceConfigTest.java +++ b/core/api/src/test/java/com/alipay/sofa/rpc/std/config/AbstractInterfaceConfigTest.java @@ -23,6 +23,7 @@ import com.alipay.sofa.rpc.config.AbstractInterfaceConfig; import com.alipay.sofa.rpc.config.ApplicationConfig; import com.alipay.sofa.rpc.config.MethodConfig; +import com.alipay.sofa.rpc.config.ProviderConfig; import com.alipay.sofa.rpc.config.RegistryConfig; import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException; import com.alipay.sofa.rpc.listener.ConfigListener; @@ -50,6 +51,21 @@ * @version : AbstractInterfaceConfigTest.java, v 0.1 2022年01月25日 4:46 下午 zhaowang */ public class AbstractInterfaceConfigTest { + @Test + public void testMethodTimeout() { + MethodConfig config = new MethodConfig(); + config.setTimeout(null); + + ProviderConfig p = new ProviderConfig(); + p.setMethods(new HashMap<>()); + p.getMethods().put("test", config); + + try { + Assert.assertFalse(p.hasTimeout()); + } catch (Exception e) { + Assert.fail("exception should not appears: " + e.getMessage()); + } + } @Test public void testDefaultValue() { diff --git a/registry/registry-sofa/src/test/java/com/alipay/sofa/rpc/registry/sofa/SofaRegistryHelperTest.java b/registry/registry-sofa/src/test/java/com/alipay/sofa/rpc/registry/sofa/SofaRegistryHelperTest.java index f00413f51..dd7951e34 100644 --- a/registry/registry-sofa/src/test/java/com/alipay/sofa/rpc/registry/sofa/SofaRegistryHelperTest.java +++ b/registry/registry-sofa/src/test/java/com/alipay/sofa/rpc/registry/sofa/SofaRegistryHelperTest.java @@ -150,6 +150,17 @@ public void convertProviderToUrls() throws Exception { Assert.assertEquals(serverConfig3.getPort(), providerInfo3.getPort()); Assert.assertEquals(providerConfig.getAppName(), providerInfo3.getAttr(ProviderInfoAttrs.ATTR_APP_NAME)); Assert.assertEquals(providerConfig.getTimeout(), providerInfo3.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT)); + + ProviderConfig providerConfig2 = new ProviderConfig<>(); + providerConfig2.setMethods(new HashMap<>()); + providerConfig2.getMethods().put("test", new MethodConfig().setTimeout(null)); + Assert.assertNotNull(providerConfig2.getTimeout()); + String s4 = SofaRegistryHelper.convertProviderToUrls(providerConfig2, serverConfig); + Assert.assertNotNull(s3); + ProviderInfo providerInfo4 = SofaRegistryHelper.parseProviderInfo(s4); + Assert.assertEquals(0, providerInfo4.getDynamicAttr(".test.timeout")); + Assert.assertFalse(providerConfig2.hasTimeout()); + } @Test