+sam #3689 Make activator template of the multi-node sample

This commit is contained in:
Patrik Nordwall 2014-02-11 13:28:22 +01:00
parent 276eb0881f
commit 1a3080b7a6
12 changed files with 177 additions and 107 deletions

View file

@ -21,86 +21,9 @@ You can then add multi-JVM testing to ``project/Build.scala`` by including the `
settings and config. Please note that MultiJvm test sources are located in ``src/multi-jvm/...``, settings and config. Please note that MultiJvm test sources are located in ``src/multi-jvm/...``,
and not in ``src/test/...``. and not in ``src/test/...``.
Here is an example Build.scala file for sbt 0.12 that uses the MultiJvm plugin: Here is an example Build.scala file for sbt 0.13 that uses the MultiJvm plugin:
.. parsed-literal:: .. includecode:: ../../../akka-samples/akka-sample-multi-node-scala/project/Build.scala
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 := "@scalaVersion@",
// 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 results = testResults ++ multiJvmResults
(Tests.overall(results.values), results)
}
)
object Dependencies {
val example = Seq(
// ---- application dependencies ----
"com.typesafe.akka" %% "akka-actor" % "@version@" @crossString@,
"com.typesafe.akka" %% "akka-remote" % "@version@" @crossString@,
// ---- test dependencies ----
"com.typesafe.akka" %% "akka-testkit" % "@version@" %
"test" @crossString@,
"com.typesafe.akka" %% "akka-multi-node-testkit" % "@version@" %
"test" @crossString@,
"org.scalatest" %% "scalatest" % "2.0" % "test",
"junit" % "junit" % "4.10" % "test"
)
}
}
If you are using sbt 0.13 the multiJvmSettings in the Build.scala file looks like this instead:
.. parsed-literal::
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)
}
)
You can specify JVM options for the forked JVMs:: You can specify JVM options for the forked JVMs::

View file

@ -185,20 +185,23 @@ A Multi Node Testing Example
First we need some scaffolding to hook up the ``MultiNodeSpec`` with your favorite test framework. Lets define a trait First we need some scaffolding to hook up the ``MultiNodeSpec`` with your favorite test framework. Lets define a trait
``STMultiNodeSpec`` that uses ScalaTest to start and stop ``MultiNodeSpec``. ``STMultiNodeSpec`` that uses ScalaTest to start and stop ``MultiNodeSpec``.
.. includecode:: ../../../akka-samples/akka-sample-multi-node/src/test/scala/sample/multinode/STMultiNodeSpec.scala#example .. includecode:: ../../../akka-samples/akka-sample-multi-node-scala/src/test/scala/sample/multinode/STMultiNodeSpec.scala#example
Then we need to define a configuration. Lets use two nodes ``"node1`` and ``"node2"`` and call it Then we need to define a configuration. Lets use two nodes ``"node1`` and ``"node2"`` and call it
``MultiNodeSampleConfig``. ``MultiNodeSampleConfig``.
.. includecode:: ../../../akka-samples/akka-sample-multi-node/src/multi-jvm/scala/sample/multinode/MultiNodeSample.scala .. includecode:: ../../../akka-samples/akka-sample-multi-node-scala/src/multi-jvm/scala/sample/multinode/MultiNodeSample.scala
:include: package,config :include: package,config
And then finally to the node test code. That starts the two nodes, and demonstrates a barrier, and a remote actor And then finally to the node test code. That starts the two nodes, and demonstrates a barrier, and a remote actor
message send/receive. message send/receive.
.. includecode:: ../../../akka-samples/akka-sample-multi-node/src/multi-jvm/scala/sample/multinode/MultiNodeSample.scala .. includecode:: ../../../akka-samples/akka-sample-multi-node-scala/src/multi-jvm/scala/sample/multinode/MultiNodeSample.scala
:include: package,spec :include: package,spec
The easiest way to run this example yourself is to download `Typesafe Activator <http://typesafe.com/platform/getstarted>`_
and open the tutorial named `Akka Multi-Node Testing Sample with Scala <http://typesafe.com/activator/template/akka-sample-multi-node-scala>`_.
Things to Keep in Mind Things to Keep in Mind
====================== ======================

View file

@ -0,0 +1,13 @@
Copyright 2013-2014 Typesafe, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View file

