Skip to content

Instantly share code, notes, and snippets.

@bantonsson
Last active October 12, 2015 10:58
Show Gist options
  • Save bantonsson/4016493 to your computer and use it in GitHub Desktop.
Save bantonsson/4016493 to your computer and use it in GitHub Desktop.
Spotlight for Multi Node Testing
Spotlight for Multi Node Testing
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.0-RC2"
)
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 results = testResults ++ multiJvmResults
(Tests.overall(results.values), results)
}
)
object Dependencies {
val example = Seq(
// ---- application dependencies ----
"com.typesafe.akka" %% "akka-actor" % "2.1.0-RC2" cross CrossVersion.full,
"com.typesafe.akka" %% "akka-remote" % "2.1.0-RC2" cross CrossVersion.full,
// ---- test dependencies ----
"com.typesafe.akka" %% "akka-testkit" % "2.1.0-RC2" %
"test" cross CrossVersion.full,
"com.typesafe.akka" %% "akka-remote-tests-experimental" % "2.1.0-RC2" %
"test" cross CrossVersion.full,
"org.scalatest" %% "scalatest" % "1.8-B2" % "test" cross CrossVersion.full
)
}
}
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.{Props, Actor}
object MultiNodeExampleConfig extends MultiNodeConfig {
val node1 = role("node1")
val node2 = role("node2")
}
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.actorFor(node(node2) / "user" / "ponger")
ponger ! "ping"
expectMsg("pong")
}
runOn(node2) {
// deploy the ponger actor
system.actorOf(Props(new Actor {
def receive = {
case "ping" => sender ! "pong"
}
}), "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()
}
addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.3.4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment