BehaviourTestKit: Add receptionist inbox (#29294)

This commit is contained in:
Radim Kolar 2020-07-08 09:15:13 +02:00 committed by GitHub
parent 43e6c94f47
commit 3fff152b96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 0 deletions

View file

@ -0,0 +1,4 @@
# #29294 Add receptionist inbox to BehaviourTestKit
ProblemFilters.exclude[ReversedMissingMethodProblem]("akka.actor.testkit.typed.javadsl.BehaviorTestKit.receptionistInbox")
ProblemFilters.exclude[ReversedMissingMethodProblem]("akka.actor.testkit.typed.scaladsl.BehaviorTestKit.receptionistInbox")

View file

@ -29,6 +29,7 @@ import akka.actor.typed.Scheduler
import akka.actor.typed.Settings
import akka.actor.typed.internal.ActorRefImpl
import akka.actor.typed.internal.InternalRecipientRef
import akka.actor.typed.receptionist.Receptionist
import akka.annotation.InternalApi
/**
@ -78,6 +79,10 @@ import akka.annotation.InternalApi
override def ignoreRef[U]: ActorRef[U] = deadLettersInbox
val receptionistInbox = new TestInboxImpl[Receptionist.Command](path.parent / "receptionist")
override def receptionist: ActorRef[Receptionist.Command] = receptionistInbox.ref
val controlledExecutor = new ControlledExecutor
implicit override def executionContext: scala.concurrent.ExecutionContextExecutor = controlledExecutor
override def dispatchers: akka.actor.typed.Dispatchers = new Dispatchers {

View file

@ -16,6 +16,7 @@ import akka.actor.ActorPath
import akka.actor.testkit.typed.{ CapturedLogEvent, Effect }
import akka.actor.testkit.typed.Effect._
import akka.actor.typed.{ ActorRef, Behavior, PostStop, Signal }
import akka.actor.typed.receptionist.Receptionist
import akka.annotation.InternalApi
import akka.util.ccompat.JavaConverters._
@ -156,4 +157,6 @@ private[akka] final class BehaviorTestKitImpl[T](_path: ActorPath, _initialBehav
override def logEntries(): immutable.Seq[CapturedLogEvent] = context.logEntries
override def clearLog(): Unit = context.clearLog()
override def receptionistInbox(): TestInboxImpl[Receptionist.Command] = context.system.receptionistInbox
}

View file

@ -9,6 +9,7 @@ import java.util.concurrent.ThreadLocalRandom
import akka.actor.testkit.typed.{ CapturedLogEvent, Effect }
import akka.actor.testkit.typed.internal.BehaviorTestKitImpl
import akka.actor.typed.{ ActorRef, Behavior, Signal }
import akka.actor.typed.receptionist.Receptionist
import akka.annotation.{ ApiMayChange, DoNotInherit }
object BehaviorTestKit {
@ -142,4 +143,9 @@ abstract class BehaviorTestKit[T] {
* Clear the log entries
*/
def clearLog(): Unit
/**
* The receptionist inbox contains messages sent to `system.receptionist`
*/
def receptionistInbox(): TestInbox[Receptionist.Command]
}

View file

@ -12,6 +12,7 @@ import scala.reflect.ClassTag
import akka.actor.testkit.typed.{ CapturedLogEvent, Effect }
import akka.actor.testkit.typed.internal.BehaviorTestKitImpl
import akka.actor.typed.{ ActorRef, Behavior, Signal, TypedActorContext }
import akka.actor.typed.receptionist.Receptionist
import akka.annotation.{ ApiMayChange, DoNotInherit }
@ApiMayChange
@ -144,4 +145,9 @@ trait BehaviorTestKit[T] {
* Clear the log entries
*/
def clearLog(): Unit
/**
* The receptionist inbox contains messages sent to `system.receptionist`
*/
def receptionistInbox(): TestInbox[Receptionist.Command]
}

View file

@ -17,6 +17,7 @@ import akka.actor.testkit.typed.Effect._
import akka.actor.testkit.typed.scaladsl.BehaviorTestKitSpec.{ Child, Parent }
import akka.actor.testkit.typed.scaladsl.BehaviorTestKitSpec.Parent._
import akka.actor.typed.{ ActorRef, Behavior, Props }
import akka.actor.typed.receptionist.{ Receptionist, ServiceKey }
import akka.actor.typed.scaladsl.Behaviors
object BehaviorTestKitSpec {
@ -40,6 +41,7 @@ object BehaviorTestKitSpec {
case class SpawnSession(replyTo: ActorRef[ActorRef[String]], sessionHandler: ActorRef[String]) extends Command
case class KillSession(session: ActorRef[String], replyTo: ActorRef[Done]) extends Command
case class Log(what: String) extends Command
case class RegisterWithReceptionist(name: String) extends Command
val init: Behavior[Command] = Behaviors.receive[Command] { (context, message) =>
message match {
@ -105,6 +107,9 @@ object BehaviorTestKitSpec {
case Log(what) =>
context.log.info(what)
Behaviors.same
case RegisterWithReceptionist(name: String) =>
context.system.receptionist ! Receptionist.Register(ServiceKey[Command](name), context.self)
Behaviors.same
}
}
}
@ -328,4 +333,18 @@ class BehaviorTestKitSpec extends AnyWordSpec with Matchers with LogCapturing {
child.childName shouldBe newChild.childName
}
}
"BehaviorTestKit's receptionist support" must {
"register with receptionist without crash" in {
val testkit = BehaviorTestKit[Parent.Command](Parent.init)
testkit.run(RegisterWithReceptionist("aladin"))
}
"capture Register message in receptionist's inbox" in {
val testkit = BehaviorTestKit[Parent.Command](Parent.init)
testkit.receptionistInbox().hasMessages should equal(false)
testkit.run(RegisterWithReceptionist("aladin"))
testkit.receptionistInbox().hasMessages should equal(true)
testkit.receptionistInbox().expectMessage(Receptionist.Register(ServiceKey[Command]("aladin"), testkit.ref))
testkit.receptionistInbox().hasMessages should equal(false)
}
}
}