This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 456c34f9d add shenyu-plugin-metrics unit test (#3669)
456c34f9d is described below

commit 456c34f9d22b569b664b09c208b182fcd496d67c
Author: qinghai777 <[email protected]>
AuthorDate: Thu Jul 7 10:45:16 2022 +0800

    add shenyu-plugin-metrics unit test (#3669)
---
 shenyu-plugin/shenyu-plugin-metrics/pom.xml        |   5 +
 .../shenyu/plugin/metrics/MetricsPluginTest.java   |  87 +++++++++++++++
 .../shenyu/plugin/metrics/config/MetricTest.java   |  50 +++++++++
 .../prometheus/PrometheusMetricsRegisterTest.java  |  39 +++++++
 .../metrics/reporter/MetricsReporterTest.java      | 122 ++++++++++++++++++++-
 5 files changed, 301 insertions(+), 2 deletions(-)

diff --git a/shenyu-plugin/shenyu-plugin-metrics/pom.xml 
b/shenyu-plugin/shenyu-plugin-metrics/pom.xml
index 002b5594d..2223cfb14 100644
--- a/shenyu-plugin/shenyu-plugin-metrics/pom.xml
+++ b/shenyu-plugin/shenyu-plugin-metrics/pom.xml
@@ -61,6 +61,11 @@
             <artifactId>spring-test</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>io.projectreactor</groupId>
+            <artifactId>reactor-test</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>
\ No newline at end of file
diff --git 
a/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/MetricsPluginTest.java
 
b/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/MetricsPluginTest.java
new file mode 100644
index 000000000..c987ecc10
--- /dev/null
+++ 
b/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/MetricsPluginTest.java
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shenyu.plugin.metrics;
+
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.plugin.api.RemoteAddressResolver;
+import org.apache.shenyu.plugin.api.ShenyuPluginChain;
+import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mockito;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
+import org.springframework.mock.web.server.MockServerWebExchange;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+import reactor.test.StepVerifier;
+
+import java.net.InetSocketAddress;
+
+/**
+ * The Test Case For MetricsPlugin.
+ */
+public class MetricsPluginTest {
+
+    private MetricsPlugin metricsPlugin;
+
+    private ServerWebExchange exchange;
+
+    private ShenyuPluginChain chain;
+
+    @BeforeEach
+    public void setUp() {
+        this.metricsPlugin = new MetricsPlugin();
+        this.chain = Mockito.mock(ShenyuPluginChain.class);
+        MockServerHttpRequest request = MockServerHttpRequest
+                .get("localhost")
+                .remoteAddress(new InetSocketAddress(8090))
+                .header("X-source", "mock test")
+                .queryParam("queryParam", "Hello,World")
+                .build();
+        ConfigurableApplicationContext context = 
Mockito.mock(ConfigurableApplicationContext.class);
+        SpringBeanUtils.getInstance().setApplicationContext(context);
+        RemoteAddressResolver remoteAddressResolver = new 
RemoteAddressResolver() {
+        };
+        
Mockito.lenient().when(context.getBean(RemoteAddressResolver.class)).thenReturn(remoteAddressResolver);
+        this.exchange = Mockito.spy(MockServerWebExchange.from(request));
+        ShenyuContext shenyuContext = Mockito.mock(ShenyuContext.class);
+        exchange.getAttributes().put(Constants.CONTEXT, shenyuContext);
+    }
+
+    @Test
+    public void testDoExecute() {
+        
Mockito.when(chain.execute(ArgumentMatchers.any())).thenReturn(Mono.empty());
+        Mono<Void> result = metricsPlugin.execute(exchange, chain);
+        StepVerifier.create(result).expectSubscription().verifyComplete();
+    }
+
+    @Test
+    public void testGetOrder() {
+        Assertions.assertEquals(metricsPlugin.getOrder(), 
PluginEnum.METRICS.getCode());
+    }
+
+    @Test
+    public void testNamed() {
+        Assertions.assertEquals(metricsPlugin.named(), 
PluginEnum.METRICS.getName());
+    }
+}
diff --git 
a/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/config/MetricTest.java
 
b/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/config/MetricTest.java
new file mode 100644
index 000000000..0378462f5
--- /dev/null
+++ 
b/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/config/MetricTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shenyu.plugin.metrics.config;
+
+import org.apache.shenyu.plugin.metrics.enums.MetricType;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * The Test Case For Metric.
+ */
+public final class MetricTest {
+
+    private static final String NAME = "testName";
+
+    private static final String DOCUMENT = "testDocument";
+
+    private List<String> labels;
+
+    private Metric metric;
+
+    @Test
+    public void testMetric() {
+        labels = new ArrayList<>();
+        labels.add("shenyu_request_total");
+        metric = new Metric(MetricType.COUNTER, NAME, DOCUMENT, labels);
+        Assertions.assertEquals(metric.getType(), MetricType.COUNTER);
+        Assertions.assertEquals(metric.getName(), "testName");
+        Assertions.assertEquals(metric.getDocument(), "testDocument");
+        Assertions.assertEquals(metric.getLabels().get(0), 
"shenyu_request_total");
+    }
+}
diff --git 
a/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/prometheus/PrometheusMetricsRegisterTest.java
 
b/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/prometheus/PrometheusMetricsRegisterTest.java
index c8a5c2e72..417f76aa1 100644
--- 
a/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/prometheus/PrometheusMetricsRegisterTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/prometheus/PrometheusMetricsRegisterTest.java
@@ -18,10 +18,14 @@
 package org.apache.shenyu.plugin.metrics.prometheus;
 
 import io.prometheus.client.Counter;
+import io.prometheus.client.Gauge;
 import io.prometheus.client.Histogram;
 import org.apache.shenyu.common.utils.ReflectUtils;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
+import org.springframework.util.CollectionUtils;
 
+import java.lang.reflect.Field;
 import java.util.Map;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -63,4 +67,39 @@ public final class PrometheusMetricsRegisterTest {
         Histogram histogram = histogramMap.get(name);
         assertThat(histogram.labels(labelNames).get().sum, is(1000.0));
     }
+
+    @Test
+    public void testRegisterGauge() throws Exception {
+        String name = "request_throw_total";
+        String[] labelNames = new String[] {"name1", "name2"};
+        prometheusMetricsRegister.registerGauge(name, labelNames, "shenyu 
request total count");
+        Field field = 
prometheusMetricsRegister.getClass().getDeclaredField("GAUGE_MAP");
+        field.setAccessible(true);
+        Map<String, Gauge> map = (Map<String, Gauge>) 
field.get(prometheusMetricsRegister);
+        Assertions.assertEquals(map.get(name).describe().toString(),
+                "[Name: request_throw_total Unit: Type: GAUGE Help: shenyu 
request total count Samples: []]");
+        Gauge gauge = map.get(name);
+        prometheusMetricsRegister.gaugeIncrement(name, labelNames);
+        Assertions.assertEquals(gauge.labels(labelNames).get(), 1.0);
+        prometheusMetricsRegister.gaugeDecrement(name, labelNames);
+        Assertions.assertEquals(gauge.labels(labelNames).get(), 0.0);
+        prometheusMetricsRegister.clean();
+    }
+
+    @Test
+    public void testClean() throws Exception {
+        prometheusMetricsRegister.clean();
+        Field field1 = 
prometheusMetricsRegister.getClass().getDeclaredField("COUNTER_MAP");
+        field1.setAccessible(true);
+        Map<String, Counter> map1 = (Map<String, Counter>) 
field1.get(prometheusMetricsRegister);
+        Assertions.assertTrue(CollectionUtils.isEmpty(map1));
+        Field field2 = 
prometheusMetricsRegister.getClass().getDeclaredField("GAUGE_MAP");
+        field2.setAccessible(true);
+        Map<String, Gauge> map2 = (Map<String, Gauge>) 
field2.get(prometheusMetricsRegister);
+        Assertions.assertTrue(CollectionUtils.isEmpty(map2));
+        Field field3 = 
prometheusMetricsRegister.getClass().getDeclaredField("HISTOGRAM_MAP");
+        field3.setAccessible(true);
+        Map<String, Histogram> map3 = (Map<String, Histogram>) 
field3.get(prometheusMetricsRegister);
+        Assertions.assertTrue(CollectionUtils.isEmpty(map3));
+    }
 }
diff --git 
a/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporterTest.java
 
b/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporterTest.java
index 3b1bfb4af..946bb72dd 100644
--- 
a/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporterTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporterTest.java
@@ -17,9 +17,127 @@
 
 package org.apache.shenyu.plugin.metrics.reporter;
 
+import io.prometheus.client.Counter;
+import io.prometheus.client.Gauge;
+import io.prometheus.client.Histogram;
+import org.apache.shenyu.common.utils.ReflectUtils;
+import org.apache.shenyu.plugin.metrics.config.Metric;
+import org.apache.shenyu.plugin.metrics.enums.MetricType;
+import org.apache.shenyu.plugin.metrics.prometheus.PrometheusMetricsRegister;
+import org.apache.shenyu.plugin.metrics.spi.MetricsRegister;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.springframework.util.CollectionUtils;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
 /**
- * MetricsReporterTest.
+ * The Test Case For MetricsReporter.
  */
 public final class MetricsReporterTest {
-    
+
+    private static final String DOCUMENT = "testDocument";
+
+    private static MetricsRegister metricsRegister;
+
+    @BeforeAll
+    public static void setUp() {
+        metricsRegister = new PrometheusMetricsRegister();
+        MetricsReporter.register(metricsRegister);
+    }
+
+    @Test
+    public void testRegister() throws Exception {
+        Field field1 = 
metricsRegister.getClass().getDeclaredField("COUNTER_MAP");
+        field1.setAccessible(true);
+        Map<String, Counter> map1 = (Map<String, Counter>) 
field1.get(metricsRegister);
+        Assertions.assertEquals(map1.size(), 3);
+        Field field2 = 
metricsRegister.getClass().getDeclaredField("HISTOGRAM_MAP");
+        field2.setAccessible(true);
+        Map<String, Histogram> map2 = (Map<String, Histogram>) 
field2.get(metricsRegister);
+        Assertions.assertEquals(map2.size(), 3);
+        List<String> labels = new ArrayList<>();
+        labels.add("shenyu_request_total");
+        Collection<Metric> metrics = new ArrayList<>();
+        metrics.add(new Metric(MetricType.COUNTER, "name1", DOCUMENT, labels));
+        metrics.add(new Metric(MetricType.GAUGE, "name2", DOCUMENT, labels));
+        metrics.add(new Metric(MetricType.HISTOGRAM, "name3", DOCUMENT, 
labels));
+        MetricsReporter.registerMetrics(metrics);
+        Field field3 = 
metricsRegister.getClass().getDeclaredField("COUNTER_MAP");
+        field3.setAccessible(true);
+        Map<String, Counter> map3 = (Map<String, Counter>) 
field3.get(metricsRegister);
+        Assertions.assertEquals(map3.size(), 4);
+        Field field4 = 
metricsRegister.getClass().getDeclaredField("HISTOGRAM_MAP");
+        field4.setAccessible(true);
+        Map<String, Histogram> map4 = (Map<String, Histogram>) 
field4.get(metricsRegister);
+        Assertions.assertEquals(map4.size(), 4);
+        Field field5 = 
metricsRegister.getClass().getDeclaredField("GAUGE_MAP");
+        field5.setAccessible(true);
+        Map<String, Gauge> map5 = (Map<String, Gauge>) 
field5.get(metricsRegister);
+        Assertions.assertEquals(map5.size(), 3);
+        MetricsReporter.clean();
+        Assertions.assertTrue(CollectionUtils.isEmpty(map3));
+    }
+
+    @Test
+    public void testGauge() throws Exception {
+        String name1 = "request_throw_total1";
+        String name2 = "request_throw_total2";
+        String[] labelNames = new String[] {"name1", "name2"};
+        MetricsReporter.registerGauge(name1, labelNames, "shenyu request total 
count");
+        MetricsReporter.registerGauge(name2, "shenyu request total count");
+        MetricsReporter.gaugeIncrement(name1, labelNames);
+        Field field1 = 
metricsRegister.getClass().getDeclaredField("GAUGE_MAP");
+        field1.setAccessible(true);
+        Map<String, Gauge> map1 = (Map<String, Gauge>) 
field1.get(metricsRegister);
+        Gauge gauge1 = map1.get(name1);
+        Assertions.assertEquals(gauge1.labels(labelNames).get(), 1.0);
+        MetricsReporter.gaugeIncrement(name2);
+        Field field2 = 
metricsRegister.getClass().getDeclaredField("GAUGE_MAP");
+        field2.setAccessible(true);
+        Map<String, Gauge> map2 = (Map<String, Gauge>) 
field2.get(metricsRegister);
+        Gauge gauge2 = map2.get(name2);
+        Assertions.assertEquals(gauge2.get(), 1.0);
+        MetricsReporter.gaugeDecrement(name1, labelNames);
+        Field field3 = 
metricsRegister.getClass().getDeclaredField("GAUGE_MAP");
+        field3.setAccessible(true);
+        Map<String, Gauge> map3 = (Map<String, Gauge>) 
field3.get(metricsRegister);
+        Gauge gauge3 = map3.get(name1);
+        Assertions.assertEquals(gauge3.labels(labelNames).get(), 0.0);
+        MetricsReporter.gaugeDecrement(name2);
+        Field field4 = 
metricsRegister.getClass().getDeclaredField("GAUGE_MAP");
+        field4.setAccessible(true);
+        Map<String, Gauge> map4 = (Map<String, Gauge>) 
field4.get(metricsRegister);
+        Gauge gauge4 = map4.get(name2);
+        Assertions.assertEquals(gauge4.get(), 0.0);
+    }
+
+    @Test
+    public void testRecordTime() {
+        String name1 = "requests_latency_histogram_millis1";
+        String name2 = "requests_latency_histogram_millis2";
+        String[] labelNames = new String[] {"name"};
+        MetricsReporter.registerHistogram(name1, labelNames, "the shenyu proxy 
executor latency millis");
+        MetricsReporter.registerHistogram(name2, "the shenyu proxy executor 
latency millis");
+        MetricsReporter.recordTime(name1, labelNames, 1000);
+        Map<String, Histogram> histogramMap1 = (Map<String, Histogram>) 
ReflectUtils.getFieldValue(metricsRegister, "HISTOGRAM_MAP");
+        Histogram histogram1 = histogramMap1.get(name1);
+        Assertions.assertEquals(histogram1.labels(labelNames).get().sum, 
1000.0);
+        MetricsReporter.recordTime(name2, 1000);
+        Map<String, Histogram> histogramMap2 = (Map<String, Histogram>) 
ReflectUtils.getFieldValue(metricsRegister, "HISTOGRAM_MAP");
+        Histogram histogram2 = histogramMap2.get(name2);
+        Assertions.assertEquals(histogram2.labels().get().sum, 1000.0);
+    }
+
+    @AfterAll
+    public static void clean() {
+        metricsRegister.clean();
+        MetricsReporter.clean();
+    }
 }

Reply via email to