From 518a8e2c57c92f38932fabad50bd8e91f04fcfb3 Mon Sep 17 00:00:00 2001 From: Vojin Jovanovic Date: Fri, 3 Aug 2012 18:07:35 +0200 Subject: Additional Actor Migration test cases. --- test/files/jvm/actmig-loop-react.check | 1 + test/files/jvm/actmig-loop-react.scala | 191 +++++++++++++++++++++++++++++++ test/files/jvm/actmig-react-within.check | 2 + test/files/jvm/actmig-react-within.scala | 43 +++++++ test/files/jvm/actmig-receive.check | 27 +++++ test/files/jvm/actmig-receive.scala | 115 +++++++++++++++++++ 6 files changed, 379 insertions(+) create mode 100644 test/files/jvm/actmig-loop-react.scala create mode 100644 test/files/jvm/actmig-react-within.check create mode 100644 test/files/jvm/actmig-react-within.scala create mode 100644 test/files/jvm/actmig-receive.check create mode 100644 test/files/jvm/actmig-receive.scala 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..d0cba656f8 --- /dev/null +++ b/test/files/jvm/actmig-loop-react.scala @@ -0,0 +1,191 @@ +import scala.actors.MigrationSystem._ +import scala.actors.Actor._ +import scala.actors.{ Actor, StashingActor, ActorRef, Props, MigrationSystem, PoisonPill } +import java.util.concurrent.{ TimeUnit, CountDownLatch } +import scala.collection.mutable.ArrayBuffer +import scala.concurrent.util.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-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..f0326f885d --- /dev/null +++ b/test/files/jvm/actmig-react-within.scala @@ -0,0 +1,43 @@ +import scala.actors.MigrationSystem._ +import scala.actors.Actor._ +import scala.actors.{ Actor, StashingActor, ActorRef, Props, MigrationSystem, PoisonPill } +import java.util.concurrent.{ TimeUnit, CountDownLatch } +import scala.collection.mutable.ArrayBuffer +import scala.concurrent.util.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) + } + +} \ No newline at end of file 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..30730e9d87 --- /dev/null +++ b/test/files/jvm/actmig-receive.scala @@ -0,0 +1,115 @@ +import scala.actors.MigrationSystem._ +import scala.actors.Actor._ +import scala.actors.{ Actor, StashingActor, ActorRef, Props, MigrationSystem, PoisonPill } +import java.util.concurrent.{ TimeUnit, CountDownLatch } +import scala.collection.mutable.ArrayBuffer +import scala.concurrent.util.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() + } + +} -- cgit v1.2.3 From cc561873185d25e71091a11f5cb1b3003b9ebca3 Mon Sep 17 00:00:00 2001 From: Vojin Jovanovic Date: Wed, 12 Sep 2012 14:48:06 +0200 Subject: SI-6315 fixed. --- src/actors-migration/scala/actors/MigrationSystem.scala | 5 +++-- src/actors-migration/scala/actors/Pattern.scala | 3 ++- src/actors-migration/scala/actors/Props.scala | 4 +++- src/actors-migration/scala/actors/StashingActor.scala | 4 +++- src/actors-migration/scala/actors/Timeout.scala | 2 +- test/files/jvm/actmig-PinS_1.scala | 1 + test/files/jvm/actmig-PinS_2.scala | 3 ++- test/files/jvm/actmig-PinS_3.scala | 3 ++- test/files/jvm/actmig-loop-react.scala | 5 +++-- test/files/jvm/actmig-public-methods_1.scala | 3 ++- test/files/jvm/actmig-react-receive.scala | 5 +++-- test/files/jvm/actmig-react-within.scala | 5 +++-- test/files/jvm/actmig-receive.scala | 5 +++-- 13 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/actors-migration/scala/actors/MigrationSystem.scala b/src/actors-migration/scala/actors/MigrationSystem.scala index ffc93d9c6f..3dcb38e634 100644 --- a/src/actors-migration/scala/actors/MigrationSystem.scala +++ b/src/actors-migration/scala/actors/MigrationSystem.scala @@ -1,10 +1,11 @@ -package scala.actors +package scala.actors.migration +import scala.actors._ import scala.collection._ object MigrationSystem { - private[actors] val contextStack = new ThreadLocal[immutable.Stack[Boolean]] { + private[migration] val contextStack = new ThreadLocal[immutable.Stack[Boolean]] { override def initialValue() = immutable.Stack[Boolean]() } diff --git a/src/actors-migration/scala/actors/Pattern.scala b/src/actors-migration/scala/actors/Pattern.scala index 26e9d1bb64..28fd128141 100644 --- a/src/actors-migration/scala/actors/Pattern.scala +++ b/src/actors-migration/scala/actors/Pattern.scala @@ -1,5 +1,6 @@ -package scala.actors +package scala.actors.migration +import scala.actors._ import scala.concurrent.util.Duration import language.implicitConversions diff --git a/src/actors-migration/scala/actors/Props.scala b/src/actors-migration/scala/actors/Props.scala index 891e23213a..c12384ea55 100644 --- a/src/actors-migration/scala/actors/Props.scala +++ b/src/actors-migration/scala/actors/Props.scala @@ -1,4 +1,6 @@ -package scala.actors +package scala.actors.migration + +import scala.actors._ /** * ActorRef configuration object. It represents the minimal subset of Akka Props class. diff --git a/src/actors-migration/scala/actors/StashingActor.scala b/src/actors-migration/scala/actors/StashingActor.scala index 8f96e1b002..47f73c252a 100644 --- a/src/actors-migration/scala/actors/StashingActor.scala +++ b/src/actors-migration/scala/actors/StashingActor.scala @@ -1,5 +1,7 @@ -package scala.actors +package scala.actors.migration +import scala.actors._ +import scala.actors.Actor._ import scala.collection._ import scala.concurrent.util.Duration import java.util.concurrent.TimeUnit diff --git a/src/actors-migration/scala/actors/Timeout.scala b/src/actors-migration/scala/actors/Timeout.scala index 7e400ab140..78d15e5704 100644 --- a/src/actors-migration/scala/actors/Timeout.scala +++ b/src/actors-migration/scala/actors/Timeout.scala @@ -6,7 +6,7 @@ ** |/ ** \* */ -package scala.actors +package scala.actors.migration import scala.concurrent.util.Duration import java.util.concurrent.TimeUnit diff --git a/test/files/jvm/actmig-PinS_1.scala b/test/files/jvm/actmig-PinS_1.scala index 1fb50567b9..7ffff2d889 100644 --- a/test/files/jvm/actmig-PinS_1.scala +++ b/test/files/jvm/actmig-PinS_1.scala @@ -1,4 +1,5 @@ import scala.actors._ +import scala.actors.migration._ import scala.concurrent.util.duration._ import scala.concurrent.{ Promise, Await } diff --git a/test/files/jvm/actmig-PinS_2.scala b/test/files/jvm/actmig-PinS_2.scala index 46277efd43..dd0e6e5f0e 100644 --- a/test/files/jvm/actmig-PinS_2.scala +++ b/test/files/jvm/actmig-PinS_2.scala @@ -1,4 +1,5 @@ -import scala.actors.{ MigrationSystem, StashingActor, ActorRef, Props, Exit } +import scala.actors._ +import scala.actors.migration._ import scala.concurrent.util.duration._ import scala.concurrent.{ Promise, Await } diff --git a/test/files/jvm/actmig-PinS_3.scala b/test/files/jvm/actmig-PinS_3.scala index 321e99b1c2..9261046770 100644 --- a/test/files/jvm/actmig-PinS_3.scala +++ b/test/files/jvm/actmig-PinS_3.scala @@ -1,4 +1,5 @@ -import scala.actors.{ MigrationSystem, StashingActor, ActorRef, Terminated, Props } +import scala.actors._ +import scala.actors.migration._ import scala.concurrent.util.duration._ import scala.concurrent.{ Promise, Await } diff --git a/test/files/jvm/actmig-loop-react.scala b/test/files/jvm/actmig-loop-react.scala index d0cba656f8..828ebf6546 100644 --- a/test/files/jvm/actmig-loop-react.scala +++ b/test/files/jvm/actmig-loop-react.scala @@ -1,6 +1,7 @@ -import scala.actors.MigrationSystem._ +import scala.actors.migration.MigrationSystem._ import scala.actors.Actor._ -import scala.actors.{ Actor, StashingActor, ActorRef, Props, MigrationSystem, PoisonPill } +import scala.actors._ +import scala.actors.migration._ import java.util.concurrent.{ TimeUnit, CountDownLatch } import scala.collection.mutable.ArrayBuffer import scala.concurrent.util.duration._ diff --git a/test/files/jvm/actmig-public-methods_1.scala b/test/files/jvm/actmig-public-methods_1.scala index 7e5bc24210..59bdb500a5 100644 --- a/test/files/jvm/actmig-public-methods_1.scala +++ b/test/files/jvm/actmig-public-methods_1.scala @@ -1,10 +1,11 @@ import scala.collection.mutable.ArrayBuffer import scala.actors.Actor._ import scala.actors._ +import scala.actors.migration._ import scala.util._ import java.util.concurrent.{ TimeUnit, CountDownLatch } import scala.concurrent.util.Duration -import scala.actors.pattern._ +import scala.actors.migration.pattern._ object Test { val NUMBER_OF_TESTS = 6 diff --git a/test/files/jvm/actmig-react-receive.scala b/test/files/jvm/actmig-react-receive.scala index 8464a2af79..4ffdc722fd 100644 --- a/test/files/jvm/actmig-react-receive.scala +++ b/test/files/jvm/actmig-react-receive.scala @@ -1,6 +1,7 @@ -import scala.actors.MigrationSystem._ +import scala.actors.migration.MigrationSystem._ import scala.actors.Actor._ -import scala.actors.{ Actor, StashingActor, ActorRef, Props, MigrationSystem, PoisonPill } +import scala.actors._ +import scala.actors.migration._ import java.util.concurrent.{ TimeUnit, CountDownLatch } import scala.collection.mutable.ArrayBuffer import scala.concurrent.util.duration._ diff --git a/test/files/jvm/actmig-react-within.scala b/test/files/jvm/actmig-react-within.scala index f0326f885d..5c51508e5d 100644 --- a/test/files/jvm/actmig-react-within.scala +++ b/test/files/jvm/actmig-react-within.scala @@ -1,6 +1,7 @@ -import scala.actors.MigrationSystem._ +import scala.actors.migration.MigrationSystem._ import scala.actors.Actor._ -import scala.actors.{ Actor, StashingActor, ActorRef, Props, MigrationSystem, PoisonPill } +import scala.actors._ +import scala.actors.migration._ import java.util.concurrent.{ TimeUnit, CountDownLatch } import scala.collection.mutable.ArrayBuffer import scala.concurrent.util.duration._ diff --git a/test/files/jvm/actmig-receive.scala b/test/files/jvm/actmig-receive.scala index 30730e9d87..bd45d6e4ca 100644 --- a/test/files/jvm/actmig-receive.scala +++ b/test/files/jvm/actmig-receive.scala @@ -1,6 +1,7 @@ -import scala.actors.MigrationSystem._ +import scala.actors.migration.MigrationSystem._ import scala.actors.Actor._ -import scala.actors.{ Actor, StashingActor, ActorRef, Props, MigrationSystem, PoisonPill } +import scala.actors._ +import scala.actors.migration._ import java.util.concurrent.{ TimeUnit, CountDownLatch } import scala.collection.mutable.ArrayBuffer import scala.concurrent.util.duration._ -- cgit v1.2.3