From 759673569a503fda416e3563627fcc306d9babea Mon Sep 17 00:00:00 2001 From: Pritam Kadam Date: Fri, 21 Jun 2019 20:29:32 +0530 Subject: [PATCH] Adding WatchedWith effect in akka actor typed testkit #27190 (#27195) --- .../src/main/scala/akka/actor/testkit/typed/Effect.scala | 5 +++++ .../testkit/typed/internal/EffectfulActorContext.scala | 2 +- .../scala/akka/actor/testkit/typed/javadsl/Effects.scala | 5 +++++ .../scala/akka/actor/testkit/typed/scaladsl/Effects.scala | 5 +++++ .../actor/testkit/typed/javadsl/BehaviorTestKitTest.java | 8 ++++++-- .../testkit/typed/scaladsl/BehaviorTestKitSpec.scala | 5 +++-- akka-docs/src/main/paradox/typed/testing.md | 1 + 7 files changed, 26 insertions(+), 5 deletions(-) diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/Effect.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/Effect.scala index 8638c98266..f7c5797acb 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/Effect.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/Effect.scala @@ -170,6 +170,11 @@ object Effect { */ final case class Watched[T](other: ActorRef[T]) extends Effect + /** + * The behavior started watching `other`, through `context.watchWith(other, message)` + */ + final case class WatchedWith[U, T](other: ActorRef[U], message: T) extends Effect + /** * The behavior started watching `other`, through `context.unwatch(other)` */ diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/EffectfulActorContext.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/EffectfulActorContext.scala index 660db77fd1..c261bcf3b5 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/EffectfulActorContext.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/EffectfulActorContext.scala @@ -66,7 +66,7 @@ import scala.compat.java8.FunctionConverters._ super.watch(other) } override def watchWith[U](other: ActorRef[U], message: T): Unit = { - effectQueue.offer(Watched(other)) + effectQueue.offer(WatchedWith(other, message)) super.watchWith(other, message) } override def unwatch[U](other: ActorRef[U]): Unit = { diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/Effects.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/Effects.scala index 78c119e928..3b9e58f60c 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/Effects.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/Effects.scala @@ -71,6 +71,11 @@ object Effects { */ def watched[T](other: ActorRef[T]): Watched[T] = Watched(other) + /** + * The behavior started watching `other`, through `context.watchWith(other, message)` + */ + def watchedWith[U, T](other: ActorRef[U], message: T): WatchedWith[U, T] = WatchedWith(other, message) + /** * The behavior started watching `other`, through `context.unwatch(other)` */ diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/Effects.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/Effects.scala index d725b852bb..67111794b7 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/Effects.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/Effects.scala @@ -70,6 +70,11 @@ object Effects { */ def watched[T](other: ActorRef[T]): Watched[T] = Watched(other) + /** + * The behavior started watching `other`, through `context.watchWith(other, message)` + */ + def watchedWith[U, T](other: ActorRef[U], message: T): WatchedWith[U, T] = WatchedWith(other, message) + /** * The behavior started watching `other`, through `context.unwatch(other)` */ diff --git a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/BehaviorTestKitTest.java b/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/BehaviorTestKitTest.java index 869c184eac..f3a9f6659a 100644 --- a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/BehaviorTestKitTest.java +++ b/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/BehaviorTestKitTest.java @@ -338,10 +338,14 @@ public class BehaviorTestKitTest extends JUnitSuite { @Test public void recordWatchWith() { BehaviorTestKit test = BehaviorTestKit.create(behavior); - test.run(new SpawnWatchAndUnWatch("name")); + SpawnAndWatchWith spawnAndWatchWithMsg = new SpawnAndWatchWith("name"); + test.run(spawnAndWatchWithMsg); ActorRef child = test.childInbox("name").getRef(); test.expectEffectClass(Effect.Spawned.class); - assertEquals(child, test.expectEffectClass(Effect.Watched.class).other()); + + Effect.WatchedWith watchedWith = test.expectEffectClass(Effect.WatchedWith.class); + assertEquals(child, watchedWith.other()); + assertEquals(spawnAndWatchWithMsg, watchedWith.message()); } @Test 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 b1a5c011bb..b11c988d93 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 @@ -273,10 +273,11 @@ class BehaviorTestKitSpec extends WordSpec with Matchers { "record effects for watchWith" in { val testkit = BehaviorTestKit(Father.init) - testkit.run(SpawnAndWatchWith("hello")) + val spawnAndWatchWithMsg = SpawnAndWatchWith("hello") + testkit.run(spawnAndWatchWithMsg) val child = testkit.childInbox("hello").ref testkit.retrieveAllEffects() should be( - Seq(Effects.spawned(Child.initial, "hello", Props.empty), Effects.watched(child))) + Seq(Effects.spawned(Child.initial, "hello", Props.empty), Effects.watchedWith(child, spawnAndWatchWithMsg))) } } diff --git a/akka-docs/src/main/paradox/typed/testing.md b/akka-docs/src/main/paradox/typed/testing.md index f9516cc87e..28b6bc39e4 100644 --- a/akka-docs/src/main/paradox/typed/testing.md +++ b/akka-docs/src/main/paradox/typed/testing.md @@ -127,6 +127,7 @@ The @apidoc[BehaviorTestKit] keeps track other effects you can verify, look at t * SpawnedAdapter * Stopped * Watched + * WatchedWith * Unwatched * Scheduled