Skip to content

Commit

Permalink
not anonymous actor class, #32128
Browse files Browse the repository at this point in the history
  • Loading branch information
patriknw committed Sep 27, 2023
1 parent 4eadf48 commit a53a8c9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
33 changes: 23 additions & 10 deletions akka-actor-tests/src/test/scala/akka/actor/FSMActorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,28 @@ class FSMActorSpec extends AkkaSpec(Map("akka.actor.debug.fsm" -> true)) with Im

"run onTermination upon ActorRef.stop()" in {
val started = TestLatch(1)
/*
* This lazy val trick is beyond evil: KIDS, DON'T TRY THIS AT HOME!
* It is necessary here because of the path-dependent type fsm.StopEvent.
*/
lazy val fsm = new Actor with FSM[Int, Null] {
override def preStart() = { started.countDown() }

// can't be anonymous class due to https://github.com/akka/akka/issues/32128
class FsmActor extends Actor with FSM[Int, Null] {
override def preStart() = {
started.countDown()
}

startWith(1, null)
when(1) { FSM.NullFunction }
when(1) {
FSM.NullFunction
}
onTermination {
case x => testActor ! x
}
}

/*
* This lazy val trick is beyond evil: KIDS, DON'T TRY THIS AT HOME!
* It is necessary here because of the path-dependent type fsm.StopEvent.
*/
lazy val fsm = new FsmActor

val ref = system.actorOf(Props(fsm))
Await.ready(started, timeout.duration)
system.stop(ref)
Expand All @@ -214,8 +224,8 @@ class FSMActorSpec extends AkkaSpec(Map("akka.actor.debug.fsm" -> true)) with Im
"cancel all timers when terminated" in {
val timerNames = List("timer-1", "timer-2", "timer-3")

// Lazy so fsmref can refer to checkTimersActive
lazy val fsmref = TestFSMRef(new Actor with FSM[String, Null] {
// can't be anonymous class due to https://github.com/akka/akka/issues/32128
class FsmActor extends Actor with FSM[String, Null] {
startWith("not-started", null)
when("not-started") {
case Event("start", _) => goto("started").replying("starting")
Expand All @@ -233,7 +243,10 @@ class FSMActorSpec extends AkkaSpec(Map("akka.actor.debug.fsm" -> true)) with Im
testActor ! "stopped"
}
}
})
}

// Lazy so fsmref can refer to checkTimersActive
lazy val fsmref = TestFSMRef(new FsmActor)

def checkTimersActive(active: Boolean): Unit = {
for (timer <- timerNames) fsmref.isTimerActive(timer) should ===(active)
Expand Down
33 changes: 26 additions & 7 deletions akka-actor-tests/src/test/scala/akka/actor/SupervisorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -219,27 +219,46 @@ class SupervisorSpec

"restart properly when same instance is returned" in {
val restarts = 3 //max number of restarts
lazy val childInstance = new Actor {

// can't be anonymous class due to https://github.com/akka/akka/issues/32128
class ChildActor extends Actor {
var preRestarts = 0
var postRestarts = 0
var preStarts = 0
var postStops = 0

override def preRestart(reason: Throwable, message: Option[Any]): Unit = {
preRestarts += 1; testActor ! ("preRestart" + preRestarts)
preRestarts += 1;
testActor ! ("preRestart" + preRestarts)
}

override def postRestart(reason: Throwable): Unit = {
postRestarts += 1; testActor ! ("postRestart" + postRestarts)
postRestarts += 1;
testActor ! ("postRestart" + postRestarts)
}

override def preStart(): Unit = {
preStarts += 1; testActor ! ("preStart" + preStarts)
}

override def postStop(): Unit = {
postStops += 1; testActor ! ("postStop" + postStops)
}
override def preStart(): Unit = { preStarts += 1; testActor ! ("preStart" + preStarts) }
override def postStop(): Unit = { postStops += 1; testActor ! ("postStop" + postStops) }

def receive = {
case "crash" => { testActor ! "crashed"; throw new RuntimeException("Expected") }
case "ping" => sender() ! "pong"
case "crash" => {
testActor ! "crashed"; throw new RuntimeException("Expected")
}
case "ping" => sender() ! "pong"
}
}

lazy val childInstance = new ChildActor

val master = system.actorOf(Props(new Actor {
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = restarts)(List(classOf[Exception]))
val child = context.actorOf(Props(childInstance))

def receive = {
case msg => child.forward(msg)
}
Expand Down

0 comments on commit a53a8c9

Please sign in to comment.