Replacing ActorRestartSpec with ActorLifeCycleSpec
This commit is contained in:
parent
a6f53d8605
commit
fed0cf7a42
2 changed files with 60 additions and 90 deletions
|
|
@ -0,0 +1,60 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
|
||||
*/
|
||||
|
||||
package akka.actor
|
||||
|
||||
import org.scalatest.{ WordSpec, BeforeAndAfterAll, BeforeAndAfterEach }
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
|
||||
import akka.actor.Actor._
|
||||
import akka.testkit._
|
||||
import akka.util.duration._
|
||||
import java.util.concurrent.atomic._
|
||||
|
||||
object ActorLifeCycleSpec {
|
||||
}
|
||||
|
||||
class ActorLifeCycleSpec extends WordSpec with MustMatchers with TestKit with BeforeAndAfterEach {
|
||||
import ActorRestartSpec._
|
||||
|
||||
"An Actor" must {
|
||||
|
||||
"invoke preRestart, preStart, postRestart when using OneForOneStrategy" in {
|
||||
filterEvents(EventFilter[ActorKilledException] :: Nil) {
|
||||
val gen = new AtomicInteger(0)
|
||||
val supervisor = actorOf(Props(self ⇒ { case _ ⇒ }).withFaultHandler(OneForOneStrategy(List(classOf[Exception]), Some(3))))
|
||||
|
||||
val restarter = actorOf(Props(new Actor {
|
||||
val currentGen = gen.getAndIncrement()
|
||||
override def preStart() { testActor ! (("preStart", currentGen)) }
|
||||
override def postStop() { testActor ! (("postStop", currentGen)) }
|
||||
override def preRestart(reason: Throwable, message: Option[Any]) { testActor ! (("preRestart", currentGen)) }
|
||||
override def postRestart(reason: Throwable) { testActor ! (("postRestart", currentGen)) }
|
||||
def receive = { case "status" ⇒ this reply (("OK", currentGen)) }
|
||||
}).withSupervisor(supervisor))
|
||||
|
||||
expectMsg(("preStart", 0))
|
||||
restarter ! Kill
|
||||
expectMsg(("preRestart", 0))
|
||||
expectMsg(("postRestart", 1))
|
||||
restarter ! "status"
|
||||
expectMsg(("OK", 1))
|
||||
restarter ! Kill
|
||||
expectMsg(("preRestart", 1))
|
||||
expectMsg(("postRestart", 2))
|
||||
restarter ! "status"
|
||||
expectMsg(("OK", 2))
|
||||
restarter ! Kill
|
||||
expectMsg(("preRestart", 2))
|
||||
expectMsg(("postRestart", 3))
|
||||
restarter ! "status"
|
||||
expectMsg(("OK", 3))
|
||||
restarter ! Kill
|
||||
expectMsg(("postStop", 3))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
|
||||
*/
|
||||
|
||||
package akka.actor
|
||||
|
||||
import org.scalatest.{ WordSpec, BeforeAndAfterAll, BeforeAndAfterEach }
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
|
||||
import Actor.actorOf
|
||||
import akka.testkit._
|
||||
import akka.util.duration._
|
||||
|
||||
import java.util.concurrent.atomic._
|
||||
|
||||
object ActorRestartSpec {
|
||||
|
||||
private var _gen = new AtomicInteger(0)
|
||||
def generation = _gen.incrementAndGet
|
||||
def generation_=(x: Int) { _gen.set(x) }
|
||||
|
||||
sealed trait RestartType
|
||||
case object Normal extends RestartType
|
||||
case object Nested extends RestartType
|
||||
case object Handover extends RestartType
|
||||
case object Fail extends RestartType
|
||||
|
||||
class Restarter(val testActor: ActorRef) extends Actor {
|
||||
val gen = generation
|
||||
var xx = 0
|
||||
var restart: RestartType = Normal
|
||||
def receive = {
|
||||
case x: Int ⇒ xx = x
|
||||
case t: RestartType ⇒ restart = t
|
||||
case "get" ⇒ reply(xx)
|
||||
}
|
||||
override def preStart { testActor ! (("preStart", gen)) }
|
||||
override def preRestart(cause: Throwable, msg: Option[Any]) { testActor ! (("preRestart", msg, gen)) }
|
||||
override def postRestart(cause: Throwable) { testActor ! (("postRestart", gen)) }
|
||||
}
|
||||
|
||||
class Supervisor extends Actor {
|
||||
def receive = {
|
||||
case _ ⇒
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ActorRestartSpec extends WordSpec with MustMatchers with TestKit with BeforeAndAfterEach {
|
||||
import ActorRestartSpec._
|
||||
|
||||
override def beforeEach { generation = 0 }
|
||||
override def afterEach {
|
||||
val it = toStop.iterator
|
||||
while (it.hasNext) {
|
||||
try { it.next.stop() } catch { case _: akka.actor.ActorInitializationException ⇒ } //FIXME thrown because supervisor is stopped before
|
||||
it.remove
|
||||
}
|
||||
}
|
||||
|
||||
private var toStop = new java.util.concurrent.ConcurrentSkipListSet[ActorRef]
|
||||
private def collect(f: ⇒ ActorRef): ActorRef = {
|
||||
val ref = f
|
||||
toStop add ref
|
||||
ref
|
||||
}
|
||||
|
||||
private def createSupervisor =
|
||||
actorOf(Props[Supervisor].withFaultHandler(OneForOneStrategy(List(classOf[Throwable]), 5, 5000)))
|
||||
|
||||
val expectedEvents = Seq(EventFilter[ActorKilledException], EventFilter[IllegalActorStateException]("expected"))
|
||||
|
||||
"An Actor restart" must {
|
||||
|
||||
"invoke preRestart, preStart, postRestart" in {
|
||||
filterEvents(expectedEvents) {
|
||||
val supervisor = collect(createSupervisor)
|
||||
val actor = collect(actorOf(Props(new Restarter(testActor)) withSupervisor (supervisor)))
|
||||
expectMsg(1 second, ("preStart", 1))
|
||||
actor ! Kill
|
||||
within(1 second) {
|
||||
expectMsg(("preRestart", Some(Kill), 1))
|
||||
expectMsg(("postRestart", 2))
|
||||
expectNoMsg
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue