-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-5735] Replace uses of EasyMock with Mockito #4578
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc5e94d
Remove EasyMock in CacheManagerSuite
JoshRosen 7cca486
Remove EasyMock usage in MesosSchedulerBackendSuite
JoshRosen fae1d8f
Remove EasyMock usage in KinesisReceiverSuite.
JoshRosen 977565b
Remove EasyMock from build.
JoshRosen 0ab192b
Import sorting plus two minor changes to more closely match old seman…
JoshRosen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,45 +17,48 @@ | |
|
||
package org.apache.spark.scheduler.mesos | ||
|
||
import org.apache.spark.executor.MesosExecutorBackend | ||
import org.scalatest.FunSuite | ||
import org.apache.spark.{SparkConf, SparkContext, LocalSparkContext} | ||
import org.apache.spark.scheduler.{SparkListenerExecutorAdded, LiveListenerBus, | ||
TaskDescription, WorkerOffer, TaskSchedulerImpl} | ||
import org.apache.spark.scheduler.cluster.ExecutorInfo | ||
import org.apache.spark.scheduler.cluster.mesos.{MemoryUtils, MesosSchedulerBackend} | ||
import org.apache.mesos.SchedulerDriver | ||
import org.apache.mesos.Protos.{ExecutorInfo => MesosExecutorInfo, _} | ||
import org.apache.mesos.Protos.Value.Scalar | ||
import org.easymock.{Capture, EasyMock} | ||
import java.nio.ByteBuffer | ||
import java.util.Collections | ||
import java.util | ||
import org.scalatest.mock.EasyMockSugar | ||
|
||
import scala.collection.mutable | ||
import scala.collection.mutable.ArrayBuffer | ||
|
||
class MesosSchedulerBackendSuite extends FunSuite with LocalSparkContext with EasyMockSugar { | ||
import org.apache.mesos.SchedulerDriver | ||
import org.apache.mesos.Protos._ | ||
import org.apache.mesos.Protos.Value.Scalar | ||
import org.scalatest.FunSuite | ||
import org.scalatest.mock.MockitoSugar | ||
import org.mockito.Mockito._ | ||
import org.mockito.Matchers._ | ||
import org.mockito.{Matchers, ArgumentCaptor} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you reorganize this? |
||
|
||
import org.apache.spark.{SparkConf, SparkContext, LocalSparkContext} | ||
import org.apache.spark.executor.MesosExecutorBackend | ||
import org.apache.spark.scheduler.{SparkListenerExecutorAdded, LiveListenerBus, TaskDescription, | ||
WorkerOffer, TaskSchedulerImpl} | ||
import org.apache.spark.scheduler.cluster.ExecutorInfo | ||
import org.apache.spark.scheduler.cluster.mesos.{MemoryUtils, MesosSchedulerBackend} | ||
|
||
class MesosSchedulerBackendSuite extends FunSuite with LocalSparkContext with MockitoSugar { | ||
|
||
test("check spark-class location correctly") { | ||
val conf = new SparkConf | ||
conf.set("spark.mesos.executor.home" , "/mesos-home") | ||
|
||
val listenerBus = EasyMock.createMock(classOf[LiveListenerBus]) | ||
listenerBus.post(SparkListenerExecutorAdded(EasyMock.anyLong, "s1", new ExecutorInfo("host1", 2, Map.empty))) | ||
EasyMock.replay(listenerBus) | ||
|
||
val sc = EasyMock.createMock(classOf[SparkContext]) | ||
EasyMock.expect(sc.getSparkHome()).andReturn(Option("/spark-home")).anyTimes() | ||
EasyMock.expect(sc.conf).andReturn(conf).anyTimes() | ||
EasyMock.expect(sc.executorEnvs).andReturn(new mutable.HashMap).anyTimes() | ||
EasyMock.expect(sc.executorMemory).andReturn(100).anyTimes() | ||
EasyMock.expect(sc.listenerBus).andReturn(listenerBus) | ||
EasyMock.replay(sc) | ||
val taskScheduler = EasyMock.createMock(classOf[TaskSchedulerImpl]) | ||
EasyMock.expect(taskScheduler.CPUS_PER_TASK).andReturn(2).anyTimes() | ||
EasyMock.replay(taskScheduler) | ||
val listenerBus = mock[LiveListenerBus] | ||
listenerBus.post( | ||
SparkListenerExecutorAdded(anyLong, "s1", new ExecutorInfo("host1", 2, Map.empty))) | ||
|
||
val sc = mock[SparkContext] | ||
when(sc.getSparkHome()).thenReturn(Option("/spark-home")) | ||
|
||
when(sc.conf).thenReturn(conf) | ||
when(sc.executorEnvs).thenReturn(new mutable.HashMap[String, String]) | ||
when(sc.executorMemory).thenReturn(100) | ||
when(sc.listenerBus).thenReturn(listenerBus) | ||
val taskScheduler = mock[TaskSchedulerImpl] | ||
when(taskScheduler.CPUS_PER_TASK).thenReturn(2) | ||
|
||
val mesosSchedulerBackend = new MesosSchedulerBackend(taskScheduler, sc, "master") | ||
|
||
|
@@ -84,20 +87,19 @@ class MesosSchedulerBackendSuite extends FunSuite with LocalSparkContext with Ea | |
.setSlaveId(SlaveID.newBuilder().setValue(s"s${id.toString}")).setHostname(s"host${id.toString}").build() | ||
} | ||
|
||
val driver = EasyMock.createMock(classOf[SchedulerDriver]) | ||
val taskScheduler = EasyMock.createMock(classOf[TaskSchedulerImpl]) | ||
val driver = mock[SchedulerDriver] | ||
val taskScheduler = mock[TaskSchedulerImpl] | ||
|
||
val listenerBus = EasyMock.createMock(classOf[LiveListenerBus]) | ||
listenerBus.post(SparkListenerExecutorAdded(EasyMock.anyLong, "s1", new ExecutorInfo("host1", 2, Map.empty))) | ||
EasyMock.replay(listenerBus) | ||
val listenerBus = mock[LiveListenerBus] | ||
listenerBus.post( | ||
SparkListenerExecutorAdded(anyLong, "s1", new ExecutorInfo("host1", 2, Map.empty))) | ||
|
||
val sc = EasyMock.createMock(classOf[SparkContext]) | ||
EasyMock.expect(sc.executorMemory).andReturn(100).anyTimes() | ||
EasyMock.expect(sc.getSparkHome()).andReturn(Option("/path")).anyTimes() | ||
EasyMock.expect(sc.executorEnvs).andReturn(new mutable.HashMap).anyTimes() | ||
EasyMock.expect(sc.conf).andReturn(new SparkConf).anyTimes() | ||
EasyMock.expect(sc.listenerBus).andReturn(listenerBus) | ||
EasyMock.replay(sc) | ||
val sc = mock[SparkContext] | ||
when(sc.executorMemory).thenReturn(100) | ||
when(sc.getSparkHome()).thenReturn(Option("/path")) | ||
when(sc.executorEnvs).thenReturn(new mutable.HashMap[String, String]) | ||
when(sc.conf).thenReturn(new SparkConf) | ||
when(sc.listenerBus).thenReturn(listenerBus) | ||
|
||
val minMem = MemoryUtils.calculateTotalMemory(sc).toInt | ||
val minCpu = 4 | ||
|
@@ -121,25 +123,29 @@ class MesosSchedulerBackendSuite extends FunSuite with LocalSparkContext with Ea | |
2 | ||
)) | ||
val taskDesc = new TaskDescription(1L, 0, "s1", "n1", 0, ByteBuffer.wrap(new Array[Byte](0))) | ||
EasyMock.expect(taskScheduler.resourceOffers(EasyMock.eq(expectedWorkerOffers))).andReturn(Seq(Seq(taskDesc))) | ||
EasyMock.expect(taskScheduler.CPUS_PER_TASK).andReturn(2).anyTimes() | ||
EasyMock.replay(taskScheduler) | ||
when(taskScheduler.resourceOffers(expectedWorkerOffers)).thenReturn(Seq(Seq(taskDesc))) | ||
when(taskScheduler.CPUS_PER_TASK).thenReturn(2) | ||
|
||
val capture = new Capture[util.Collection[TaskInfo]] | ||
EasyMock.expect( | ||
val capture = ArgumentCaptor.forClass(classOf[util.Collection[TaskInfo]]) | ||
when( | ||
driver.launchTasks( | ||
EasyMock.eq(Collections.singleton(mesosOffers.get(0).getId)), | ||
EasyMock.capture(capture), | ||
EasyMock.anyObject(classOf[Filters]) | ||
Matchers.eq(Collections.singleton(mesosOffers.get(0).getId)), | ||
capture.capture(), | ||
any(classOf[Filters]) | ||
) | ||
).andReturn(Status.valueOf(1)).once | ||
EasyMock.expect(driver.declineOffer(mesosOffers.get(1).getId)).andReturn(Status.valueOf(1)).times(1) | ||
EasyMock.expect(driver.declineOffer(mesosOffers.get(2).getId)).andReturn(Status.valueOf(1)).times(1) | ||
EasyMock.replay(driver) | ||
).thenReturn(Status.valueOf(1)) | ||
when(driver.declineOffer(mesosOffers.get(1).getId)).thenReturn(Status.valueOf(1)) | ||
when(driver.declineOffer(mesosOffers.get(2).getId)).thenReturn(Status.valueOf(1)) | ||
|
||
backend.resourceOffers(driver, mesosOffers) | ||
|
||
EasyMock.verify(driver) | ||
verify(driver, times(1)).launchTasks( | ||
Matchers.eq(Collections.singleton(mesosOffers.get(0).getId)), | ||
capture.capture(), | ||
any(classOf[Filters]) | ||
) | ||
verify(driver, times(1)).declineOffer(mesosOffers.get(1).getId) | ||
verify(driver, times(1)).declineOffer(mesosOffers.get(2).getId) | ||
assert(capture.getValue.size() == 1) | ||
val taskInfo = capture.getValue.iterator().next() | ||
assert(taskInfo.getName.equals("n1")) | ||
|
@@ -151,15 +157,13 @@ class MesosSchedulerBackendSuite extends FunSuite with LocalSparkContext with Ea | |
// Unwanted resources offered on an existing node. Make sure they are declined | ||
val mesosOffers2 = new java.util.ArrayList[Offer] | ||
mesosOffers2.add(createOffer(1, minMem, minCpu)) | ||
EasyMock.reset(taskScheduler) | ||
EasyMock.reset(driver) | ||
EasyMock.expect(taskScheduler.resourceOffers(EasyMock.anyObject(classOf[Seq[WorkerOffer]])).andReturn(Seq(Seq()))) | ||
EasyMock.expect(taskScheduler.CPUS_PER_TASK).andReturn(2).anyTimes() | ||
EasyMock.replay(taskScheduler) | ||
EasyMock.expect(driver.declineOffer(mesosOffers2.get(0).getId)).andReturn(Status.valueOf(1)).times(1) | ||
EasyMock.replay(driver) | ||
reset(taskScheduler) | ||
reset(driver) | ||
when(taskScheduler.resourceOffers(any(classOf[Seq[WorkerOffer]]))).thenReturn(Seq(Seq())) | ||
when(taskScheduler.CPUS_PER_TASK).thenReturn(2) | ||
when(driver.declineOffer(mesosOffers2.get(0).getId)).thenReturn(Status.valueOf(1)) | ||
|
||
backend.resourceOffers(driver, mesosOffers2) | ||
EasyMock.verify(driver) | ||
verify(driver, times(1)).declineOffer(mesosOffers2.get(0).getId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: sorting