Last active
December 21, 2015 22:29
-
-
Save bantonsson/6375951 to your computer and use it in GitHub Desktop.
Multi Node testing and sbt-0.13
This file contains hidden or 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
Multi Node testing and sbt-0.13 |
This file contains hidden or 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
import sbt._ | |
import Keys._ | |
import com.typesafe.sbt.SbtMultiJvm | |
import com.typesafe.sbt.SbtMultiJvm.MultiJvmKeys.{ MultiJvm } | |
object ExampleBuild extends Build { | |
lazy val buildSettings = Defaults.defaultSettings ++ multiJvmSettings ++ Seq( | |
organization := "example", | |
version := "1.0", | |
scalaVersion := "2.10.2", | |
// make sure that the artifacts don't have the scala version in the name | |
crossPaths := false | |
) | |
lazy val example = Project( | |
id = "example", | |
base = file("."), | |
settings = buildSettings ++ | |
Seq(libraryDependencies ++= Dependencies.example) | |
) configs(MultiJvm) | |
lazy val multiJvmSettings = SbtMultiJvm.multiJvmSettings ++ Seq( | |
// make sure that MultiJvm test are compiled by the default test compilation | |
compile in MultiJvm <<= (compile in MultiJvm) triggeredBy (compile in Test), | |
// disable parallel tests | |
parallelExecution in Test := false, | |
// make sure that MultiJvm tests are executed by the default test target | |
executeTests in Test <<= | |
((executeTests in Test), (executeTests in MultiJvm)) map { | |
case ((testResults), (multiJvmResults)) => | |
val overall = | |
if (testResults.overall.id < multiJvmResults.overall.id) | |
multiJvmResults.overall | |
else | |
testResults.overall | |
Tests.Output(overall, | |
testResults.events ++ multiJvmResults.events, | |
testResults.summaries ++ multiJvmResults.summaries) | |
} | |
) | |
} | |
object Dependencies { | |
val example = Seq( | |
// ---- application dependencies ---- | |
"com.typesafe.akka" %% "akka-actor" % "2.2.1" , | |
"com.typesafe.akka" %% "akka-remote" % "2.2.1" , | |
// ---- test dependencies ---- | |
"com.typesafe.akka" %% "akka-testkit" % "2.2.1" % | |
"test" , | |
"com.typesafe.akka" %% "akka-multi-node-testkit" % "2.2.1" % | |
"test" , | |
"org.scalatest" %% "scalatest" % "1.9.1" % "test", | |
"junit" % "junit" % "4.10" % "test" | |
) | |
} |
This file contains hidden or 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
package example | |
import org.scalatest.{ BeforeAndAfterAll, WordSpec } | |
import org.scalatest.matchers.MustMatchers | |
import akka.remote.testkit.MultiNodeConfig | |
import akka.remote.testkit.MultiNodeSpec | |
import akka.testkit.ImplicitSender | |
import akka.actor.{ActorSelection, Props, Actor} | |
object MultiNodeExampleConfig extends MultiNodeConfig { | |
val node1 = role("node1") | |
val node2 = role("node2") | |
class Ponger extends Actor { | |
def receive = { | |
case "ping" => sender ! "pong" | |
} | |
} | |
} | |
class MultiNodeExampleSpecMultiJvmNode1 extends MultiNodeExample | |
class MultiNodeExampleSpecMultiJvmNode2 extends MultiNodeExample | |
class MultiNodeExample extends MultiNodeSpec(MultiNodeExampleConfig) | |
with WordSpec with MustMatchers with BeforeAndAfterAll with ImplicitSender { | |
import MultiNodeExampleConfig._ | |
def initialParticipants = roles.size | |
"A MultiNodeExample" must { | |
"wait for all nodes to enter a barrier" in { | |
enterBarrier("startup") | |
} | |
"send to and receive from a remote node" in { | |
runOn(node1) { | |
// wait for the other node to have deployed an actor | |
enterBarrier("deployed") | |
val ponger = system.actorSelection(node(node2) / "user" / "ponger") | |
ponger ! "ping" | |
expectMsg("pong") | |
} | |
runOn(node2) { | |
// deploy the ponger actor | |
system.actorOf(Props[Ponger], "ponger") | |
enterBarrier("deployed") | |
} | |
// wait for all nodes to finish the test | |
enterBarrier("finished") | |
} | |
} | |
// hook the MultiNodeSpec into ScalaTest | |
override def beforeAll() = multiNodeSpecBeforeAll() | |
override def afterAll() = multiNodeSpecAfterAll() | |
} |
This file contains hidden or 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
addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.3.8") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment