From 3fff152b96846f2c732adc991e8ab8878cf3fc89 Mon Sep 17 00:00:00 2001 From: Radim Kolar Date: Wed, 8 Jul 2020 09:15:13 +0200 Subject: [PATCH] BehaviourTestKit: Add receptionist inbox (#29294) --- .../29294-receptionist-inbox.excludes | 4 ++++ .../typed/internal/ActorSystemStub.scala | 5 +++++ .../typed/internal/BehaviorTestKitImpl.scala | 3 +++ .../typed/javadsl/BehaviorTestKit.scala | 6 ++++++ .../typed/scaladsl/BehaviorTestKit.scala | 6 ++++++ .../typed/scaladsl/BehaviorTestKitSpec.scala | 19 +++++++++++++++++++ 6 files changed, 43 insertions(+) create mode 100644 akka-actor-testkit-typed/src/main/mima-filters/2.6.6.backwards.excludes/29294-receptionist-inbox.excludes diff --git a/akka-actor-testkit-typed/src/main/mima-filters/2.6.6.backwards.excludes/29294-receptionist-inbox.excludes b/akka-actor-testkit-typed/src/main/mima-filters/2.6.6.backwards.excludes/29294-receptionist-inbox.excludes new file mode 100644 index 0000000000..2521f37ae3 --- /dev/null +++ b/akka-actor-testkit-typed/src/main/mima-filters/2.6.6.backwards.excludes/29294-receptionist-inbox.excludes @@ -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") + \ No newline at end of file diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/ActorSystemStub.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/ActorSystemStub.scala index 035befd6d0..aed592c51d 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/ActorSystemStub.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/ActorSystemStub.scala @@ -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 { diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/BehaviorTestKitImpl.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/BehaviorTestKitImpl.scala index bcfce93d48..2c2f7ac93c 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/BehaviorTestKitImpl.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/BehaviorTestKitImpl.scala @@ -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 } diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/BehaviorTestKit.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/BehaviorTestKit.scala index 07b79bcac4..2faea38388 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/BehaviorTestKit.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/BehaviorTestKit.scala @@ -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] } diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKit.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKit.scala index 5eb4e71c53..7be0c21d1c 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKit.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKit.scala @@ -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] } diff --git a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKitSpec.scala b/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKitSpec.scala index 3e3471774a..ca0c252657 100644 --- a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKitSpec.scala +++ b/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKitSpec.scala @@ -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) + } + } }