Allow restarting a named child in StubbedActorContext

This commit is contained in:
Arnout Engelen 2018-09-14 13:39:08 +01:00 committed by Patrik Nordwall
parent 176b718b2a
commit e2c443b40f
2 changed files with 24 additions and 1 deletions

View file

@ -176,7 +176,9 @@ final case class CapturedLogEvent(logLevel: LogLevel, message: String,
"Only direct children of an actor can be stopped through the actor context, " + "Only direct children of an actor can be stopped through the actor context, " +
s"but [$child] is not a child of [$self]. Stopping other actors has to be expressed as " + s"but [$child] is not a child of [$self]. Stopping other actors has to be expressed as " +
"an explicit stop message that the actor accepts.") "an explicit stop message that the actor accepts.")
else () else {
_children -= child.path.name
}
} }
override def watch[U](other: ActorRef[U]): Unit = () override def watch[U](other: ActorRef[U]): Unit = ()
override def watchWith[U](other: ActorRef[U], msg: T): Unit = () override def watchWith[U](other: ActorRef[U], msg: T): Unit = ()

View file

@ -22,10 +22,12 @@ object BehaviorTestKitSpec {
sealed trait Command sealed trait Command
case object SpawnChild extends Command
case class SpawnChildren(numberOfChildren: Int) extends Command case class SpawnChildren(numberOfChildren: Int) extends Command
case class SpawnChildrenWithProps(numberOfChildren: Int, props: Props) extends Command case class SpawnChildrenWithProps(numberOfChildren: Int, props: Props) extends Command
case class SpawnAnonymous(numberOfChildren: Int) extends Command case class SpawnAnonymous(numberOfChildren: Int) extends Command
case class SpawnAnonymousWithProps(numberOfChildren: Int, props: Props) extends Command case class SpawnAnonymousWithProps(numberOfChildren: Int, props: Props) extends Command
case class StopChild(child: ActorRef[String]) extends Command
case object SpawnAdapter extends Command case object SpawnAdapter extends Command
case class SpawnAdapterWithName(name: String) extends Command case class SpawnAdapterWithName(name: String) extends Command
case class CreateMessageAdapter[U](messageClass: Class[U], f: U Command) extends Command case class CreateMessageAdapter[U](messageClass: Class[U], f: U Command) extends Command
@ -36,6 +38,9 @@ object BehaviorTestKitSpec {
val init: Behavior[Command] = Behaviors.receive[Command] { (ctx, msg) val init: Behavior[Command] = Behaviors.receive[Command] { (ctx, msg)
msg match { msg match {
case SpawnChild
ctx.spawn(Child.initial, "child")
Behaviors.same
case SpawnChildren(numberOfChildren) if numberOfChildren > 0 case SpawnChildren(numberOfChildren) if numberOfChildren > 0
0.until(numberOfChildren).foreach { i 0.until(numberOfChildren).foreach { i
ctx.spawn(Child.initial, s"child$i") ctx.spawn(Child.initial, s"child$i")
@ -56,6 +61,9 @@ object BehaviorTestKitSpec {
ctx.spawnAnonymous(Child.initial, props) ctx.spawnAnonymous(Child.initial, props)
} }
Behaviors.same Behaviors.same
case StopChild(child)
ctx.stop(child)
Behaviors.same
case SpawnAdapter case SpawnAdapter
ctx.spawnMessageAdapter { ctx.spawnMessageAdapter {
r: Reproduce SpawnAnonymous(r.times) r: Reproduce SpawnAnonymous(r.times)
@ -280,5 +288,18 @@ class BehaviorTestKitSpec extends WordSpec with Matchers {
d.receiveAll shouldBe Seq(Done) d.receiveAll shouldBe Seq(Done)
testkit.expectEffectType[Stopped] testkit.expectEffectType[Stopped]
} }
"stop and restart a named child" in {
val testkit = BehaviorTestKit(Father.init)
testkit.run(SpawnChild)
val child = testkit.expectEffectType[Spawned[String]]
testkit.run(StopChild(child.ref))
testkit.expectEffect(Stopped(child.childName))
testkit.run(SpawnChild)
val newChild = testkit.expectEffectType[Spawned[_]]
child.childName shouldBe newChild.childName
}
} }
} }