summaryrefslogtreecommitdiff
path: root/test/files
diff options
context:
space:
mode:
authorVojin Jovanovic <vojin.jovanovic@epfl.ch>2012-09-24 17:06:08 +0200
committerVojin Jovanovic <vojin.jovanovic@epfl.ch>2012-09-25 11:14:46 +0200
commit3ba88113f23a1cd614366d770cb22fd2c6336771 (patch)
treef1bfb46d34f3538ffdc1f5b567b33e889febfd96 /test/files
parentb92becd757d8319129fa8bd0a93af8c6fd2b23b7 (diff)
downloadscala-3ba88113f23a1cd614366d770cb22fd2c6336771.tar.gz
scala-3ba88113f23a1cd614366d770cb22fd2c6336771.tar.bz2
scala-3ba88113f23a1cd614366d770cb22fd2c6336771.zip
Additional Actor Migration Tests.
Review by @phaller.
Diffstat (limited to 'test/files')
-rw-r--r--test/files/jvm/actmig-hierarchy.scala47
-rw-r--r--test/files/jvm/actmig-hierarchy_1.scala45
-rw-r--r--test/files/jvm/actmig-instantiation.scala96
-rw-r--r--test/files/jvm/actmig-loop-react.check1
-rw-r--r--test/files/jvm/actmig-loop-react.scala196
-rw-r--r--test/files/jvm/actmig-public-methods.scala74
-rw-r--r--test/files/jvm/actmig-react-within.check2
-rw-r--r--test/files/jvm/actmig-react-within.scala48
-rw-r--r--test/files/jvm/actmig-receive.check27
-rw-r--r--test/files/jvm/actmig-receive.scala120
10 files changed, 656 insertions, 0 deletions
diff --git a/test/files/jvm/actmig-hierarchy.scala b/test/files/jvm/actmig-hierarchy.scala
new file mode 100644
index 0000000000..17a44fda7a
--- /dev/null
+++ b/test/files/jvm/actmig-hierarchy.scala
@@ -0,0 +1,47 @@
+/**
+ * NOTE: Code snippets from this test are included in the Actor Migration Guide. In case you change
+ * code in these tests prior to the 2.10.0 release please send the notification to @vjovanov.
+ */
+import scala.actors._
+
+
+class ReactorActor extends Reactor[String] {
+ def act() {
+ var cond = true
+ loopWhile(cond) {
+ react {
+ case x if x == "hello1" => println("hello")
+ case "exit" => cond = false
+ }
+ }
+ }
+}
+
+class ReplyActor extends ReplyReactor {
+ def act() {
+ var cond = true
+ loopWhile(cond) {
+ react {
+ case "hello" => println("hello")
+ case "exit" => cond = false;
+ }
+ }
+ }
+}
+
+
+object Test {
+
+ def main(args: Array[String]) {
+ val reactorActor = new ReactorActor
+ val replyActor = new ReplyActor
+ reactorActor.start()
+ replyActor.start()
+
+ reactorActor ! "hello1"
+ replyActor ! "hello"
+
+ reactorActor ! "exit"
+ replyActor ! "exit"
+ }
+} \ No newline at end of file
diff --git a/test/files/jvm/actmig-hierarchy_1.scala b/test/files/jvm/actmig-hierarchy_1.scala
new file mode 100644
index 0000000000..14f03c9d48
--- /dev/null
+++ b/test/files/jvm/actmig-hierarchy_1.scala
@@ -0,0 +1,45 @@
+/**
+ * NOTE: Code snippets from this test are included in the Actor Migration Guide. In case you change
+ * code in these tests prior to the 2.10.0 release please send the notification to @vjovanov.
+ */
+import scala.actors._
+
+class ReactorActor extends Actor {
+ def act() {
+ var cond = true
+ loopWhile(cond) {
+ react {
+ case x: String if x == "hello1" => println("hello")
+ case "exit" => cond = false
+ }
+ }
+ }
+}
+
+class ReplyActor extends Actor {
+ def act() {
+ var cond = true
+ loopWhile(cond) {
+ react {
+ case "hello" => println("hello")
+ case "exit" => cond = false;
+ }
+ }
+ }
+}
+
+object Test {
+
+ def main(args: Array[String]) {
+ val reactorActor = new ReactorActor
+ val replyActor = new ReplyActor
+ reactorActor.start()
+ replyActor.start()
+
+ reactorActor ! "hello1"
+ replyActor ! "hello"
+
+ reactorActor ! "exit"
+ replyActor ! "exit"
+ }
+} \ No newline at end of file
diff --git a/test/files/jvm/actmig-instantiation.scala b/test/files/jvm/actmig-instantiation.scala
new file mode 100644
index 0000000000..d54dff9558
--- /dev/null
+++ b/test/files/jvm/actmig-instantiation.scala
@@ -0,0 +1,96 @@
+/**
+ * NOTE: Code snippets from this test are included in the Actor Migration Guide. In case you change
+ * code in these tests prior to the 2.10.0 release please send the notification to @vjovanov.
+ */
+import scala.actors.migration.MigrationSystem._
+import scala.actors.migration._
+import scala.actors.Actor._
+import scala.actors._
+import java.util.concurrent.{ TimeUnit, CountDownLatch }
+import scala.collection.mutable.ArrayBuffer
+
+class TestStashingActor extends StashingActor {
+
+ def receive = { case v: Int => Test.append(v); Test.latch.countDown() }
+
+}
+
+object Test {
+ val NUMBER_OF_TESTS = 5
+
+ // used for sorting non-deterministic output
+ val buff = ArrayBuffer[Int](0)
+ val latch = new CountDownLatch(NUMBER_OF_TESTS)
+ val toStop = ArrayBuffer[ActorRef]()
+
+ def append(v: Int) = synchronized {
+ buff += v
+ }
+
+ def main(args: Array[String]) = {
+ // plain scala actor
+ val a1 = actor {
+ react { case v: Int => Test.append(v); Test.latch.countDown() }
+ }
+ a1 ! 100
+
+ // simple instantiation
+ val a2 = MigrationSystem.actorOf(Props(() => new TestStashingActor, "akka.actor.default-stash-dispatcher"))
+ a2 ! 200
+ toStop += a2
+
+ // actor of with scala actor
+ val a3 = MigrationSystem.actorOf(Props(() => actor {
+ react { case v: Int => Test.append(v); Test.latch.countDown() }
+ }, "akka.actor.default-stash-dispatcher"))
+ a3 ! 300
+
+ // using the manifest
+ val a4 = MigrationSystem.actorOf(Props(() => new TestStashingActor, "akka.actor.default-stash-dispatcher"))
+ a4 ! 400
+ toStop += a4
+
+ // deterministic part of a test
+ // creation without actorOf
+ try {
+ val a3 = new TestStashingActor
+ a3 ! -1
+ } catch {
+ case e => println("OK error: " + e)
+ }
+
+ // actorOf double creation
+ try {
+ val a3 = MigrationSystem.actorOf(Props(() => {
+ new TestStashingActor
+ new TestStashingActor
+ }, "akka.actor.default-stash-dispatcher"))
+ a3 ! -1
+ } catch {
+ case e => println("OK error: " + e)
+ }
+
+ // actorOf nesting
+ try {
+ val a5 = MigrationSystem.actorOf(Props(() => {
+ val a6 = MigrationSystem.actorOf(Props(() => new TestStashingActor, "akka.actor.default-stash-dispatcher"))
+ toStop += a6
+ new TestStashingActor
+ }, "akka.actor.default-stash-dispatcher"))
+
+ a5 ! 500
+ toStop += a5
+ } catch {
+ case e => println("Should not throw an exception: " + e)
+ }
+
+ // output
+ latch.await(5, TimeUnit.SECONDS)
+ if (latch.getCount() > 0) {
+ println("Error: Tasks have not finished!!!")
+ }
+
+ buff.sorted.foreach(println)
+ toStop.foreach(_ ! PoisonPill)
+ }
+} \ No newline at end of file
diff --git a/test/files/jvm/actmig-loop-react.check b/test/files/jvm/actmig-loop-react.check
index 54cbe942c0..2474cbe71b 100644
--- a/test/files/jvm/actmig-loop-react.check
+++ b/test/files/jvm/actmig-loop-react.check
@@ -13,3 +13,4 @@ after react
do task 1
do string I am a String
do task 42
+after react
diff --git a/test/files/jvm/actmig-loop-react.scala b/test/files/jvm/actmig-loop-react.scala
new file mode 100644
index 0000000000..7f4c6f96dc
--- /dev/null
+++ b/test/files/jvm/actmig-loop-react.scala
@@ -0,0 +1,196 @@
+/**
+ * NOTE: Code snippets from this test are included in the Actor Migration Guide. In case you change
+ * code in these tests prior to the 2.10.0 release please send the notification to @vjovanov.
+ */
+import scala.actors.migration.MigrationSystem._
+import scala.actors.Actor._
+import scala.actors._
+import scala.actors.migration._
+import java.util.concurrent.{ TimeUnit, CountDownLatch }
+import scala.collection.mutable.ArrayBuffer
+import scala.concurrent.duration._
+import scala.concurrent.{ Promise, Await }
+
+object Test {
+ val finishedLWCR, finishedTNR, finishedEH = Promise[Boolean]
+ val finishedLWCR1, finishedTNR1, finishedEH1 = Promise[Boolean]
+
+ def testLoopWithConditionReact() = {
+ // Snippet showing composition of receives
+ // Loop with Condition Snippet - before
+ val myActor = actor {
+ var c = true
+ loopWhile(c) {
+ react {
+ case x: Int =>
+ // do task
+ println("do task")
+ if (x == 42) {
+ c = false
+ finishedLWCR1.success(true)
+ }
+ }
+ }
+ }
+
+ myActor.start()
+ myActor ! 1
+ myActor ! 42
+
+ Await.ready(finishedLWCR1.future, 5 seconds)
+
+ // Loop with Condition Snippet - migrated
+ val myAkkaActor = MigrationSystem.actorOf(Props(() => new StashingActor {
+
+ def receive = {
+ case x: Int =>
+ // do task
+ println("do task")
+ if (x == 42) {
+ finishedLWCR.success(true)
+ context.stop(self)
+ }
+ }
+ }, "default-stashing-dispatcher"))
+ myAkkaActor ! 1
+ myAkkaActor ! 42
+ }
+
+ def testNestedReact() = {
+ // Snippet showing composition of receives
+ // Loop with Condition Snippet - before
+ val myActor = actor {
+ var c = true
+ loopWhile(c) {
+ react {
+ case x: Int =>
+ // do task
+ println("do task " + x)
+ if (x == 42) {
+ c = false
+ } else {
+ react {
+ case y: String =>
+ println("do string " + y)
+ }
+ }
+ println("after react")
+ finishedTNR1.success(true)
+ }
+ }
+ }
+ myActor.start()
+
+ myActor ! 1
+ myActor ! "I am a String"
+ myActor ! 42
+
+ Await.ready(finishedTNR1.future, 5 seconds)
+
+ // Loop with Condition Snippet - migrated
+ val myAkkaActor = MigrationSystem.actorOf(Props(() => new StashingActor {
+
+ def receive = {
+ case x: Int =>
+ // do task
+ println("do task " + x)
+ if (x == 42) {
+ println("after react")
+ finishedTNR.success(true)
+ context.stop(self)
+ } else
+ context.become(({
+ case y: String =>
+ println("do string " + y)
+ }: Receive).andThen(x => {
+ unstashAll()
+ context.unbecome()
+ }).orElse { case x => stash() })
+ }
+ }, "default-stashing-dispatcher"))
+
+ myAkkaActor ! 1
+ myAkkaActor ! "I am a String"
+ myAkkaActor ! 42
+
+ }
+
+ def exceptionHandling() = {
+ // Stashing actor with act and exception handler
+ val myActor = MigrationSystem.actorOf(Props(() => new StashingActor {
+
+ def receive = { case _ => println("Dummy method.") }
+ override def act() = {
+ loop {
+ react {
+ case "fail" =>
+ throw new Exception("failed")
+ case "work" =>
+ println("working")
+ case "die" =>
+ finishedEH1.success(true)
+ exit()
+ }
+ }
+ }
+
+ override def exceptionHandler = {
+ case x: Exception => println("scala got exception")
+ }
+
+ }, "default-stashing-dispatcher"))
+
+ myActor ! "work"
+ myActor ! "fail"
+ myActor ! "die"
+
+ Await.ready(finishedEH1.future, 5 seconds)
+ // Stashing actor in Akka style
+ val myAkkaActor = MigrationSystem.actorOf(Props(() => new StashingActor {
+ def receive = PFCatch({
+ case "fail" =>
+ throw new Exception("failed")
+ case "work" =>
+ println("working")
+ case "die" =>
+ finishedEH.success(true)
+ context.stop(self)
+ }, { case x: Exception => println("akka got exception") })
+ }, "default-stashing-dispatcher"))
+
+ myAkkaActor ! "work"
+ myAkkaActor ! "fail"
+ myAkkaActor ! "die"
+ }
+
+ def main(args: Array[String]) = {
+ testLoopWithConditionReact()
+ Await.ready(finishedLWCR.future, 5 seconds)
+ exceptionHandling()
+ Await.ready(finishedEH.future, 5 seconds)
+ testNestedReact()
+ Await.ready(finishedTNR.future, 5 seconds)
+ }
+
+}
+
+// As per Jim Mcbeath's blog (http://jim-mcbeath.blogspot.com/2008/07/actor-exceptions.html)
+class PFCatch(f: PartialFunction[Any, Unit],
+ handler: PartialFunction[Exception, Unit])
+ extends PartialFunction[Any, Unit] {
+
+ def apply(x: Any) = {
+ try {
+ f(x)
+ } catch {
+ case e: Exception if handler.isDefinedAt(e) => handler(e)
+ }
+ }
+
+ def isDefinedAt(x: Any) = f.isDefinedAt(x)
+}
+
+object PFCatch {
+ def apply(f: PartialFunction[Any, Unit],
+ handler: PartialFunction[Exception, Unit]) = new PFCatch(f, handler)
+}
diff --git a/test/files/jvm/actmig-public-methods.scala b/test/files/jvm/actmig-public-methods.scala
new file mode 100644
index 0000000000..58d7a1a9d4
--- /dev/null
+++ b/test/files/jvm/actmig-public-methods.scala
@@ -0,0 +1,74 @@
+/**
+ * NOTE: Code snippets from this test are included in the Actor Migration Guide. In case you change
+ * code in these tests prior to the 2.10.0 release please send the notification to @vjovanov.
+ */
+import scala.collection.mutable.ArrayBuffer
+import scala.actors.Actor._
+import scala.actors._
+import scala.actors.migration.MigrationSystem
+import scala.util.continuations._
+import java.util.concurrent.{ TimeUnit, CountDownLatch }
+
+object Test {
+ val NUMBER_OF_TESTS = 6
+
+ // used for sorting non-deterministic output
+ val buff = ArrayBuffer[String]()
+ val latch = new CountDownLatch(NUMBER_OF_TESTS)
+ val toStop = ArrayBuffer[Actor]()
+
+ def append(v: String) = synchronized {
+ buff += v
+ }
+
+ def main(args: Array[String]) = {
+
+ val respActor = actor {
+ loop {
+ react {
+ case (x: String, time: Long) =>
+ Thread.sleep(time)
+ reply(x + " after " + time)
+ case str: String =>
+ append(str)
+ latch.countDown()
+ case _ => exit()
+ }
+ }
+ }
+
+ toStop += respActor
+
+ respActor ! ("bang")
+
+ val res1 = respActor !? (("bang qmark", 0L))
+ append(res1.toString)
+ latch.countDown()
+
+ val res2 = respActor !? (5000, ("bang qmark", 1L))
+ append(res2.toString)
+ latch.countDown()
+
+ // this one should timeout
+ val res21 = respActor !? (1, ("bang qmark", 5000L))
+ append(res21.toString)
+ latch.countDown()
+
+ val fut1 = respActor !! (("bang bang in the future", 0L))
+ append(fut1().toString())
+ latch.countDown()
+
+ val fut2 = respActor !! (("typed bang bang in the future", 0L), { case x: String => x })
+ append(fut2())
+ latch.countDown()
+
+ // output
+ latch.await(10, TimeUnit.SECONDS)
+ if (latch.getCount() > 0) {
+ println("Error: Tasks have not finished!!!")
+ }
+
+ buff.sorted.foreach(println)
+ toStop.foreach(_ ! 'stop)
+ }
+} \ No newline at end of file
diff --git a/test/files/jvm/actmig-react-within.check b/test/files/jvm/actmig-react-within.check
new file mode 100644
index 0000000000..57798dbefb
--- /dev/null
+++ b/test/files/jvm/actmig-react-within.check
@@ -0,0 +1,2 @@
+received
+received
diff --git a/test/files/jvm/actmig-react-within.scala b/test/files/jvm/actmig-react-within.scala
new file mode 100644
index 0000000000..43350ef120
--- /dev/null
+++ b/test/files/jvm/actmig-react-within.scala
@@ -0,0 +1,48 @@
+/**
+ * NOTE: Code snippets from this test are included in the Actor Migration Guide. In case you change
+ * code in these tests prior to the 2.10.0 release please send the notification to @vjovanov.
+ */
+import scala.actors.migration.MigrationSystem._
+import scala.actors.Actor._
+import scala.actors._
+import scala.actors.migration._
+import java.util.concurrent.{ TimeUnit, CountDownLatch }
+import scala.collection.mutable.ArrayBuffer
+import scala.concurrent.duration._
+import scala.concurrent.{ Promise, Await }
+
+object Test {
+ val finished = Promise[Boolean]
+
+ def testReactWithin() = {
+ val sActor = actor {
+ loop {
+ reactWithin(1) {
+ case scala.actors.TIMEOUT =>
+ println("received")
+ exit()
+ case _ =>
+ println("Should not occur.")
+ }
+ }
+ }
+
+ val myActor = MigrationSystem.actorOf(Props(() => new StashingActor {
+ context.setReceiveTimeout(1 millisecond)
+ def receive = {
+ case ReceiveTimeout =>
+ println("received")
+ finished.success(true)
+ context.stop(self)
+ case _ =>
+ println("Should not occur.")
+ }
+ }, "default-stashing-dispatcher"))
+ }
+
+ def main(args: Array[String]) = {
+ testReactWithin()
+ Await.ready(finished.future, 5 seconds)
+ }
+
+}
diff --git a/test/files/jvm/actmig-receive.check b/test/files/jvm/actmig-receive.check
new file mode 100644
index 0000000000..30886140e1
--- /dev/null
+++ b/test/files/jvm/actmig-receive.check
@@ -0,0 +1,27 @@
+Original
+do before
+receive 1
+do in between
+receive 1
+do after
+Transformed
+do before
+receive 1
+do in between
+receive 1
+do after
+Test Loop Receive
+Original
+do before body
+receive 1
+do after receive
+do before body
+do after receive
+after loop
+Transformed
+do before body
+receive 1
+do after receive
+do before body
+do after receive
+after loop
diff --git a/test/files/jvm/actmig-receive.scala b/test/files/jvm/actmig-receive.scala
new file mode 100644
index 0000000000..03dc1be63b
--- /dev/null
+++ b/test/files/jvm/actmig-receive.scala
@@ -0,0 +1,120 @@
+/**
+ * NOTE: Code snippets from this test are included in the Actor Migration Guide. In case you change
+ * code in these tests prior to the 2.10.0 release please send the notification to @vjovanov.
+ */
+import scala.actors.migration.MigrationSystem._
+import scala.actors.Actor._
+import scala.actors._
+import scala.actors.migration._
+import java.util.concurrent.{ TimeUnit, CountDownLatch }
+import scala.collection.mutable.ArrayBuffer
+import scala.concurrent.duration._
+import scala.concurrent.{ Promise, Await }
+
+object Test {
+ val finishedSingle, finishedSingle1, finishedLoop, finishedLoop1 = Promise[Boolean]
+
+ def testDoubleReceive() = {
+ println("Original")
+ // Snippet that shows how to get rid of receive calls in Scala Actors.
+ // This snippet is used in the Actors Migration Kit.
+ val myActor = actor {
+ println("do before")
+ receive {
+ case "hello" =>
+ println("receive 1")
+ }
+ println("do in between")
+ receive {
+ case "hello" =>
+ println("receive 1")
+ }
+ println("do after")
+ finishedSingle.success(true)
+ }
+
+ myActor ! "hello"
+ myActor ! "hello"
+
+ Await.ready(finishedSingle.future, 5 seconds)
+ println("Transformed")
+ val myActorReact = actor {
+ println("do before")
+ react (({
+ case "hello" =>
+ println("receive 1")
+ }: PartialFunction[Any, Unit]).andThen { x =>
+ println("do in between")
+ react (({
+ case "hello" =>
+ println("receive 1")
+ }: PartialFunction[Any, Unit]).andThen { x =>
+ println("do after")
+ finishedSingle1.success(true)
+ })
+ })
+ }
+
+ myActorReact ! "hello"
+ myActorReact ! "hello"
+
+ Await.ready(finishedSingle1.future, 5 seconds)
+ }
+
+ def testLoopReceive() = {
+ println("Test Loop Receive")
+ // Snippet that shows how to get rid of receive calls in loops.
+ // This snippet is used in the Actors Migration Kit.
+ println("Original")
+ val myActor = actor {
+ var c = true
+ while (c) {
+ println("do before body")
+ receive {
+ case "hello" =>
+ println("receive 1")
+ case "exit" =>
+ c = false
+ }
+ println("do after receive")
+ }
+ println("after loop")
+ finishedLoop.success(true)
+ }
+
+ myActor ! "hello"
+ myActor ! "exit"
+ Await.ready(finishedLoop.future, 5 seconds)
+ println("Transformed")
+
+ val myActorReact = actor {
+ var c = true
+ loopWhile(c) {
+ println("do before body")
+ react (({
+ case "hello" =>
+ println("receive 1")
+ case "exit" =>
+ c = false
+ }: PartialFunction[Any, Unit]).andThen { x =>
+ println("do after receive")
+ if (c == false) {
+ println("after loop")
+ finishedLoop1.success(true)
+ }
+ })
+ }
+ }
+
+ myActorReact ! "hello"
+ myActorReact ! "exit"
+
+ Await.ready(finishedLoop1.future, 5 seconds)
+ }
+
+ def main(args: Array[String]) = {
+ testDoubleReceive()
+ testLoopReceive()
+ }
+
+}