@ -0,0 +1,7 @@
name=akka-sample-multi-node-scala
title=Akka Multi-Node Testing Sample with Scala
description=Sample containing sbt build settings and test classes for illustrating multi-node testing with Akka and Scala
tags=akka,testing,scala,sample
authorName=Akka Team
authorLink=http://akka.io/
sourceLink=https://github.com/akka/akka

View file

@ -0,0 +1,40 @@
import sbt._
import sbt.Keys._
import com.typesafe.sbt.SbtMultiJvm
import com.typesafe.sbt.SbtMultiJvm.MultiJvmKeys.MultiJvm
object AkkaSampleMultiNodeBuild extends Build {
val akkaVersion = "2.3-SNAPSHOT"
lazy val akkaSampleMultiNode = Project(
id = "akka-sample-multi-node-scala",
base = file("."),
settings = Project.defaultSettings ++ SbtMultiJvm.multiJvmSettings ++ Seq(
name := "akka-sample-multi-node-scala",
version := "1.0",
scalaVersion := "2.10.3",
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-remote" % akkaVersion,
"com.typesafe.akka" %% "akka-multi-node-testkit" % akkaVersion,
"org.scalatest" %% "scalatest" % "2.0" % "test"),
// 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,
// and combine the results from ordinary test and multi-jvm tests
executeTests in Test <<= (executeTests in Test, executeTests in MultiJvm) map {
case (testResults, multiNodeResults) =>
val overall =
if (testResults.overall.id < multiNodeResults.overall.id)
multiNodeResults.overall
else
testResults.overall
Tests.Output(overall,
testResults.events ++ multiNodeResults.events,
testResults.summaries ++ multiNodeResults.summaries)
}
)
) configs (MultiJvm)
}

View file

@ -0,0 +1 @@
sbt.version=0.13.1

View file

@ -0,0 +1,4 @@
resolvers += Classpaths.typesafeResolver
addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.3.8")

View file

