Yicong-Huang commented on code in PR #57247:
URL: https://github.com/apache/spark/pull/57247#discussion_r3581707481


##########
core/src/test/scala/org/apache/spark/api/python/PythonWorkerHandleSuite.scala:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.api.python
+
+import java.util.concurrent.TimeUnit
+
+import org.apache.spark.SparkFunSuite
+import org.apache.spark.util.Utils
+
+class PythonWorkerHandleSuite extends SparkFunSuite {
+
+  test("terminationDiagnostics reads the injected log only when faulthandler 
is enabled") {
+    // The enabled flag is a per-session policy the caller supplies at read 
time; the gate lives in
+    // terminationDiagnostics, so the injected reader is invoked only when 
enabled and its result
+    // is returned verbatim. When disabled the reader must not be called at 
all.
+    var reads = 0
+    val faultHandlerLog: () => Option[String] = () => {
+      reads += 1
+      Some("py-traceback")
+    }
+    val handle = new LocalPythonWorkerHandle(ProcessHandle.current(), 
faultHandlerLog)
+
+    assert(handle.terminationDiagnostics(faultHandlerEnabled = 
true).contains("py-traceback"))
+    assert(reads == 1)
+
+    assert(handle.terminationDiagnostics(faultHandlerEnabled = false).isEmpty)
+    assert(reads == 1) // gate short-circuited; reader not invoked again
+  }
+
+  test("terminationDiagnostics defaults to empty when no log reader is 
injected") {
+    val handle = new LocalPythonWorkerHandle(ProcessHandle.current())
+    assert(handle.terminationDiagnostics(faultHandlerEnabled = true).isEmpty)
+    assert(handle.terminationDiagnostics(faultHandlerEnabled = false).isEmpty)
+  }
+
+  test("of binds the pid into the injected reader and returns a handle for a 
live pid") {
+    var seenPid: Option[Long] = None
+    val reader: Long => Option[String] = { pid =>
+      seenPid = Some(pid)
+      Some(s"log-for-$pid")
+    }
+    val livePid = ProcessHandle.current().pid()
+    val handle = PythonWorkerHandle.of(livePid, reader)
+    assert(handle.isDefined)
+    assert(handle.get.isAlive())
+
+    assert(handle.get.terminationDiagnostics(faultHandlerEnabled = true)
+      .contains(s"log-for-$livePid"))
+    assert(seenPid.contains(livePid))
+  }
+
+  test("isAlive and destroy delegate to the underlying process") {
+    assume(!Utils.isWindows)
+    val proc = new ProcessBuilder("sleep", "30").start()
+    try {
+      val handle = new LocalPythonWorkerHandle(proc.toHandle)
+      assert(handle.isAlive())
+      assert(handle.destroy()) // kill issued
+      assert(proc.waitFor(10, TimeUnit.SECONDS)) // process actually reaped
+      assert(!handle.isAlive())
+    } finally {
+      proc.destroyForcibly()
+    }
+  }
+
+  test("of returns None for a pid with no live process") {
+    assume(!Utils.isWindows)
+    val proc = new ProcessBuilder("sleep", "30").start()
+    val pid = proc.pid()
+    proc.destroyForcibly()
+    assert(proc.waitFor(10, TimeUnit.SECONDS))
+    assert(PythonWorkerHandle.of(pid).isEmpty)

Review Comment:
   Minor flake risk: the OS may reuse `pid` after the process is reaped, making 
`of(pid)` return a live handle. Idiomatic in Spark suites, but worth a note; 
alternatively assert on a handle taken before `destroyForcibly`.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to