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

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new e14b4e9da78 Add test cases for MetricsExporter (#23647)
e14b4e9da78 is described below

commit e14b4e9da78276e24e05c8e258d5425bb9658d03
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Jan 19 13:42:14 2023 +0800

    Add test cases for MetricsExporter (#23647)
---
 .../core/advice/RouteResultCountAdviceTest.java    |  4 +-
 .../core/advice/SQLParseCountAdviceTest.java       |  3 +-
 .../core/advice/SQLRouteCountAdviceTest.java       |  3 +-
 .../proxy/CommitTransactionsCountAdviceTest.java   |  3 +-
 .../proxy/CurrentConnectionsCountAdviceTest.java   |  3 +-
 .../advice/proxy/ExecuteErrorsCountAdviceTest.java |  3 +-
 .../proxy/ExecuteLatencyHistogramAdviceTest.java   |  3 +-
 .../core/advice/proxy/RequestsCountAdviceTest.java |  3 +-
 .../proxy/RollbackTransactionsCountAdviceTest.java |  3 +-
 .../impl/JDKBuildInfoExporterTest.java}            | 23 +++----
 .../impl/proxy/ProxyMetaDataInfoExporterTest.java  | 68 +++++++++++++++++++
 .../impl/proxy/ProxyStateExporterTest.java         | 78 ++++++++++++++++++++++
 .../core/fixture/MetricsCollectorFixture.java      | 15 ++++-
 13 files changed, 181 insertions(+), 31 deletions(-)

diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteResultCountAdviceTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteResultCountAdviceTest.java
index b71539407cd..f0b2c2bc616 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteResultCountAdviceTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteResultCountAdviceTest.java
@@ -37,10 +37,10 @@ import static org.mockito.Mockito.mock;
 public final class RouteResultCountAdviceTest {
     
     private final MetricConfiguration routedDataSourcesConfig = new 
MetricConfiguration("routed_data_sources_total",
-            MetricCollectorType.COUNTER, "Total count of data source routed", 
Collections.singletonList("name"), Collections.emptyMap());
+            MetricCollectorType.COUNTER, null, 
Collections.singletonList("name"), Collections.emptyMap());
     
     private final MetricConfiguration routedTablesConfig = new 
MetricConfiguration("routed_tables_total",
-            MetricCollectorType.COUNTER, "Total count of table routed", 
Collections.singletonList("name"), Collections.emptyMap());
+            MetricCollectorType.COUNTER, null, 
Collections.singletonList("name"), Collections.emptyMap());
     
     @After
     public void reset() {
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java
index a4c5f720484..a70e92930c7 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java
@@ -47,8 +47,7 @@ import static org.mockito.Mockito.mock;
 
 public final class SQLParseCountAdviceTest {
     
-    private final MetricConfiguration config = new 
MetricConfiguration("parsed_sql_total",
-            MetricCollectorType.COUNTER, "Total count of parsed SQL", 
Collections.singletonList("type"), Collections.emptyMap());
+    private final MetricConfiguration config = new 
MetricConfiguration("parsed_sql_total", MetricCollectorType.COUNTER, null, 
Collections.singletonList("type"), Collections.emptyMap());
     
     @After
     public void reset() {
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdviceTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdviceTest.java
index 9e6ac7142f9..d2b2a501eba 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdviceTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdviceTest.java
@@ -40,8 +40,7 @@ import static org.mockito.Mockito.mock;
 
 public final class SQLRouteCountAdviceTest {
     
-    private final MetricConfiguration config = new 
MetricConfiguration("routed_sql_total",
-            MetricCollectorType.COUNTER, "Total count of routed SQL", 
Collections.singletonList("type"), Collections.emptyMap());
+    private final MetricConfiguration config = new 
MetricConfiguration("routed_sql_total", MetricCollectorType.COUNTER, null, 
Collections.singletonList("type"), Collections.emptyMap());
     
     private final SQLRouteCountAdvice advice = new SQLRouteCountAdvice();
     
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/CommitTransactionsCountAdviceTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/CommitTransactionsCountAdviceTest.java
index 34a795a8d4a..ab192596a7e 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/CommitTransactionsCountAdviceTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/CommitTransactionsCountAdviceTest.java
@@ -34,8 +34,7 @@ import static org.mockito.Mockito.mock;
 
 public final class CommitTransactionsCountAdviceTest {
     
-    private final MetricConfiguration config = new 
MetricConfiguration("proxy_commit_transactions_total",
-            MetricCollectorType.COUNTER, "Total commit transactions of 
ShardingSphere-Proxy", Collections.emptyList(), Collections.emptyMap());
+    private final MetricConfiguration config = new 
MetricConfiguration("proxy_commit_transactions_total", 
MetricCollectorType.COUNTER, null, Collections.emptyList(), 
Collections.emptyMap());
     
     private final CommitTransactionsCountAdvice advice = new 
CommitTransactionsCountAdvice();
     
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/CurrentConnectionsCountAdviceTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/CurrentConnectionsCountAdviceTest.java
index 6256e8c88da..ef1cfe56cb8 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/CurrentConnectionsCountAdviceTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/CurrentConnectionsCountAdviceTest.java
@@ -35,8 +35,7 @@ import static org.mockito.Mockito.when;
 
 public final class CurrentConnectionsCountAdviceTest {
     
-    private final MetricConfiguration config = new 
MetricConfiguration("proxy_current_connections",
-            MetricCollectorType.GAUGE, "Current connections of 
ShardingSphere-Proxy", Collections.emptyList(), Collections.emptyMap());
+    private final MetricConfiguration config = new 
MetricConfiguration("proxy_current_connections", MetricCollectorType.GAUGE, 
null, Collections.emptyList(), Collections.emptyMap());
     
     private final CurrentConnectionsCountAdvice advice = new 
CurrentConnectionsCountAdvice();
     
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteErrorsCountAdviceTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteErrorsCountAdviceTest.java
index 352224310da..f55a044f175 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteErrorsCountAdviceTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteErrorsCountAdviceTest.java
@@ -34,8 +34,7 @@ import static org.mockito.Mockito.mock;
 
 public final class ExecuteErrorsCountAdviceTest {
     
-    private final MetricConfiguration config = new 
MetricConfiguration("proxy_execute_errors_total",
-            MetricCollectorType.COUNTER, "Total execute errors of 
ShardingSphere-Proxy", Collections.emptyList(), Collections.emptyMap());
+    private final MetricConfiguration config = new 
MetricConfiguration("proxy_execute_errors_total", MetricCollectorType.COUNTER, 
null, Collections.emptyList(), Collections.emptyMap());
     
     @After
     public void reset() {
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteLatencyHistogramAdviceTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteLatencyHistogramAdviceTest.java
index 001f5a5f94d..63e1974d877 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteLatencyHistogramAdviceTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteLatencyHistogramAdviceTest.java
@@ -34,8 +34,7 @@ import static org.mockito.Mockito.mock;
 
 public final class ExecuteLatencyHistogramAdviceTest {
     
-    private final MetricConfiguration config = new 
MetricConfiguration("proxy_execute_latency_millis",
-            MetricCollectorType.HISTOGRAM, "Execute latency millis histogram 
of ShardingSphere-Proxy", Collections.emptyList(), Collections.emptyMap());
+    private final MetricConfiguration config = new 
MetricConfiguration("proxy_execute_latency_millis", 
MetricCollectorType.HISTOGRAM, null, Collections.emptyList(), 
Collections.emptyMap());
     
     @After
     public void reset() {
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/RequestsCountAdviceTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/RequestsCountAdviceTest.java
index ecc1eb0aad0..55c93f7a58b 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/RequestsCountAdviceTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/RequestsCountAdviceTest.java
@@ -34,8 +34,7 @@ import static org.mockito.Mockito.mock;
 
 public final class RequestsCountAdviceTest {
     
-    private final MetricConfiguration config = new 
MetricConfiguration("proxy_requests_total",
-            MetricCollectorType.COUNTER, "Total requests of 
ShardingSphere-Proxy", Collections.emptyList(), Collections.emptyMap());
+    private final MetricConfiguration config = new 
MetricConfiguration("proxy_requests_total", MetricCollectorType.COUNTER, null, 
Collections.emptyList(), Collections.emptyMap());
     
     private final RequestsCountAdvice advice = new RequestsCountAdvice();
     
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/RollbackTransactionsCountAdviceTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/RollbackTransactionsCountAdviceTest.java
index c02162a9d65..51e228dcae3 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/RollbackTransactionsCountAdviceTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/RollbackTransactionsCountAdviceTest.java
@@ -34,8 +34,7 @@ import static org.mockito.Mockito.mock;
 
 public final class RollbackTransactionsCountAdviceTest {
     
-    private final MetricConfiguration config = new 
MetricConfiguration("proxy_rollback_transactions_total",
-            MetricCollectorType.COUNTER, "Total rollback transactions of 
ShardingSphere-Proxy", Collections.emptyList(), Collections.emptyMap());
+    private final MetricConfiguration config = new 
MetricConfiguration("proxy_rollback_transactions_total", 
MetricCollectorType.COUNTER, null, Collections.emptyList(), 
Collections.emptyMap());
     
     private final RollbackTransactionsCountAdvice advice = new 
RollbackTransactionsCountAdvice();
     
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteErrorsCountAdviceTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/JDKBuildInfoExporterTest.java
similarity index 62%
copy from 
agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteErrorsCountAdviceTest.java
copy to 
agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/JDKBuildInfoExporterTest.java
index 352224310da..205371dd7c2 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteErrorsCountAdviceTest.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/JDKBuildInfoExporterTest.java
@@ -15,37 +15,36 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.agent.plugin.metrics.core.advice.proxy;
+package org.apache.shardingsphere.agent.plugin.metrics.core.exporter.impl;
 
-import 
org.apache.shardingsphere.agent.plugin.metrics.core.advice.MockTargetAdviceObject;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeMetricFamilyMetricsCollector;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.fixture.MetricsCollectorFixture;
 import org.junit.After;
 import org.junit.Test;
 
-import java.lang.reflect.Method;
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
+import static org.junit.Assert.assertTrue;
 
-public final class ExecuteErrorsCountAdviceTest {
-    
-    private final MetricConfiguration config = new 
MetricConfiguration("proxy_execute_errors_total",
-            MetricCollectorType.COUNTER, "Total execute errors of 
ShardingSphere-Proxy", Collections.emptyList(), Collections.emptyMap());
+public final class JDKBuildInfoExporterTest {
     
     @After
     public void reset() {
+        MetricConfiguration config = new MetricConfiguration("jdk_build_info", 
MetricCollectorType.GAUGE_METRIC_FAMILY, null, Arrays.asList("version", 
"name"), Collections.emptyMap());
         ((MetricsCollectorFixture) MetricsCollectorRegistry.get(config, 
"FIXTURE")).reset();
     }
     
     @Test
-    public void assertCountExecuteErrors() {
-        MockTargetAdviceObject targetObject = new MockTargetAdviceObject();
-        new ExecuteErrorsCountAdvice().afterMethod(targetObject, 
mock(Method.class), new Object[]{}, null, "FIXTURE");
-        assertThat(((MetricsCollectorFixture) 
MetricsCollectorRegistry.get(config, "FIXTURE")).getValue(), is(1d));
+    public void assertExport() {
+        Optional<GaugeMetricFamilyMetricsCollector> collector = new 
JDKBuildInfoExporter().export("FIXTURE");
+        assertTrue(collector.isPresent());
+        assertThat(((MetricsCollectorFixture) collector.get()).getValue(), 
is(2d));
     }
 }
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java
new file mode 100644
index 00000000000..999349ca2d9
--- /dev/null
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.shardingsphere.agent.plugin.metrics.core.exporter.impl.proxy;
+
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeMetricFamilyMetricsCollector;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.fixture.MetricsCollectorFixture;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.junit.After;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class ProxyMetaDataInfoExporterTest {
+    
+    private final ContextManager originalContextManager = 
ProxyContext.getInstance().getContextManager();
+    
+    @After
+    public void reset() {
+        MetricConfiguration config = new 
MetricConfiguration("proxy_meta_data_info", 
MetricCollectorType.GAUGE_METRIC_FAMILY, null, 
Collections.singletonList("name"), Collections.emptyMap());
+        ((MetricsCollectorFixture) MetricsCollectorRegistry.get(config, 
"FIXTURE")).reset();
+        ProxyContext.init(originalContextManager);
+    }
+    
+    @Test
+    public void assertExportWithoutContextManager() {
+        ProxyContext.init(null);
+        assertFalse(new 
ProxyMetaDataInfoExporter().export("FIXTURE").isPresent());
+    }
+    
+    @Test
+    public void assertExportWithContextManager() {
+        ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
+        
when(contextManager.getMetaDataContexts().getMetaData().getDatabases()).thenReturn(Collections.emptyMap());
+        // TODO mock schema_count and database_count
+        ProxyContext.init(contextManager);
+        Optional<GaugeMetricFamilyMetricsCollector> collector = new 
ProxyMetaDataInfoExporter().export("FIXTURE");
+        assertTrue(collector.isPresent());
+        assertThat(((MetricsCollectorFixture) collector.get()).getValue(), 
is(0d));
+    }
+}
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java
new file mode 100644
index 00000000000..18e72c00a74
--- /dev/null
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyStateExporterTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.shardingsphere.agent.plugin.metrics.core.exporter.impl.proxy;
+
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeMetricFamilyMetricsCollector;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.fixture.MetricsCollectorFixture;
+import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
+import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
+import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
+import org.apache.shardingsphere.infra.lock.LockContext;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import 
org.apache.shardingsphere.mode.manager.standalone.workerid.generator.StandaloneWorkerIdGenerator;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.junit.After;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public final class ProxyStateExporterTest {
+    
+    private final ContextManager originalContextManager = 
ProxyContext.getInstance().getContextManager();
+    
+    @After
+    public void reset() {
+        MetricConfiguration config = new MetricConfiguration("proxy_state", 
MetricCollectorType.GAUGE_METRIC_FAMILY, null, Collections.emptyList(), 
Collections.emptyMap());
+        ((MetricsCollectorFixture) MetricsCollectorRegistry.get(config, 
"FIXTURE")).reset();
+        ProxyContext.init(originalContextManager);
+    }
+    
+    @Test
+    public void assertExportWithoutContextManager() {
+        ProxyContext.init(null);
+        assertFalse(new ProxyStateExporter().export("FIXTURE").isPresent());
+    }
+    
+    @Test
+    public void assertExportWithContextManager() {
+        MetaDataContexts metaDataContexts = new 
MetaDataContexts(mock(MetaDataPersistService.class), new 
ShardingSphereMetaData());
+        InstanceContext instanceContext = new InstanceContext(
+                new ComputeNodeInstance(mock(InstanceMetaData.class)), new 
StandaloneWorkerIdGenerator(), new ModeConfiguration("Standalone", null),
+                mock(ModeContextManager.class), mock(LockContext.class), new 
EventBusContext());
+        ProxyContext.init(new ContextManager(metaDataContexts, 
instanceContext));
+        Optional<GaugeMetricFamilyMetricsCollector> collector = new 
ProxyStateExporter().export("FIXTURE");
+        assertTrue(collector.isPresent());
+        assertThat(((MetricsCollectorFixture) collector.get()).getValue(), 
is(0d));
+    }
+}
diff --git 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/fixture/MetricsCollectorFixture.java
 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/fixture/MetricsCollectorFixture.java
index 5405f42ff88..b0a5438b3bc 100644
--- 
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/fixture/MetricsCollectorFixture.java
+++ 
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/fixture/MetricsCollectorFixture.java
@@ -19,12 +19,15 @@ package 
org.apache.shardingsphere.agent.plugin.metrics.core.fixture;
 
 import lombok.Getter;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.CounterMetricsCollector;
+import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeMetricFamilyMetricsCollector;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeMetricsCollector;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.HistogramMetricsCollector;
 import 
org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.SummaryMetricsCollector;
 
+import java.util.List;
+
 @Getter
-public final class MetricsCollectorFixture implements CounterMetricsCollector, 
GaugeMetricsCollector, HistogramMetricsCollector, SummaryMetricsCollector {
+public final class MetricsCollectorFixture implements CounterMetricsCollector, 
GaugeMetricsCollector, HistogramMetricsCollector, SummaryMetricsCollector, 
GaugeMetricFamilyMetricsCollector {
     
     private double value;
     
@@ -53,6 +56,16 @@ public final class MetricsCollectorFixture implements 
CounterMetricsCollector, G
         this.value = value;
     }
     
+    @Override
+    public void addMetric(final List<String> labelValues, final double value) {
+        this.value += value;
+    }
+    
+    @Override
+    public Object getRawMetricFamilyObject() {
+        return null;
+    }
+    
     /**
      * Reset.
      */

Reply via email to