diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/TestGangliaMetrics.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/TestGangliaMetrics.java index daf64bd9e49..7e094edc097 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/TestGangliaMetrics.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/TestGangliaMetrics.java @@ -50,23 +50,28 @@ import org.junit.Test; public class TestGangliaMetrics { public static final Log LOG = LogFactory.getLog(TestMetricsSystemImpl.class); - private final String[] expectedMetrics = - { "test.s1rec.C1", - "test.s1rec.G1", - "test.s1rec.Xxx", - "test.s1rec.Yyy", - "test.s1rec.S1NumOps", - "test.s1rec.S1AvgTime" }; + // This is the prefix to locate the config file for this particular test + // This is to avoid using the same config file with other test cases, + // which can cause race conditions. + private String testNamePrefix = "gangliametrics"; + private final String[] expectedMetrics = { + testNamePrefix + ".s1rec.C1", + testNamePrefix + ".s1rec.G1", + testNamePrefix + ".s1rec.Xxx", + testNamePrefix + ".s1rec.Yyy", + testNamePrefix + ".s1rec.S1NumOps", + testNamePrefix + ".s1rec.S1AvgTime" + }; @Test public void testTagsForPrefix() throws Exception { ConfigBuilder cb = new ConfigBuilder() - .add("test.sink.ganglia.tagsForPrefix.all", "*") - .add("test.sink.ganglia.tagsForPrefix.some", "NumActiveSinks, " + - "NumActiveSources") - .add("test.sink.ganglia.tagsForPrefix.none", ""); + .add(testNamePrefix + ".sink.ganglia.tagsForPrefix.all", "*") + .add(testNamePrefix + ".sink.ganglia.tagsForPrefix.some", + "NumActiveSinks, " + "NumActiveSources") + .add(testNamePrefix + ".sink.ganglia.tagsForPrefix.none", ""); GangliaSink30 sink = new GangliaSink30(); - sink.init(cb.subset("test.sink.ganglia")); + sink.init(cb.subset(testNamePrefix + ".sink.ganglia")); List tags = new ArrayList(); tags.add(new MetricsTag(MsInfo.Context, "all")); @@ -101,11 +106,14 @@ public class TestGangliaMetrics { // Setting long interval to avoid periodic publishing. // We manually publish metrics by MeticsSystem#publishMetricsNow here. ConfigBuilder cb = new ConfigBuilder().add("*.period", 120) - .add("test.sink.gsink30.context", "test") // filter out only "test" - .add("test.sink.gsink31.context", "test") // filter out only "test" - .save(TestMetricsConfig.getTestFilename("hadoop-metrics2-test")); + .add(testNamePrefix + + ".sink.gsink30.context", testNamePrefix) // filter out only "test" + .add(testNamePrefix + + ".sink.gsink31.context", testNamePrefix) // filter out only "test" + .save(TestMetricsConfig.getTestFilename("hadoop-metrics2-" + + testNamePrefix)); - MetricsSystemImpl ms = new MetricsSystemImpl("Test"); + MetricsSystemImpl ms = new MetricsSystemImpl(testNamePrefix); ms.start(); TestSource s1 = ms.register("s1", "s1 desc", new TestSource("s1rec")); s1.c1.incr(); @@ -119,13 +127,13 @@ public class TestGangliaMetrics { // Setup test for GangliaSink30 AbstractGangliaSink gsink30 = new GangliaSink30(); - gsink30.init(cb.subset("test")); + gsink30.init(cb.subset(testNamePrefix)); MockDatagramSocket mockds30 = new MockDatagramSocket(); GangliaMetricsTestHelper.setDatagramSocket(gsink30, mockds30); // Setup test for GangliaSink31 AbstractGangliaSink gsink31 = new GangliaSink31(); - gsink31.init(cb.subset("test")); + gsink31.init(cb.subset(testNamePrefix)); MockDatagramSocket mockds31 = new MockDatagramSocket(); GangliaMetricsTestHelper.setDatagramSocket(gsink31, mockds31); @@ -168,7 +176,7 @@ public class TestGangliaMetrics { } @SuppressWarnings("unused") - @Metrics(context="test") + @Metrics(context="gangliametrics") private static class TestSource { @Metric("C1 desc") MutableCounterLong c1; @Metric("XXX desc") MutableCounterLong xxx;