diff --git common/src/test/org/apache/hadoop/hive/common/metrics/MetricsTestUtils.java common/src/test/org/apache/hadoop/hive/common/metrics/MetricsTestUtils.java index c90a614..4aaa808 100644 --- common/src/test/org/apache/hadoop/hive/common/metrics/MetricsTestUtils.java +++ common/src/test/org/apache/hadoop/hive/common/metrics/MetricsTestUtils.java @@ -57,4 +57,13 @@ private static JsonNode getJsonNode(String json, MetricsCategory category, Strin JsonNode metricsNode = categoryNode.path(metricsName); return metricsNode.path(category.metricsHandle); } + + public static byte[] getFileData(String path, int timeoutInterval, int tries) throws Exception { + File file = new File(path); + do { + Thread.sleep(timeoutInterval); + tries--; + } while (tries > 0 && !file.exists()); + return Files.readAllBytes(Paths.get(path)); + } } diff --git common/src/test/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleMetrics.java common/src/test/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleMetrics.java index 55bb2c2..6ee6245 100644 --- common/src/test/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleMetrics.java +++ common/src/test/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleMetrics.java @@ -23,6 +23,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.hadoop.fs.CommonConfigurationKeysPublic; +import org.apache.hadoop.hive.common.metrics.MetricsTestUtils; import org.apache.hadoop.hive.common.metrics.common.MetricsFactory; import org.apache.hadoop.hive.common.metrics.common.MetricsVariable; import org.apache.hadoop.hive.conf.HiveConf; @@ -122,11 +123,9 @@ public void testFileReporting() throws Exception { int runs = 5; for (int i = 0; i < runs; i++) { MetricsFactory.getInstance().incrementCounter("count2"); - Thread.sleep(100); } - Thread.sleep(2000); - byte[] jsonData = Files.readAllBytes(Paths.get(jsonReportFile.getAbsolutePath())); + byte[] jsonData = MetricsTestUtils.getFileData(jsonReportFile.getAbsolutePath(), 2000, 3); ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(jsonData); @@ -154,25 +153,12 @@ public void testGauge() throws Exception { testVar.setValue(20); MetricsFactory.getInstance().addGauge("gauge1", testVar); - Thread.sleep(2000); - byte[] jsonData = Files.readAllBytes(Paths.get(jsonReportFile.getAbsolutePath())); - ObjectMapper objectMapper = new ObjectMapper(); + String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); + MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "gauge1", testVar.getValue()); - JsonNode rootNode = objectMapper.readTree(jsonData); - JsonNode gaugesNode = rootNode.path("gauges"); - JsonNode methodGaugeNode = gaugesNode.path("gauge1"); - JsonNode countNode = methodGaugeNode.path("value"); - Assert.assertEquals(countNode.asInt(), testVar.getValue()); testVar.setValue(40); - Thread.sleep(2000); - - jsonData = Files.readAllBytes(Paths.get(jsonReportFile.getAbsolutePath())); - - rootNode = objectMapper.readTree(jsonData); - gaugesNode = rootNode.path("gauges"); - methodGaugeNode = gaugesNode.path("gauge1"); - countNode = methodGaugeNode.path("value"); - Assert.assertEquals(countNode.asInt(), testVar.getValue()); + json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); + MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "gauge1", testVar.getValue()); } }