tinhto-000 commented on a change in pull request #26955: [SPARK-30310] [Core] 
Resolve missing match case in SparkUncaughtExceptionHandler and added tests
URL: https://github.com/apache/spark/pull/26955#discussion_r360627627
 
 

 ##########
 File path: 
core/src/test/scala/org/apache/spark/util/SparkUncaughtExceptionHandlerSuite.scala
 ##########
 @@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.util
+
+import java.io.File
+
+import org.apache.spark.SparkFunSuite
+
+class SparkUncaughtExceptionSuite extends SparkFunSuite {
+
+  private val sparkHome =
+    sys.props.getOrElse("spark.test.home", fail("spark.test.home is not set!"))
+
+  // creates a spark-class process that invokes the exception thrower
+  // the testcases will detect the process's exit code
+  def getThrowerProcess(exceptionThrower: Any, exitOnUncaughtException: 
Boolean): Process = {
+    Utils.executeCommand(
+      Seq(s"$sparkHome/bin/spark-class",
+        exceptionThrower.getClass.getCanonicalName.dropRight(1), // drops the 
"$" at the end
+        if (exitOnUncaughtException) "true" else "false"),
+      new File(sparkHome),
+      Map("SPARK_TESTING" -> "1", "SPARK_HOME" -> sparkHome))
+  }
+
+  test("SPARK-30310: Test uncaught RuntimeException, exitOnUncaughtException = 
true") {
+    val process = getThrowerProcess(RuntimeExceptionThrower, 
exitOnUncaughtException = true)
+    assert(process.waitFor == SparkExitCode.UNCAUGHT_EXCEPTION)
+  }
+
+  test("SPARK-30310: Test uncaught RuntimeException, exitOnUncaughtException = 
false") {
+    val process = getThrowerProcess(RuntimeExceptionThrower, 
exitOnUncaughtException = false)
+    assert(process.waitFor == 0)
+  }
+
+  test("SPARK-30310: Test uncaught OutOfMemoryError, exitOnUncaughtException = 
true") {
+    val process = getThrowerProcess(OutOfMemoryErrorThrower, 
exitOnUncaughtException = true)
+    assert(process.waitFor == SparkExitCode.OOM)
+  }
+
+  test("SPARK-30310: Test uncaught OutOfMemoryError, exitOnUncaughtException = 
false") {
+    val process = getThrowerProcess(OutOfMemoryErrorThrower, 
exitOnUncaughtException = false)
+    assert(process.waitFor == SparkExitCode.OOM)
+  }
+
+  test("SPARK-30310: Test uncaught SparkFatalException, 
exitOnUncaughtException = true") {
+    val process = getThrowerProcess(SparkFatalExceptionThrower, 
exitOnUncaughtException = true)
+    assert(process.waitFor == SparkExitCode.UNCAUGHT_EXCEPTION)
+  }
+
+  test("SPARK-30310: Test uncaught SparkFatalException, 
exitOnUncaughtException = false") {
+    val process = getThrowerProcess(SparkFatalExceptionThrower, 
exitOnUncaughtException = false)
+    assert(process.waitFor == 0)
+  }
+
+  test("SPARK-30310: Test uncaught SparkFatalException (OOM), 
exitOnUncaughtException = true") {
+    val process = getThrowerProcess(SparkFatalExceptionWithOOMThrower,
+      exitOnUncaughtException = true)
+    assert(process.waitFor == SparkExitCode.OOM)
+  }
+
+  test("SPARK-30310: Test uncaught SparkFatalException (OOM), 
exitOnUncaughtException = false") {
+    val process = getThrowerProcess(SparkFatalExceptionWithOOMThrower,
+      exitOnUncaughtException = false)
+    assert(process.waitFor == SparkExitCode.OOM)
+  }
+
+}
+
+// a thread that uses SparkUncaughtExceptionHandler, then throws the throwable
+class ThrowableThrowerThread(t: Throwable,
+    exitOnUncaughtException: Boolean) extends Thread {
+  override def run() {
+    Thread.setDefaultUncaughtExceptionHandler(
+      new SparkUncaughtExceptionHandler(exitOnUncaughtException))
+    throw t
+  }
+}
+
+// Objects to be invoked by spark-class for different Throwable types
+// that SparkUncaughtExceptionHandler handles.  spark-class will exit with
+// exit code dictated by either SparkUncaughtExceptionHandler (SparkExitCode)
+// or main() (0)
+
+object RuntimeExceptionThrower {
+  def main(args: Array[String]): Unit = {
+    val t = new ThrowableThrowerThread(new RuntimeException, if (args(0) == 
"true") true else false)
+    t.start()
+    t.join()
+    System.exit(0)
+  }
+}
+
+object OutOfMemoryErrorThrower {
+  def main(args: Array[String]): Unit = {
+    val t = new ThrowableThrowerThread(new OutOfMemoryError, if (args(0) == 
"true") true else false)
 
 Review comment:
   thanks for the suggestion!  modded the throwers to check args.length and 
exit with -1 if it's empty now.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to