@ -22,10 +22,19 @@ import akka.actor.{ Props, Actor }
class MultiNodeSampleSpecMultiJvmNode1 extends MultiNodeSample class MultiNodeSampleSpecMultiJvmNode1 extends MultiNodeSample
class MultiNodeSampleSpecMultiJvmNode2 extends MultiNodeSample class MultiNodeSampleSpecMultiJvmNode2 extends MultiNodeSample
object MultiNodeSample {
class Ponger extends Actor {
def receive = {
case "ping" => sender() ! "pong"
}
}
}
class MultiNodeSample extends MultiNodeSpec(MultiNodeSampleConfig) class MultiNodeSample extends MultiNodeSpec(MultiNodeSampleConfig)
with STMultiNodeSpec with ImplicitSender { with STMultiNodeSpec with ImplicitSender {
import MultiNodeSampleConfig._ import MultiNodeSampleConfig._
import MultiNodeSample._
def initialParticipants = roles.size def initialParticipants = roles.size
@ -38,17 +47,13 @@ class MultiNodeSample extends MultiNodeSpec(MultiNodeSampleConfig)
"send to and receive from a remote node" in { "send to and receive from a remote node" in {
runOn(node1) { runOn(node1) {
enterBarrier("deployed") enterBarrier("deployed")
val ponger = system.actorFor(node(node2) / "user" / "ponger") val ponger = system.actorSelection(node(node2) / "user" / "ponger")
ponger ! "ping" ponger ! "ping"
expectMsg("pong") expectMsg("pong")
} }
runOn(node2) { runOn(node2) {
system.actorOf(Props(new Actor { system.actorOf(Props[Ponger], "ponger")
def receive = {
case "ping" => sender() ! "pong"
}
}), "ponger")
enterBarrier("deployed") enterBarrier("deployed")
} }

View file

@ -0,0 +1,89 @@
<!-- <html> -->
<head>
<title>Akka Multi-Node Testing Sample with Scala</title>
</head>
<body>
<div>
<p>
This sample contains <a href="http://www.scala-sbt.org/" target="_blank">sbt</a> build settings
and test classes for illustrating multi-node testing with Akka.
</p>
<p>
Please refer to the full documentation of
<a href="http://doc.akka.io/docs/akka/2.3-SNAPSHOT/dev/multi-node-testing.html" target="_blank">multi-node testing</a>
and the closely related
<a href="http://doc.akka.io/docs/akka/2.3-SNAPSHOT/dev/multi-jvm-testing.html" target="_blank">multi-jvm testing</a>
for details.
There is also an section on
<a href="http://doc.akka.io/docs/akka/2.3-SNAPSHOT/scala/cluster-usage.html#How_to_Test" target="_blank">cluster testing</a>.
</p>
</div>
<div>
<h2>sbt setup</h2>
<p>
Open <a href="#code/project/plugins.sbt" class="shortcut">project/plugins.sbt</a>
</p>
<p>
It adds the <a href="http://github.com/typesafehub/sbt-multi-jvm" target="_blank">sbt-multi-jvm</a> plugin to the build.
</p>
<p>
Open <a href="#code/project/Build.scala" class="shortcut">project/Build.scala</a>
</p>
<p>
It includes the MultiJvm settings that are needed to run multi-jvm tests.
</p>
</div>
<div>
<h2>Tests</h2>
<p>
Open <a href="#code/src/multi-jvm/scala/sample/multinode/MultiNodeSample.scala" class="shortcut">MultiNodeSample.scala</a>
</p>
<p>
Note that MultiJvm test sources are located in <code>src/multi-jvm/...</code> and the
test classes must end with <code>MultiJvm</code> followed by the node name, typically
<code>Node1</code>, <code>Node2</code>, <code>Node3</code>...
</p>
<p>
To hook up the MultiNodeSpec with with ScalaTest you need something like:
<a href="#code/src/test/scala/sample/multinode/STMultiNodeSpec.scala" class="shortcut">STMultiNodeSpec.scala</a>
</p>
<p>
To see the test in action, open the <a href="#test" class="shortcut">Test</a> tab
and click <b>Start</b> to run the <code>MultiNodeSample</code>. This corresponds to
<code>sbt test</code>.
</p>
<p>
In case you have many tests in the project it can be convenient to run a single test from
the sbt prompt:
</p>
<pre><code>
&gt; multi-jvm:testOnly sample.multinode.MultiNodeSampleSpec
</code></pre>
<p>
The same test can be run on multiple machines as described in the
<a href="http://doc.akka.io/docs/akka/2.3-SNAPSHOT/dev/multi-node-testing.html" target="_blank">multi-node testing documentation</a>.
</p>
</div>
</body>
</html>

View file

@ -1,15 +0,0 @@
Multi-Node Test Sample
======================
This sample is meant to be used by studying the code; it does not perform any
astounding functions when running it. If you want to run it, check out the akka
sources on your local hard drive, follow the [instructions for setting up Akka
with SBT](http://doc.akka.io/docs/akka/current/intro/getting-started.html).
When you start SBT within the checked-out akka source directory, you can run
this sample by typing
akka-sample-multi-node-experimental/multi-jvm:test-only sample.multinode.MultiNodeSampleSpec
(You might have to pass a system property containing `akka.test.tags.include=long-running`.)
You can read more in the [Akka docs](http://akka.io/docs).

View file

@ -370,7 +370,7 @@ object AkkaBuild extends Build {
aggregate = Seq(camelSampleJava, camelSampleScala, mainSampleJava, mainSampleScala, aggregate = Seq(camelSampleJava, camelSampleScala, mainSampleJava, mainSampleScala,
remoteSampleJava, remoteSampleScala, clusterSampleJava, clusterSampleScala, remoteSampleJava, remoteSampleScala, clusterSampleJava, clusterSampleScala,
fsmSampleScala, persistenceSample, fsmSampleScala, persistenceSample,
multiNodeSample, helloKernelSample, osgiDiningHakkersSample) multiNodeSampleScala, helloKernelSample, osgiDiningHakkersSample)
) )
lazy val camelSampleJava = Project( lazy val camelSampleJava = Project(
@ -472,9 +472,9 @@ object AkkaBuild extends Build {
) )
) configs (MultiJvm) ) configs (MultiJvm)
lazy val multiNodeSample = Project( lazy val multiNodeSampleScala = Project(
id = "akka-sample-multi-node", id = "akka-sample-multi-node-scala",
base = file("akka-samples/akka-sample-multi-node"), base = file("akka-samples/akka-sample-multi-node-scala"),
dependencies = Seq(multiNodeTestkit % "test", testkit % "test"), dependencies = Seq(multiNodeTestkit % "test", testkit % "test"),
settings = multiJvmSettings ++ sampleSettings ++ experimentalSettings ++ Seq( settings = multiJvmSettings ++ sampleSettings ++ experimentalSettings ++ Seq(
libraryDependencies ++= Dependencies.multiNodeSample, libraryDependencies ++= Dependencies.multiNodeSample,