Github user zsxwing commented on a diff in the pull request:

    https://github.com/apache/spark/pull/6205#discussion_r33797178
  
    --- Diff: core/src/test/scala/org/apache/spark/rpc/RpcEnvSuite.scala ---
    @@ -539,6 +544,97 @@ abstract class RpcEnvSuite extends SparkFunSuite with 
BeforeAndAfterAll {
         }
       }
     
    +  test("construct RpcTimeout with conf property") {
    +    val conf = new SparkConf
    +
    +    val testProp = "spark.ask.test.timeout"
    +    val testDurationSeconds = 30
    +    val secondaryProp = "spark.ask.secondary.timeout"
    +
    +    conf.set(testProp, s"${testDurationSeconds}s")
    +    conf.set(secondaryProp, "100s")
    +
    +    // Construct RpcTimeout with a single property
    +    val rt1 = RpcTimeout(conf, testProp)
    +    assert( testDurationSeconds === rt1.duration.toSeconds )
    +
    +    // Construct RpcTimeout with prioritized list of properties
    +    val rt2 = RpcTimeout(conf, Seq("spark.ask.invalid.timeout", testProp, 
secondaryProp), "1s")
    +    assert( testDurationSeconds === rt2.duration.toSeconds )
    +
    +    // Construct RpcTimeout with default value,
    +    val defaultProp = "spark.ask.default.timeout"
    +    val defaultDurationSeconds = 1
    +    val rt3 = RpcTimeout(conf, Seq(defaultProp), 
defaultDurationSeconds.toString + "s")
    +    assert( defaultDurationSeconds === rt3.duration.toSeconds )
    +    assert( rt3.timeoutProp.contains(defaultProp) )
    +
    +    // Try to construct RpcTimeout with an unconfigured property
    +    intercept[NoSuchElementException] {
    +      RpcTimeout(conf, "spark.ask.invalid.timeout")
    +    }
    +  }
    +
    +  test("ask a message timeout on Future using RpcTimeout") {
    +    case class SleepyReply(msg: String)
    +
    +    val rpcEndpointRef = env.setupEndpoint("ask-future", new RpcEndpoint {
    +      override val rpcEnv = env
    +
    +      override def receiveAndReply(context: RpcCallContext): 
PartialFunction[Any, Unit] = {
    +        case msg: String => {
    +          context.reply(msg)
    +        }
    +        case sr: SleepyReply => {
    +          Thread.sleep(50)
    --- End diff --
    
    `Thread.sleep(50)` may cause this test becomes a flaky one. How about:
    ```
        val sleepLatch = new CountDownLatch(1)
    
        val rpcEndpointRef = env.setupEndpoint("ask-future", new RpcEndpoint {
          override val rpcEnv = env
    
          override def receiveAndReply(context: RpcCallContext): 
PartialFunction[Any, Unit] = {
            case msg: String => {
              context.reply(msg)
            }
            case sr: SleepyReply => {
              sleepLatch.await()
              context.reply(sr.msg)
            }
          }
        })
    
        val longTimeout = new RpcTimeout(1 second, "spark.rpc.long.timeout")
        val shortTimeout = new RpcTimeout(10 millis, "spark.rpc.short.timeout")
    
        // Ask with immediate response, should complete successfully
        val fut1 = rpcEndpointRef.ask[String]("hello", longTimeout)
        val reply1 = longTimeout.awaitResult(fut1)
        assert("hello" === reply1)
    
        // Ask with a delayed response and wait for response immediately that 
should timeout
        val fut2 = rpcEndpointRef.ask[String](SleepyReply("doh"), shortTimeout)
        val reply2 =
          intercept[RpcTimeoutException] {
            shortTimeout.awaitResult(fut2)
          }.getMessage
    
        // RpcTimeout.awaitResult should have added the property to the 
TimeoutException message
        assert(reply2.contains(shortTimeout.timeoutProp))
    
        // Ask with delayed response and allow the Future to timeout before 
Await.result
        val fut3 = rpcEndpointRef.ask[String](SleepyReply("goodbye"), 
shortTimeout)
    
        // Allow future to complete with failure using plain Await.result, this 
will return
        // once the future is complete to verify addMessageIfTimeout was invoked
        val reply3 =
          intercept[RpcTimeoutException] {
            Await.result(fut3, 200 millis)
          }.getMessage
    
        sleepLatch.countDown()
    ```
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to