commit 396cb36f30c4a6688b3013827725b9dcf3b7f99e Author: Andrew Sherman Date: Wed Oct 11 17:22:39 2017 -0700 HIVE-17789: Fix timing problems in TestSessionManagerMetrics.testAbandonedSessionMetrics The test is waiting for a worker thread to be timed out. The time after which the timeout should happen in 3000 ms. The test waits for 3200 ms, and sometimes this is not enough as the timeout happens asynchronously. Use a counter-limited loop to wait for the counter to change; this is quicker than just sleeping for a long time. Change-Id: I62e1ef1e885425a8ea2d332d17e88a1fd7242579 diff --git service/src/test/org/apache/hive/service/cli/session/TestSessionManagerMetrics.java service/src/test/org/apache/hive/service/cli/session/TestSessionManagerMetrics.java index 5f418c70cb4c75426520e2996ef5b3cb2b621576..646159f1e4ff44da5d97f6b90af063d441ee47f9 100644 --- service/src/test/org/apache/hive/service/cli/session/TestSessionManagerMetrics.java +++ service/src/test/org/apache/hive/service/cli/session/TestSessionManagerMetrics.java @@ -25,6 +25,8 @@ import java.util.concurrent.CyclicBarrier; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; + +import com.fasterxml.jackson.databind.JsonNode; import org.apache.hadoop.hive.common.metrics.MetricsTestUtils; import org.apache.hadoop.hive.common.metrics.common.MetricsConstant; import org.apache.hadoop.hive.common.metrics.common.MetricsFactory; @@ -32,7 +34,6 @@ import org.apache.hadoop.hive.common.metrics.metrics2.MetricsReporting; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.ql.metadata.Hive; -import org.apache.hadoop.util.Time; import org.apache.hive.service.cli.FetchOrientation; import org.apache.hive.service.cli.HiveSQLException; import org.apache.hive.service.cli.OperationHandle; @@ -373,9 +374,20 @@ public void testAbandonedSessionMetrics() throws Exception { sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap()); - Thread.sleep(3200); - - json = metrics.dumpJson(); - MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.COUNTER, MetricsConstant.HS2_ABANDONED_SESSIONS, 1); + // We're going to wait for the session to be abandoned. + String currentValue; + int count = 5; // how many times we'll sleep before giving up + String expectedValue = "1"; + do { + // HIVE_SERVER2_SESSION_CHECK_INTERVAL is set to 3 seconds, so we have to wait for at least + // that long to see an abandoned session + Thread.sleep(3200); + json = metrics.dumpJson(); + currentValue = MetricsTestUtils + .getJsonNode(json, MetricsTestUtils.COUNTER, MetricsConstant.HS2_ABANDONED_SESSIONS) + .asText(); + // loop until the value is correct or we run out of tries + } while (!expectedValue.equals(currentValue) && --count > 0); + Assert.assertEquals(expectedValue, currentValue); } }