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 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<MetricsTag> tags = new ArrayList<MetricsTag>();
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;