@@ -20,6 +20,8 @@ package org.apache.spark
2020import java .util .concurrent .{ExecutorService , TimeUnit }
2121
2222import scala .collection .mutable
23+ import scala .concurrent .Await
24+ import scala .concurrent .duration ._
2325import scala .language .postfixOps
2426
2527import org .scalatest .{BeforeAndAfterEach , PrivateMethodTester }
@@ -96,8 +98,8 @@ class HeartbeatReceiverSuite
9698
9799 test(" normal heartbeat" ) {
98100 heartbeatReceiverRef.askWithRetry[Boolean ](TaskSchedulerIsSet )
99- heartbeatReceiver.onExecutorAdded( SparkListenerExecutorAdded ( 0 , executorId1, null ) )
100- heartbeatReceiver.onExecutorAdded( SparkListenerExecutorAdded ( 0 , executorId2, null ) )
101+ addExecutorAndVerify( executorId1)
102+ addExecutorAndVerify( executorId2)
101103 triggerHeartbeat(executorId1, executorShouldReregister = false )
102104 triggerHeartbeat(executorId2, executorShouldReregister = false )
103105 val trackedExecutors = heartbeatReceiver.invokePrivate(_executorLastSeen())
@@ -107,7 +109,7 @@ class HeartbeatReceiverSuite
107109 }
108110
109111 test(" reregister if scheduler is not ready yet" ) {
110- heartbeatReceiver.onExecutorAdded( SparkListenerExecutorAdded ( 0 , executorId1, null ) )
112+ addExecutorAndVerify( executorId1)
111113 // Task scheduler is not set yet in HeartbeatReceiver, so executors should reregister
112114 triggerHeartbeat(executorId1, executorShouldReregister = true )
113115 }
@@ -121,10 +123,10 @@ class HeartbeatReceiverSuite
121123
122124 test(" reregister if heartbeat from removed executor" ) {
123125 heartbeatReceiverRef.askWithRetry[Boolean ](TaskSchedulerIsSet )
124- heartbeatReceiver.onExecutorAdded( SparkListenerExecutorAdded ( 0 , executorId1, null ) )
125- heartbeatReceiver.onExecutorAdded( SparkListenerExecutorAdded ( 0 , executorId2, null ) )
126+ addExecutorAndVerify( executorId1)
127+ addExecutorAndVerify( executorId2)
126128 // Remove the second executor but not the first
127- heartbeatReceiver.onExecutorRemoved( SparkListenerExecutorRemoved ( 0 , executorId2, " bad boy " ) )
129+ removeExecutorAndVerify( executorId2)
128130 // Now trigger the heartbeats
129131 // A heartbeat from the second executor should require reregistering
130132 triggerHeartbeat(executorId1, executorShouldReregister = false )
@@ -138,8 +140,8 @@ class HeartbeatReceiverSuite
138140 test(" expire dead hosts" ) {
139141 val executorTimeout = heartbeatReceiver.invokePrivate(_executorTimeoutMs())
140142 heartbeatReceiverRef.askWithRetry[Boolean ](TaskSchedulerIsSet )
141- heartbeatReceiver.onExecutorAdded( SparkListenerExecutorAdded ( 0 , executorId1, null ) )
142- heartbeatReceiver.onExecutorAdded( SparkListenerExecutorAdded ( 0 , executorId2, null ) )
143+ addExecutorAndVerify( executorId1)
144+ addExecutorAndVerify( executorId2)
143145 triggerHeartbeat(executorId1, executorShouldReregister = false )
144146 triggerHeartbeat(executorId2, executorShouldReregister = false )
145147 // Advance the clock and only trigger a heartbeat for the first executor
@@ -175,8 +177,8 @@ class HeartbeatReceiverSuite
175177 fakeSchedulerBackend.driverEndpoint.askWithRetry[RegisteredExecutor .type ](
176178 RegisterExecutor (executorId2, dummyExecutorEndpointRef2, " dummy:4040" , 0 , Map .empty))
177179 heartbeatReceiverRef.askWithRetry[Boolean ](TaskSchedulerIsSet )
178- heartbeatReceiver.onExecutorAdded( SparkListenerExecutorAdded ( 0 , executorId1, null ) )
179- heartbeatReceiver.onExecutorAdded( SparkListenerExecutorAdded ( 0 , executorId2, null ) )
180+ addExecutorAndVerify( executorId1)
181+ addExecutorAndVerify( executorId2)
180182 triggerHeartbeat(executorId1, executorShouldReregister = false )
181183 triggerHeartbeat(executorId2, executorShouldReregister = false )
182184
@@ -222,6 +224,20 @@ class HeartbeatReceiverSuite
222224 }
223225 }
224226
227+ private def addExecutorAndVerify (executorId : String ): Unit = {
228+ assert(
229+ heartbeatReceiver.addExecutor(executorId).map { f =>
230+ Await .result(f, 10 .seconds)
231+ } === Some (true ))
232+ }
233+
234+ private def removeExecutorAndVerify (executorId : String ): Unit = {
235+ assert(
236+ heartbeatReceiver.removeExecutor(executorId).map { f =>
237+ Await .result(f, 10 .seconds)
238+ } === Some (true ))
239+ }
240+
225241}
226242
227243// TODO: use these classes to add end-to-end tests for dynamic allocation!
0 commit comments