From 4c45a3afd3a10e4bec5e303fc9f4869d3d517ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E4=BF=8A?= <39894911+hujun-w-2@users.noreply.github.com> Date: Mon, 7 Feb 2022 10:38:37 +0800 Subject: [PATCH] fix by zero error (#7724) * fix by zero error * fix by zero error --unit test --- .../exception/runtime/NacosLoadException.java | 32 +++++++++++++++++++ .../client/naming/core/ServerListManager.java | 3 ++ .../NacosNamingMaintainServiceTest.java | 5 +-- .../client/naming/NacosNamingServiceTest.java | 1 + .../remote/NamingClientProxyDelegateTest.java | 16 ++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosLoadException.java diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosLoadException.java b/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosLoadException.java new file mode 100644 index 000000000..8bde58d41 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosLoadException.java @@ -0,0 +1,32 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.api.exception.runtime; + +/** + * Nacos load exception. + * + * @author hujun + */ +public class NacosLoadException extends RuntimeException { + + private static final long serialVersionUID = 3513491993982295562L; + + public NacosLoadException(String errMsg) { + super(errMsg); + } + +} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java index 01d4c625c..457948f8b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.client.naming.core; import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.exception.runtime.NacosLoadException; import com.alibaba.nacos.client.naming.event.ServerListChangedEvent; import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientManager; import com.alibaba.nacos.client.naming.utils.CollectionUtils; @@ -85,6 +86,8 @@ public class ServerListManager implements ServerListFactory, Closeable { initServerAddr(properties); if (!serverList.isEmpty()) { currentIndex.set(new Random().nextInt(serverList.size())); + } else { + throw new NacosLoadException("serverList is empty,please check configuration"); } } diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingMaintainServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingMaintainServiceTest.java index c8ab19b9d..e5a84b6e0 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingMaintainServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingMaintainServiceTest.java @@ -62,13 +62,14 @@ public class NacosNamingMaintainServiceTest { public void setUp() throws Exception { Properties prop = new Properties(); prop.setProperty(PropertyKeyConst.NAMESPACE, "public"); + prop.setProperty("serverAddr", "localhost"); + nacosNamingMaintainService = new NacosNamingMaintainService(prop); - serverProxy = mock(NamingHttpClientProxy.class); serverListManager = mock(ServerListManager.class); securityProxy = mock(SecurityProxy.class); executorService = mock(ScheduledExecutorService.class); - + Field serverProxyField = NacosNamingMaintainService.class.getDeclaredField("serverProxy"); serverProxyField.setAccessible(true); serverProxyField.set(nacosNamingMaintainService, serverProxy); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java index 155dfb7c2..18ae41b31 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java @@ -61,6 +61,7 @@ public class NacosNamingServiceTest { @Before public void before() throws NoSuchFieldException, NacosException, IllegalAccessException { Properties prop = new Properties(); + prop.setProperty("serverAddr", "localhost"); prop.put(PropertyKeyConst.NAMESPACE, "test"); client = new NacosNamingService(prop); // inject proxy diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegateTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegateTest.java index 61b761491..29d674f15 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegateTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegateTest.java @@ -49,6 +49,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); @@ -74,6 +75,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingHttpClientProxy mockHttpClient = Mockito.mock(NamingHttpClientProxy.class); @@ -99,6 +101,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); @@ -124,6 +127,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingHttpClientProxy mockHttpClient = Mockito.mock(NamingHttpClientProxy.class); @@ -149,6 +153,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); String serviceName = "service1"; @@ -166,6 +171,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); @@ -185,6 +191,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); Service service = delegate.queryService("a", "b"); @@ -196,6 +203,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); Service service = new Service(); @@ -211,6 +219,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); Assert.assertFalse(delegate.deleteService("service", "group1")); @@ -221,6 +230,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); Service service = new Service(); @@ -236,6 +246,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); @@ -257,6 +268,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); @@ -285,6 +297,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); @@ -305,6 +318,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingHttpClientProxy mockHttpClient = Mockito.mock(NamingHttpClientProxy.class); @@ -323,6 +337,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); @@ -339,6 +354,7 @@ public class NamingClientProxyDelegateTest { String ns = "ns1"; ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); + props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class);