From 7c2921b6729c5f18fabc332e4a2761cc6054b02f Mon Sep 17 00:00:00 2001 From: Raymond Zhao <35050708+rayz@users.noreply.github.com> Date: Thu, 2 Nov 2023 10:19:11 -0400 Subject: [PATCH] Clean up instance telemetry bean in tests (#485) --- src/main/java/org/datadog/jmxfetch/App.java | 4 ++++ src/test/java/org/datadog/jmxfetch/TestApp.java | 6 +++--- src/test/java/org/datadog/jmxfetch/TestCommon.java | 10 ++++++++++ src/test/resources/jmx_alias_match.yaml | 2 +- src/test/resources/jmx_counter_rate.yaml | 4 ++-- src/test/resources/jmx_sd_pipe.txt | 2 +- src/test/resources/jmx_sd_pipe_longname.txt | 4 ++-- 7 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index 3869e11a7..6d2d2e16b 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -244,6 +244,10 @@ public TaskStatusHandler invoke( } } + protected void clearAllInstances() { + this.clearInstances(this.instances); + } + /** * Builds an {@link ExecutorService} of the specified fixed size. Threads will be created * and executed as daemons if {@link AppConfig#isDaemon()} is true. Defaults to false. diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index 972b07f9e..607329ee4 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -152,7 +152,7 @@ public void testRegexpAliasing() throws Exception { List tags = Arrays.asList( "jmx_domain:org.datadog.jmxfetch.test", - "instance:jmx_test_instance", + "instance:jmx_test_instance1", "foo:Bar", "qux:Baz"); @@ -963,7 +963,7 @@ public void testServiceDiscovery() throws Exception { List tags = Arrays.asList( "type:SimpleTestJavaApp", "scope:CoolScope", - "instance:jmx_test_instance", + "instance:jmx_test_instance2", "jmx_domain:org.datadog.jmxfetch.test", "bean_host:localhost", "component" @@ -998,7 +998,7 @@ public void testServiceDiscovery() throws Exception { tags = Arrays.asList( "jmx_domain:org.datadog.jmxfetch.test", - "instance:jmx_test_instance", + "instance:jmx_test_instance1", "foo:Bar", "qux:Baz"); diff --git a/src/test/java/org/datadog/jmxfetch/TestCommon.java b/src/test/java/org/datadog/jmxfetch/TestCommon.java index e5cadcfac..cac345270 100644 --- a/src/test/java/org/datadog/jmxfetch/TestCommon.java +++ b/src/test/java/org/datadog/jmxfetch/TestCommon.java @@ -110,6 +110,16 @@ public void unregisterMBean() throws MBeanRegistrationException, InstanceNotFoun } } + /** + * Clear instances and their instance telemetry bean after execution of every test. + */ + @After + public void clearInstances() { + if (app != null) { + app.clearAllInstances(); + } + } + /** Init JMXFetch with the given YAML configuration file. */ protected void initApplication(String yamlFileName, String autoDiscoveryPipeFile) throws FileNotFoundException, IOException { diff --git a/src/test/resources/jmx_alias_match.yaml b/src/test/resources/jmx_alias_match.yaml index 3d5d51193..8626e89ed 100644 --- a/src/test/resources/jmx_alias_match.yaml +++ b/src/test/resources/jmx_alias_match.yaml @@ -2,7 +2,7 @@ init_config: instances: - process_name_regex: .*surefire.* - name: jmx_test_instance + name: jmx_test_instance1 conf: - include: domain: org.datadog.jmxfetch.test diff --git a/src/test/resources/jmx_counter_rate.yaml b/src/test/resources/jmx_counter_rate.yaml index ab58e7aad..4485994ee 100644 --- a/src/test/resources/jmx_counter_rate.yaml +++ b/src/test/resources/jmx_counter_rate.yaml @@ -3,7 +3,7 @@ init_config: instances: - process_name_regex: .*surefire.* refresh_beans: 4 - name: jmx_test_instance + name: jmx_test_instance1 conf: - include: domain: org.datadog.jmxfetch.test @@ -13,7 +13,7 @@ instances: alias: test.counter - process_name_regex: .*surefire.* refresh_beans: 4 - name: jmx_test_instance + name: jmx_test_instance2 conf: - include: domain: org.datadog.jmxfetch.test diff --git a/src/test/resources/jmx_sd_pipe.txt b/src/test/resources/jmx_sd_pipe.txt index be9ef7688..945fbc4c0 100644 --- a/src/test/resources/jmx_sd_pipe.txt +++ b/src/test/resources/jmx_sd_pipe.txt @@ -25,7 +25,7 @@ init_config: instances: - process_name_regex: .*surefire.* - name: jmx_test_instance + name: jmx_test_instance2 conf: - include: bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=Co|olScope,host=localhost,component= diff --git a/src/test/resources/jmx_sd_pipe_longname.txt b/src/test/resources/jmx_sd_pipe_longname.txt index d1f58c2ee..8e6380525 100644 --- a/src/test/resources/jmx_sd_pipe_longname.txt +++ b/src/test/resources/jmx_sd_pipe_longname.txt @@ -4,7 +4,7 @@ init_config: instances: - process_name_regex: .*surefire.* - name: jmx_test_instance + name: jmx_test_instance2 conf: - include: bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=Co|olScope,host=localhost,component= @@ -25,7 +25,7 @@ init_config: instances: - process_name_regex: .*surefire.* - name: jmx_test_instance + name: jmx_test_instance3 conf: - include: bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=Co|olScope,host=localhost,component=