HADOOP-13439. Fix race between TestMetricsSystemImpl and TestGangliaMetrics. Contributed by Chen Liang.

(cherry picked from commit 8f9b61852b)
This commit is contained in:
Masatake Iwasaki 2016-08-09 09:54:44 +09:00
parent 93b768d0d1
commit 427b54086e
1 changed files with 27 additions and 19 deletions

View File

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