From 83855da7340e6d472f9ba0e7fc0e2bb2d7cbf55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B5=A9=20Nineteen?= <2499904609@qq.com> Date: Fri, 21 Oct 2022 09:26:20 +0800 Subject: [PATCH] buf fix ip count metrics (#9355) * buf fix ip count metrics * run ci * run ci --- .../naming/core/v2/client/AbstractClient.java | 2 +- .../core/v2/client/AbstractClientTest.java | 30 ++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) 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()); } }