mridulm commented on code in PR #52373:
URL: https://github.com/apache/spark/pull/52373#discussion_r2594612702


##########
core/src/test/scala/org/apache/spark/util/UninterruptibleThreadSuite.scala:
##########
@@ -28,50 +28,69 @@ import org.apache.spark.SparkFunSuite
 
 class UninterruptibleThreadSuite extends SparkFunSuite {
 
-  /** Sleep millis and return true if it's interrupted */
+  /* Sleep millis and return true if it's interrupted */
   private def sleep(millis: Long): Boolean = {
     try {
       Thread.sleep(millis)
       false
     } catch {
-      case _: InterruptedException =>
+      case e: InterruptedException =>
+        log.error("Thread interrupted during sleep", e)
+        true
+    }
+  }
+
+  /* Await latch and return true if it's interrupted */
+  private def await(
+      latch: CountDownLatch,
+      timeout: Long = 10,
+      timeUnit: TimeUnit = TimeUnit.SECONDS): Boolean = {
+    try {
+      if (!latch.await(timeout, timeUnit)) {
+        log.error("timeout while waiting for the latch")
+        fail("timeout while waiting for the latch")
+      }
+      false
+    } catch {
+      case e: InterruptedException =>
+        log.error("Thread interrupted during await", e)
         true
     }
   }
 
   test("interrupt when runUninterruptibly is running") {
     val enterRunUninterruptibly = new CountDownLatch(1)
+    val interruptLatch = new CountDownLatch(1)
     @volatile var hasInterruptedException = false
     @volatile var interruptStatusBeforeExit = false
-    val t = new UninterruptibleThread("test") {
+    val t = new UninterruptibleThread("runUninterruptibly") {
       override def run(): Unit = {
         runUninterruptibly {
           enterRunUninterruptibly.countDown()
-          hasInterruptedException = sleep(1000)
+          hasInterruptedException = await(interruptLatch)
         }
         interruptStatusBeforeExit = Thread.interrupted()
       }
     }
     t.start()
-    assert(enterRunUninterruptibly.await(10, TimeUnit.SECONDS), "await 
timeout")
+    assert(!await(enterRunUninterruptibly), "await interrupted")
     t.interrupt()
+    interruptLatch.countDown()

Review Comment:
   Discuss:
   Introducing this `countDown` can, in theory, mean the `InterruptedException` 
need not be thrown - as it can race within `CountDownLatch` : there is no 
precedence expected between interrupt vs countDown .
   
   I would suggest dropping the `countDown()` - it is not required for the test 
anyway.



##########
core/src/test/scala/org/apache/spark/util/UninterruptibleThreadSuite.scala:
##########
@@ -80,130 +99,128 @@ class UninterruptibleThreadSuite extends SparkFunSuite {
     t.interrupt()
     interruptLatch.countDown()
     t.join()
-    assert(hasInterruptedException === false)
-    assert(interruptStatusBeforeExit)
+    assert(!hasInterruptedException, "runUninterruptibly should not be 
interrupted")
+    assert(interruptStatusBeforeExit, "interrupt flag should be set")
   }
 
   test("nested runUninterruptibly") {
     val enterRunUninterruptibly = new CountDownLatch(1)
     val interruptLatch = new CountDownLatch(1)
     @volatile var hasInterruptedException = false
     @volatile var interruptStatusBeforeExit = false
-    val t = new UninterruptibleThread("test") {
+    val t = new UninterruptibleThread("runUninterruptibly") {
       override def run(): Unit = {
         runUninterruptibly {
           enterRunUninterruptibly.countDown()
-          Uninterruptibles.awaitUninterruptibly(interruptLatch, 10, 
TimeUnit.SECONDS)
-          hasInterruptedException = sleep(1)
-          runUninterruptibly {
-            if (sleep(1)) {
-              hasInterruptedException = true
+          hasInterruptedException = await(interruptLatch)
+          if (!hasInterruptedException) {
+            runUninterruptibly {
+              hasInterruptedException = sleep(0)
             }
-          }
-          if (sleep(1)) {
-            hasInterruptedException = true
+            hasInterruptedException |= sleep(0)
           }
         }
         interruptStatusBeforeExit = Thread.interrupted()
       }
     }
     t.start()
-    assert(enterRunUninterruptibly.await(10, TimeUnit.SECONDS), "await 
timeout")
+    assert(!await(enterRunUninterruptibly), "await interrupted")
     t.interrupt()
     interruptLatch.countDown()
+    assert(!sleep(0), "sleep should not be interrupted")
+    t.interrupt()
+    assert(!sleep(0), "sleep should not be interrupted")
+    t.interrupt()
     t.join()
-    assert(hasInterruptedException === false)
-    assert(interruptStatusBeforeExit)
+    assert(!hasInterruptedException, "runUninterruptibly should not be 
interrupted")
+    assert(interruptStatusBeforeExit, "interrupt flag should be set")
   }
 
   test("no runUninterruptibly") {
     @volatile var hasInterruptedException = false
-    val t = new UninterruptibleThread("test") {
+    @volatile var interruptStatusBeforeExit = false
+    val t = new UninterruptibleThread("run") {
       override def run(): Unit = {
-        if (sleep(0)) {
-          hasInterruptedException = true
-        }
+        hasInterruptedException = sleep(0)
+        interruptStatusBeforeExit = Thread.interrupted()
       }
     }
     t.interrupt()

Review Comment:
   Discuss:
   Behavior of interrupting a thread, which has not yet started, is not defined 
.
   While the diff here is not changing existing test behavior - the existing 
test itself appears to be assuming impl details.



##########
core/src/test/scala/org/apache/spark/util/UninterruptibleThreadSuite.scala:
##########
@@ -28,50 +28,67 @@ import org.apache.spark.SparkFunSuite
 
 class UninterruptibleThreadSuite extends SparkFunSuite {
 
-  /** Sleep millis and return true if it's interrupted */
+  /* Sleep millis and return true if it's interrupted */
   private def sleep(millis: Long): Boolean = {
     try {
       Thread.sleep(millis)
       false
     } catch {
-      case _: InterruptedException =>
+      case e: InterruptedException =>
+        log.error("Thread interrupted during sleep", e)
+        true
+    }
+  }
+
+  /* Await latch and return true if it's interrupted */
+  private def await(latch: CountDownLatch, timeout: Long = 10,
+                    timeUnit: TimeUnit = TimeUnit.SECONDS): Boolean = {
+    try {
+      if (!latch.await(timeout, timeUnit)) {
+        log.error("timeout while waiting for the latch")

Review Comment:
   I understand the rationale, but this formulation is a bit confusing - `fail` 
gives the impression of test failure.
   
   I would suggest passing `failOnTimeout` as a method parameter.
   When we timeout here, 
   * call `fail` if it is `failOnTimeout == true`.
   * else throw `TimeoutException`



##########
core/src/test/scala/org/apache/spark/util/UninterruptibleThreadSuite.scala:
##########
@@ -80,130 +99,128 @@ class UninterruptibleThreadSuite extends SparkFunSuite {
     t.interrupt()
     interruptLatch.countDown()
     t.join()
-    assert(hasInterruptedException === false)
-    assert(interruptStatusBeforeExit)
+    assert(!hasInterruptedException, "runUninterruptibly should not be 
interrupted")
+    assert(interruptStatusBeforeExit, "interrupt flag should be set")
   }
 
   test("nested runUninterruptibly") {
     val enterRunUninterruptibly = new CountDownLatch(1)
     val interruptLatch = new CountDownLatch(1)
     @volatile var hasInterruptedException = false
     @volatile var interruptStatusBeforeExit = false
-    val t = new UninterruptibleThread("test") {
+    val t = new UninterruptibleThread("runUninterruptibly") {
       override def run(): Unit = {
         runUninterruptibly {
           enterRunUninterruptibly.countDown()
-          Uninterruptibles.awaitUninterruptibly(interruptLatch, 10, 
TimeUnit.SECONDS)
-          hasInterruptedException = sleep(1)
-          runUninterruptibly {
-            if (sleep(1)) {
-              hasInterruptedException = true
+          hasInterruptedException = await(interruptLatch)
+          if (!hasInterruptedException) {
+            runUninterruptibly {
+              hasInterruptedException = sleep(0)
             }
-          }
-          if (sleep(1)) {
-            hasInterruptedException = true
+            hasInterruptedException |= sleep(0)
           }
         }
         interruptStatusBeforeExit = Thread.interrupted()
       }
     }
     t.start()
-    assert(enterRunUninterruptibly.await(10, TimeUnit.SECONDS), "await 
timeout")
+    assert(!await(enterRunUninterruptibly), "await interrupted")
     t.interrupt()
     interruptLatch.countDown()
+    assert(!sleep(0), "sleep should not be interrupted")
+    t.interrupt()
+    assert(!sleep(0), "sleep should not be interrupted")
+    t.interrupt()

Review Comment:
   Why are we checking `sleep` here ?



##########
core/src/test/scala/org/apache/spark/util/UninterruptibleThreadSuite.scala:
##########
@@ -28,50 +28,69 @@ import org.apache.spark.SparkFunSuite
 
 class UninterruptibleThreadSuite extends SparkFunSuite {
 
-  /** Sleep millis and return true if it's interrupted */
+  /* Sleep millis and return true if it's interrupted */
   private def sleep(millis: Long): Boolean = {
     try {
       Thread.sleep(millis)
       false
     } catch {
-      case _: InterruptedException =>
+      case e: InterruptedException =>
+        log.error("Thread interrupted during sleep", e)
+        true
+    }
+  }
+
+  /* Await latch and return true if it's interrupted */
+  private def await(
+      latch: CountDownLatch,
+      timeout: Long = 10,
+      timeUnit: TimeUnit = TimeUnit.SECONDS): Boolean = {
+    try {
+      if (!latch.await(timeout, timeUnit)) {
+        log.error("timeout while waiting for the latch")
+        fail("timeout while waiting for the latch")
+      }
+      false
+    } catch {
+      case e: InterruptedException =>
+        log.error("Thread interrupted during await", e)
         true
     }
   }
 
   test("interrupt when runUninterruptibly is running") {
     val enterRunUninterruptibly = new CountDownLatch(1)
+    val interruptLatch = new CountDownLatch(1)
     @volatile var hasInterruptedException = false
     @volatile var interruptStatusBeforeExit = false
-    val t = new UninterruptibleThread("test") {
+    val t = new UninterruptibleThread("runUninterruptibly") {
       override def run(): Unit = {
         runUninterruptibly {
           enterRunUninterruptibly.countDown()
-          hasInterruptedException = sleep(1000)
+          hasInterruptedException = await(interruptLatch)

Review Comment:
   Was it flakey which requires a bump in timeout ?



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