diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/AbstractClient.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/AbstractClient.java index c4087baba..23c7086a6 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/AbstractClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/AbstractClient.java @@ -187,7 +187,7 @@ public abstract class AbstractClient implements Client { MetricsMonitor.getIpCountMonitor().decrementAndGet(); } } - MetricsMonitor.getIpCountMonitor().addAndGet(-1 * subscribers.size()); + MetricsMonitor.getSubscriberCount().addAndGet(-1 * subscribers.size()); } @Override diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/AbstractClientTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/AbstractClientTest.java index a38508cb4..c26e2894f 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/AbstractClientTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/AbstractClientTest.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.naming.core.v2.client; import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo; import com.alibaba.nacos.naming.core.v2.pojo.Service; +import com.alibaba.nacos.naming.monitor.MetricsMonitor; import com.alibaba.nacos.naming.pojo.Subscriber; import org.junit.Assert; import org.junit.Before; @@ -45,16 +46,18 @@ public class AbstractClientTest { instancePublishInfo = new InstancePublishInfo("127.0.0.1", 8890); subscriber = new Subscriber("127.0.0.1:8848", "agent1", "appName", "127.0.0.1", "ns1", "serviceName001", 9090); - addServiceInstance(); - addServiceSubscriber(); + MetricsMonitor.getIpCountMonitor().set(0); + MetricsMonitor.getSubscriberCount().set(0); } - private void addServiceInstance() { + @Test + public void addServiceInstance() { boolean result = abstractClient.addServiceInstance(service, instancePublishInfo); Assert.assertTrue(result); } - private void addServiceSubscriber() { + @Test + public void addServiceSubscriber() { Assert.assertTrue(abstractClient.addServiceSubscriber(service, subscriber)); } @@ -65,12 +68,14 @@ public class AbstractClientTest { @Test public void removeServiceInstanceSuccess() { + addServiceInstance(); InstancePublishInfo publishInfo = abstractClient.removeServiceInstance(service); Assert.assertNotNull(publishInfo); } @Test public void getInstancePublishInfo() { + addServiceInstance(); InstancePublishInfo publishInfo = abstractClient.getInstancePublishInfo(service); Assert.assertNotNull(publishInfo); } @@ -89,6 +94,7 @@ public class AbstractClientTest { @Test public void getSubscriber() { + addServiceSubscriber(); Subscriber subscriber1 = abstractClient.getSubscriber(service); Assert.assertNotNull(subscriber1); } @@ -107,11 +113,15 @@ public class AbstractClientTest { @Test public void release() { - try { - abstractClient.release(); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertNotNull(e); - } + + abstractClient.addServiceInstance(service, instancePublishInfo); + Assert.assertEquals(1, MetricsMonitor.getIpCountMonitor().get()); + abstractClient.addServiceSubscriber(service, subscriber); + Assert.assertEquals(1, MetricsMonitor.getSubscriberCount().get()); + + abstractClient.release(); + + Assert.assertEquals(0, MetricsMonitor.getSubscriberCount().get()); + Assert.assertEquals(0, MetricsMonitor.getIpCountMonitor().get()); } }