diff --git a/akka-typed-tests/src/test/scala/akka/typed/DeferredSpec.scala b/akka-typed-tests/src/test/scala/akka/typed/DeferredSpec.scala index 1fc798c28f..1590ada735 100644 --- a/akka-typed-tests/src/test/scala/akka/typed/DeferredSpec.scala +++ b/akka-typed-tests/src/test/scala/akka/typed/DeferredSpec.scala @@ -79,14 +79,36 @@ class DeferredSpec extends TypedSpec { def `must stop when exception from factory`(): Unit = { val probe = TestProbe[Event]("evt") - val behv = Actor.deferred[Command] { _ ⇒ - probe.ref ! Started - throw new RuntimeException("simulated exc from factory") with NoStackTrace + val behv = Actor.deferred[Command] { ctx ⇒ + val child = ctx.spawnAnonymous(Actor.deferred[Command] { _ ⇒ + probe.ref ! Started + throw new RuntimeException("simulated exc from factory") with NoStackTrace + }) + ctx.watch(child) + Actor.immutable[Command]((_, _) ⇒ Actor.same).onSignal { + case (_, Terminated(`child`)) ⇒ + probe.ref ! Pong + Actor.stopped + } } - val ref = start(behv) + start(behv) probe.expectMsg(Started) - ref ! Ping - probe.expectNoMsg(100.millis) + probe.expectMsg(Pong) + } + + def `must stop when deferred result it Stopped`(): Unit = { + val probe = TestProbe[Event]("evt") + val behv = Actor.deferred[Command] { ctx ⇒ + val child = ctx.spawnAnonymous(Actor.deferred[Command](_ ⇒ Actor.stopped)) + ctx.watch(child) + Actor.immutable[Command]((_, _) ⇒ Actor.same).onSignal { + case (_, Terminated(`child`)) ⇒ + probe.ref ! Pong + Actor.stopped + } + } + start(behv) + probe.expectMsg(Pong) } def `must create underlying when nested`(): Unit = { @@ -136,10 +158,10 @@ class DeferredSpec extends TypedSpec { } - object `A Restarter (stubbed, native)` extends StubbedTests with NativeSystem - object `A Restarter (stubbed, adapted)` extends StubbedTests with AdaptedSystem + object `A DeferredBehavior (stubbed, native)` extends StubbedTests with NativeSystem + object `A DeferredBehavior (stubbed, adapted)` extends StubbedTests with AdaptedSystem - object `A Restarter (real, native)` extends RealTests with NativeSystem - object `A Restarter (real, adapted)` extends RealTests with AdaptedSystem + object `A DeferredBehavior (real, native)` extends RealTests with NativeSystem + object `A DeferredBehavior (real, adapted)` extends RealTests with AdaptedSystem } diff --git a/akka-typed/src/main/scala/akka/typed/internal/adapter/ActorAdapter.scala b/akka-typed/src/main/scala/akka/typed/internal/adapter/ActorAdapter.scala index 81e6170473..9056334ade 100644 --- a/akka-typed/src/main/scala/akka/typed/internal/adapter/ActorAdapter.scala +++ b/akka-typed/src/main/scala/akka/typed/internal/adapter/ActorAdapter.scala @@ -75,16 +75,20 @@ import akka.util.OptionVal a.SupervisorStrategy.Stop } - override def preStart(): Unit = + override def preStart(): Unit = { behavior = validateAsInitial(undefer(behavior, ctx)) + if (!isAlive(behavior)) context.stop(self) + } override def preRestart(reason: Throwable, message: Option[Any]): Unit = { Behavior.interpretSignal(behavior, ctx, PreRestart) behavior = Behavior.stopped } - override def postRestart(reason: Throwable): Unit = + override def postRestart(reason: Throwable): Unit = { behavior = validateAsInitial(undefer(behavior, ctx)) + if (!isAlive(behavior)) context.stop(self) + } override def postStop(): Unit = { behavior match {