From 1fd66f5f490b32ec4300184d6672a351cab45640 Mon Sep 17 00:00:00 2001 From: Levi Ramsey Date: Mon, 11 Jan 2021 05:53:42 -0500 Subject: [PATCH] Make typed TestProbe a RecipientRef (#29928) --- .../pr-29928-testprobe-is-recipientref.excludes | 4 ++++ .../testkit/typed/internal/TestProbeImpl.scala | 15 ++++++++++++++- .../actor/testkit/typed/javadsl/TestProbe.scala | 4 +++- .../actor/testkit/typed/scaladsl/TestProbe.scala | 4 +++- akka-docs/src/main/paradox/typed/testing-async.md | 3 +++ 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 akka-actor-testkit-typed/src/main/mima-filters/2.6.10.backwards.excludes/pr-29928-testprobe-is-recipientref.excludes diff --git a/akka-actor-testkit-typed/src/main/mima-filters/2.6.10.backwards.excludes/pr-29928-testprobe-is-recipientref.excludes b/akka-actor-testkit-typed/src/main/mima-filters/2.6.10.backwards.excludes/pr-29928-testprobe-is-recipientref.excludes new file mode 100644 index 0000000000..9ceaf7a49e --- /dev/null +++ b/akka-actor-testkit-typed/src/main/mima-filters/2.6.10.backwards.excludes/pr-29928-testprobe-is-recipientref.excludes @@ -0,0 +1,4 @@ +# Changes to @DoNotInherit classes + +ProblemFilters.exclude[InheritedNewAbstractMethodProblem]("akka.actor.testkit.typed.scaladsl.TestProbe.tell") +ProblemFilters.exclude[InheritedNewAbstractMethodProblem]("akka.actor.testkit.typed.javadsl.TestProbe.tell") diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestProbeImpl.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestProbeImpl.scala index 70f902b492..efc88f8053 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestProbeImpl.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestProbeImpl.scala @@ -16,6 +16,8 @@ import scala.concurrent.duration._ import scala.reflect.ClassTag import scala.util.control.NonFatal +import akka.actor.ActorRefProvider +import akka.actor.ExtendedActorSystem import akka.actor.testkit.typed.FishingOutcome import akka.actor.testkit.typed.TestKitSettings import akka.actor.testkit.typed.javadsl.{ TestProbe => JavaTestProbe } @@ -26,6 +28,7 @@ import akka.actor.typed.ActorSystem import akka.actor.typed.Behavior import akka.actor.typed.Signal import akka.actor.typed.Terminated +import akka.actor.typed.internal.InternalRecipientRef import akka.actor.typed.scaladsl.Behaviors import akka.annotation.InternalApi import akka.japi.function.Creator @@ -63,7 +66,8 @@ private[akka] object TestProbeImpl { @InternalApi private[akka] final class TestProbeImpl[M](name: String, system: ActorSystem[_]) extends JavaTestProbe[M] - with ScalaTestProbe[M] { + with ScalaTestProbe[M] + with InternalRecipientRef[M] { import TestProbeImpl._ @@ -390,5 +394,14 @@ private[akka] final class TestProbeImpl[M](name: String, system: ActorSystem[_]) testActor.asInstanceOf[ActorRef[AnyRef]] ! Stop } + def tell(m: M) = testActor.tell(m) + + // impl InternalRecipientRef + def provider: ActorRefProvider = + system.classicSystem.asInstanceOf[ExtendedActorSystem].provider + + // impl InternalRecipientRef + def isTerminated: Boolean = false + override private[akka] def asJava: JavaTestProbe[M] = this } diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala index 620bb47f8f..2f422d88c3 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala @@ -14,6 +14,8 @@ import akka.actor.testkit.typed.TestKitSettings import akka.actor.testkit.typed.internal.TestProbeImpl import akka.actor.typed.ActorRef import akka.actor.typed.ActorSystem +import akka.actor.typed.RecipientRef +import akka.actor.typed.internal.InternalRecipientRef import akka.annotation.DoNotInherit import akka.util.unused @@ -65,7 +67,7 @@ object TestProbe { * Not for user extension */ @DoNotInherit -abstract class TestProbe[M] { +abstract class TestProbe[M] extends RecipientRef[M] { this: InternalRecipientRef[M] => implicit protected def settings: TestKitSettings diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/TestProbe.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/TestProbe.scala index 1934f1af76..ebf1554747 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/TestProbe.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/TestProbe.scala @@ -13,6 +13,8 @@ import akka.actor.testkit.typed.TestKitSettings import akka.actor.testkit.typed.internal.TestProbeImpl import akka.actor.typed.ActorRef import akka.actor.typed.ActorSystem +import akka.actor.typed.RecipientRef +import akka.actor.typed.internal.InternalRecipientRef import akka.annotation.DoNotInherit import akka.annotation.InternalApi @@ -56,7 +58,7 @@ object TestProbe { * * Not for user extension */ -@DoNotInherit trait TestProbe[M] { +@DoNotInherit trait TestProbe[M] extends RecipientRef[M] { this: InternalRecipientRef[M] => implicit protected def settings: TestKitSettings diff --git a/akka-docs/src/main/paradox/typed/testing-async.md b/akka-docs/src/main/paradox/typed/testing-async.md index d8b24596b6..a77d7c8fd5 100644 --- a/akka-docs/src/main/paradox/typed/testing-async.md +++ b/akka-docs/src/main/paradox/typed/testing-async.md @@ -49,6 +49,9 @@ The following demonstrates: * Creating a `TestProbe` * Verifying that the actor under test responds via the `TestProbe` +Note that it is possible to use a `TestProbe` directly as a @apidoc[akka.actor.typed.RecipientRef] (a common supertype of `ActorRef` and @ref:[Cluster Sharding](cluster-sharding.md) +`EntityRef`), in cases where a message protocol uses `RecipientRef` instead of specifying `ActorRef` or `EntityRef`. + Scala : @@snip [AsyncTestingExampleSpec.scala](/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/AsyncTestingExampleSpec.scala) { #test-spawn }