diff --git a/sql/hive-thriftserver/src/test/scala/org/apache/spark/sql/hive/thriftserver/HiveThriftServer2Suites.scala b/sql/hive-thriftserver/src/test/scala/org/apache/spark/sql/hive/thriftserver/HiveThriftServer2Suites.scala index 765844524a..979818979e 100644 --- a/sql/hive-thriftserver/src/test/scala/org/apache/spark/sql/hive/thriftserver/HiveThriftServer2Suites.scala +++ b/sql/hive-thriftserver/src/test/scala/org/apache/spark/sql/hive/thriftserver/HiveThriftServer2Suites.scala @@ -50,7 +50,7 @@ import org.apache.spark.sql.hive.test.HiveTestJars import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.internal.StaticSQLConf.HIVE_THRIFT_SERVER_SINGLESESSION import org.apache.spark.sql.test.ProcessTestUtils.ProcessOutputCapturer -import org.apache.spark.util.{ThreadUtils, Utils} +import org.apache.spark.util.{ShutdownHookManager, ThreadUtils, Utils} object TestData { def getTestDataFilePath(name: String): URL = { @@ -1338,33 +1338,36 @@ abstract class HiveThriftServer2TestBase extends SparkFunSuite with BeforeAndAft process } + ShutdownHookManager.addShutdownHook(stopThriftServer _) ThreadUtils.awaitResult(serverStarted.future, SERVER_STARTUP_TIMEOUT) } private def stopThriftServer(): Unit = { - // The `spark-daemon.sh' script uses kill, which is not synchronous, have to wait for a while. - Utils.executeAndGetOutput( - command = Seq(stopScript), - extraEnvironment = Map("SPARK_PID_DIR" -> pidDir.getCanonicalPath)) - Thread.sleep(3.seconds.toMillis) + if (pidDir.list.nonEmpty) { + // The `spark-daemon.sh' script uses kill, which is not synchronous, have to wait for a while. + Utils.executeAndGetOutput( + command = Seq(stopScript), + extraEnvironment = Map("SPARK_PID_DIR" -> pidDir.getCanonicalPath)) + Thread.sleep(3.seconds.toMillis) - warehousePath.delete() - warehousePath = null + warehousePath.delete() + warehousePath = null - metastorePath.delete() - metastorePath = null + metastorePath.delete() + metastorePath = null - operationLogPath.delete() - operationLogPath = null + operationLogPath.delete() + operationLogPath = null - lScratchDir.delete() - lScratchDir = null + lScratchDir.delete() + lScratchDir = null - Option(logPath).foreach(_.delete()) - logPath = null + Option(logPath).foreach(_.delete()) + logPath = null - Option(logTailingProcess).foreach(_.destroy()) - logTailingProcess = null + Option(logTailingProcess).foreach(_.destroy()) + logTailingProcess = null + } } private def dumpLogs(): Unit = {