2011-04-16 22:20:04 +02:00
|
|
|
package akka.testkit
|
|
|
|
|
|
|
|
|
|
import org.scalatest.matchers.MustMatchers
|
2011-05-18 17:25:30 +02:00
|
|
|
import org.scalatest.{ BeforeAndAfterEach, WordSpec }
|
2011-04-16 22:20:04 +02:00
|
|
|
import akka.actor._
|
|
|
|
|
import akka.config.Supervision.OneForOneStrategy
|
|
|
|
|
import akka.event.EventHandler
|
|
|
|
|
import akka.dispatch.Future
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test whether TestActorRef behaves as an ActorRef should, besides its own spec.
|
|
|
|
|
*
|
|
|
|
|
* @author Roland Kuhn
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
object TestActorRefSpec {
|
|
|
|
|
|
|
|
|
|
var counter = 4
|
|
|
|
|
val thread = Thread.currentThread
|
2011-05-18 17:25:30 +02:00
|
|
|
var otherthread: Thread = null
|
2011-04-16 22:20:04 +02:00
|
|
|
|
|
|
|
|
trait TActor extends Actor {
|
|
|
|
|
def receive = new Receive {
|
|
|
|
|
val recv = receiveT
|
2011-05-18 17:25:30 +02:00
|
|
|
def isDefinedAt(o: Any) = recv.isDefinedAt(o)
|
|
|
|
|
def apply(o: Any) {
|
2011-04-16 22:20:04 +02:00
|
|
|
if (Thread.currentThread ne thread)
|
|
|
|
|
otherthread = Thread.currentThread
|
|
|
|
|
recv(o)
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-18 17:25:30 +02:00
|
|
|
def receiveT: Receive
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ReplyActor extends TActor {
|
|
|
|
|
var replyTo: Channel[Any] = null
|
|
|
|
|
|
|
|
|
|
def receiveT = {
|
2011-05-18 17:25:30 +02:00
|
|
|
case "complexRequest" ⇒ {
|
2011-04-16 22:20:04 +02:00
|
|
|
replyTo = self.channel
|
|
|
|
|
val worker = TestActorRef[WorkerActor].start()
|
|
|
|
|
worker ! "work"
|
|
|
|
|
}
|
2011-05-18 17:25:30 +02:00
|
|
|
case "complexRequest2" ⇒
|
2011-04-16 22:20:04 +02:00
|
|
|
val worker = TestActorRef[WorkerActor].start()
|
|
|
|
|
worker ! self.channel
|
2011-05-18 17:25:30 +02:00
|
|
|
case "workDone" ⇒ replyTo ! "complexReply"
|
|
|
|
|
case "simpleRequest" ⇒ self.reply("simpleReply")
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class WorkerActor() extends TActor {
|
|
|
|
|
def receiveT = {
|
2011-05-18 17:25:30 +02:00
|
|
|
case "work" ⇒ {
|
2011-04-16 22:20:04 +02:00
|
|
|
self.reply("workDone")
|
|
|
|
|
self.stop()
|
|
|
|
|
}
|
2011-05-18 17:25:30 +02:00
|
|
|
case replyTo: Channel[Any] ⇒ {
|
2011-04-16 22:20:04 +02:00
|
|
|
replyTo ! "complexReply"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SenderActor(replyActor: ActorRef) extends TActor {
|
|
|
|
|
|
|
|
|
|
def receiveT = {
|
2011-05-18 17:25:30 +02:00
|
|
|
case "complex" ⇒ replyActor ! "complexRequest"
|
|
|
|
|
case "complex2" ⇒ replyActor ! "complexRequest2"
|
|
|
|
|
case "simple" ⇒ replyActor ! "simpleRequest"
|
|
|
|
|
case "complexReply" ⇒ {
|
2011-04-16 22:20:04 +02:00
|
|
|
counter -= 1
|
|
|
|
|
}
|
2011-05-18 17:25:30 +02:00
|
|
|
case "simpleReply" ⇒ {
|
2011-04-16 22:20:04 +02:00
|
|
|
counter -= 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Logger extends Actor {
|
|
|
|
|
import EventHandler._
|
|
|
|
|
var count = 0
|
2011-05-18 17:25:30 +02:00
|
|
|
var msg: String = _
|
2011-04-16 22:20:04 +02:00
|
|
|
def receive = {
|
2011-05-18 17:25:30 +02:00
|
|
|
case Warning(_, m: String) ⇒ count += 1; msg = m
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TestActorRefSpec extends WordSpec with MustMatchers with BeforeAndAfterEach {
|
|
|
|
|
|
|
|
|
|
import TestActorRefSpec._
|
|
|
|
|
|
2011-04-27 15:00:41 +02:00
|
|
|
EventHandler.start()
|
|
|
|
|
|
2011-04-16 22:20:04 +02:00
|
|
|
override def beforeEach {
|
|
|
|
|
otherthread = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def assertThread {
|
2011-05-18 17:25:30 +02:00
|
|
|
otherthread must (be(null) or equal(thread))
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"A TestActorRef must be an ActorRef, hence it" must {
|
|
|
|
|
|
|
|
|
|
"support nested Actor creation" when {
|
|
|
|
|
|
|
|
|
|
"used with TestActorRef" in {
|
|
|
|
|
val a = TestActorRef(new Actor {
|
2011-05-18 17:25:30 +02:00
|
|
|
val nested = TestActorRef(new Actor { def receive = { case _ ⇒ } }).start()
|
|
|
|
|
def receive = { case _ ⇒ self reply nested }
|
2011-04-16 22:20:04 +02:00
|
|
|
}).start()
|
|
|
|
|
a must not be (null)
|
|
|
|
|
val nested = (a !! "any").get.asInstanceOf[ActorRef]
|
|
|
|
|
nested must not be (null)
|
2011-05-18 17:25:30 +02:00
|
|
|
a must not be theSameInstanceAs(nested)
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"used with ActorRef" in {
|
|
|
|
|
val a = TestActorRef(new Actor {
|
2011-05-18 17:25:30 +02:00
|
|
|
val nested = Actor.actorOf(new Actor { def receive = { case _ ⇒ } }).start()
|
|
|
|
|
def receive = { case _ ⇒ self reply nested }
|
2011-04-16 22:20:04 +02:00
|
|
|
}).start()
|
|
|
|
|
a must not be (null)
|
|
|
|
|
val nested = (a !! "any").get.asInstanceOf[ActorRef]
|
|
|
|
|
nested must not be (null)
|
2011-05-18 17:25:30 +02:00
|
|
|
a must not be theSameInstanceAs(nested)
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"support reply via channel" in {
|
|
|
|
|
val serverRef = TestActorRef[ReplyActor].start()
|
|
|
|
|
val clientRef = TestActorRef(new SenderActor(serverRef)).start()
|
|
|
|
|
|
|
|
|
|
counter = 4
|
|
|
|
|
|
|
|
|
|
clientRef ! "complex"
|
|
|
|
|
clientRef ! "simple"
|
|
|
|
|
clientRef ! "simple"
|
|
|
|
|
clientRef ! "simple"
|
|
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
counter must be(0)
|
2011-04-16 22:20:04 +02:00
|
|
|
|
|
|
|
|
counter = 4
|
|
|
|
|
|
|
|
|
|
clientRef ! "complex2"
|
|
|
|
|
clientRef ! "simple"
|
|
|
|
|
clientRef ! "simple"
|
|
|
|
|
clientRef ! "simple"
|
|
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
counter must be(0)
|
2011-04-16 22:20:04 +02:00
|
|
|
|
|
|
|
|
assertThread
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"stop when sent a poison pill" in {
|
|
|
|
|
val a = TestActorRef[WorkerActor].start()
|
|
|
|
|
intercept[ActorKilledException] {
|
|
|
|
|
a !! PoisonPill
|
|
|
|
|
}
|
|
|
|
|
a must not be ('running)
|
2011-05-18 17:25:30 +02:00
|
|
|
a must be('shutdown)
|
2011-04-16 22:20:04 +02:00
|
|
|
assertThread
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"restart when Kill:ed" in {
|
|
|
|
|
counter = 2
|
|
|
|
|
|
|
|
|
|
val boss = TestActorRef(new TActor {
|
|
|
|
|
self.faultHandler = OneForOneStrategy(List(classOf[Throwable]), Some(2), Some(1000))
|
|
|
|
|
val ref = TestActorRef(new TActor {
|
2011-05-18 17:25:30 +02:00
|
|
|
def receiveT = { case _ ⇒ }
|
2011-04-16 22:20:04 +02:00
|
|
|
override def preRestart(reason: Throwable) { counter -= 1 }
|
|
|
|
|
override def postRestart(reason: Throwable) { counter -= 1 }
|
|
|
|
|
}).start()
|
2011-04-21 21:31:24 +02:00
|
|
|
self.dispatcher = CallingThreadDispatcher.global
|
2011-04-16 22:20:04 +02:00
|
|
|
self link ref
|
2011-05-18 17:25:30 +02:00
|
|
|
def receiveT = { case "sendKill" ⇒ ref ! Kill }
|
2011-04-16 22:20:04 +02:00
|
|
|
}).start()
|
|
|
|
|
|
|
|
|
|
boss ! "sendKill"
|
|
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
counter must be(0)
|
2011-04-16 22:20:04 +02:00
|
|
|
assertThread
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"support futures" in {
|
|
|
|
|
val a = TestActorRef[WorkerActor].start()
|
2011-05-18 17:25:30 +02:00
|
|
|
val f: Future[String] = a !!! "work"
|
|
|
|
|
f must be('completed)
|
|
|
|
|
f.get must equal("workDone")
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"A TestActorRef" must {
|
|
|
|
|
|
|
|
|
|
"allow access to internals" in {
|
|
|
|
|
val ref = TestActorRef(new TActor {
|
2011-05-18 17:25:30 +02:00
|
|
|
var s: String = _
|
2011-04-16 22:20:04 +02:00
|
|
|
def receiveT = {
|
2011-05-18 17:25:30 +02:00
|
|
|
case x: String ⇒ s = x
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
}).start()
|
|
|
|
|
ref ! "hallo"
|
|
|
|
|
val actor = ref.underlyingActor
|
2011-05-18 17:25:30 +02:00
|
|
|
actor.s must equal("hallo")
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"set receiveTimeout to None" in {
|
|
|
|
|
val a = TestActorRef[WorkerActor]
|
2011-05-18 17:25:30 +02:00
|
|
|
a.receiveTimeout must be(None)
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"set CallingThreadDispatcher" in {
|
|
|
|
|
val a = TestActorRef[WorkerActor]
|
2011-05-18 17:25:30 +02:00
|
|
|
a.dispatcher.getClass must be(classOf[CallingThreadDispatcher])
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"warn about scheduled supervisor" in {
|
2011-05-18 17:25:30 +02:00
|
|
|
val boss = Actor.actorOf(new Actor { def receive = { case _ ⇒ } }).start()
|
2011-04-16 22:20:04 +02:00
|
|
|
val ref = TestActorRef[WorkerActor].start()
|
|
|
|
|
|
|
|
|
|
val log = TestActorRef[Logger]
|
|
|
|
|
EventHandler.addListener(log)
|
|
|
|
|
boss link ref
|
|
|
|
|
val la = log.underlyingActor
|
2011-05-18 17:25:30 +02:00
|
|
|
la.count must be(1)
|
|
|
|
|
la.msg must (include("supervisor") and include("CallingThreadDispatcher"))
|
2011-04-16 22:20:04 +02:00
|
|
|
EventHandler.removeListener(log)
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-21 21:31:24 +02:00
|
|
|
"proxy isDefinedAt/apply for the underlying actor" in {
|
|
|
|
|
val ref = TestActorRef[WorkerActor].start()
|
2011-05-18 17:25:30 +02:00
|
|
|
ref.isDefinedAt("work") must be(true)
|
|
|
|
|
ref.isDefinedAt("sleep") must be(false)
|
2011-04-21 21:59:12 +02:00
|
|
|
intercept[IllegalActorStateException] { ref("work") }
|
2011-04-21 21:31:24 +02:00
|
|
|
val ch = Future.channel()
|
|
|
|
|
ref ! ch
|
|
|
|
|
val f = ch.future
|
2011-05-18 17:25:30 +02:00
|
|
|
f must be('completed)
|
|
|
|
|
f.get must be("complexReply")
|
2011-04-21 21:31:24 +02:00
|
|
|
}
|
|
|
|
|
|
2011-04-16 22:20:04 +02:00
|
|
|
}
|
2011-04-21 21:31:24 +02:00
|
|
|
}
|