Don't use TestActorRef for async testing #23445
This commit is contained in:
parent
ff5e768e16
commit
4b654d637f
2 changed files with 21 additions and 35 deletions
|
|
@ -3,17 +3,13 @@
|
||||||
*/
|
*/
|
||||||
package jdocs.testkit;
|
package jdocs.testkit;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
import akka.actor.*;
|
import akka.actor.*;
|
||||||
import akka.japi.Creator;
|
import akka.japi.Creator;
|
||||||
import akka.japi.Function;
|
import akka.japi.Function;
|
||||||
import akka.testkit.AkkaJUnitActorSystemResource;
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
||||||
import akka.testkit.TestActorRef;
|
|
||||||
import akka.testkit.TestProbe;
|
import akka.testkit.TestProbe;
|
||||||
|
|
||||||
import akka.testkit.javadsl.TestKit;
|
import akka.testkit.javadsl.TestKit;
|
||||||
import com.typesafe.config.ConfigFactory;
|
import com.typesafe.config.ConfigFactory;
|
||||||
|
|
||||||
import docs.testkit.MockedChild;
|
import docs.testkit.MockedChild;
|
||||||
import jdocs.AbstractJavaTest;
|
import jdocs.AbstractJavaTest;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
|
|
@ -76,17 +72,17 @@ public class ParentChildTest extends AbstractJavaTest {
|
||||||
//#test-dependentparent
|
//#test-dependentparent
|
||||||
class DependentParent extends AbstractActor {
|
class DependentParent extends AbstractActor {
|
||||||
final ActorRef child;
|
final ActorRef child;
|
||||||
boolean ponged = false;
|
final ActorRef probe;
|
||||||
|
public DependentParent(Props childProps, ActorRef probe) {
|
||||||
public DependentParent(Props childProps) {
|
|
||||||
child = getContext().actorOf(childProps, "child");
|
child = getContext().actorOf(childProps, "child");
|
||||||
|
this.probe = probe;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Receive createReceive() {
|
public Receive createReceive() {
|
||||||
return receiveBuilder()
|
return receiveBuilder()
|
||||||
.matchEquals("pingit", message -> child.tell("ping", getSelf()))
|
.matchEquals("pingit", message -> child.tell("ping", getSelf()))
|
||||||
.matchEquals("pong", message -> ponged = true)
|
.matchEquals("pong", message -> probe.tell("ponged", getSelf()))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -123,14 +119,13 @@ public class ParentChildTest extends AbstractJavaTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testingWithCustomProps() {
|
public void testingWithCustomProps() {
|
||||||
TestProbe probe = new TestProbe(system);
|
final TestProbe probe = new TestProbe(system);
|
||||||
Props childProps = Props.create(MockedChild.class);
|
final Props childProps = Props.create(MockedChild.class);
|
||||||
TestActorRef<DependentParent> parent = TestActorRef.create(system, Props.create(DependentParent.class, childProps));
|
final ActorRef parent = system.actorOf(Props.create(DependentParent.class, childProps, probe.ref()));
|
||||||
|
|
||||||
probe.send(parent, "pingit");
|
probe.send(parent, "pingit");
|
||||||
|
|
||||||
// test some parent state change
|
probe.expectMsg("ponged");
|
||||||
assertTrue(parent.underlyingActor().ponged == true || parent.underlyingActor().ponged == false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -138,11 +133,7 @@ public class ParentChildTest extends AbstractJavaTest {
|
||||||
public void testingWithChildProbe() throws Exception {
|
public void testingWithChildProbe() throws Exception {
|
||||||
final TestProbe probe = new TestProbe(system);
|
final TestProbe probe = new TestProbe(system);
|
||||||
//#child-maker-test
|
//#child-maker-test
|
||||||
Function<ActorRefFactory, ActorRef> maker = new Function<ActorRefFactory, ActorRef>() {
|
Function<ActorRefFactory, ActorRef> maker = param -> probe.ref();
|
||||||
@Override public ActorRef apply(ActorRefFactory param) throws Exception {
|
|
||||||
return probe.ref();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker));
|
ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker));
|
||||||
//#child-maker-test
|
//#child-maker-test
|
||||||
probe.send(parent, "pingit");
|
probe.send(parent, "pingit");
|
||||||
|
|
@ -151,11 +142,7 @@ public class ParentChildTest extends AbstractJavaTest {
|
||||||
|
|
||||||
public void exampleProdActorFactoryFunction() throws Exception {
|
public void exampleProdActorFactoryFunction() throws Exception {
|
||||||
//#child-maker-prod
|
//#child-maker-prod
|
||||||
Function<ActorRefFactory, ActorRef> maker = new Function<ActorRefFactory, ActorRef>() {
|
Function<ActorRefFactory, ActorRef> maker = f -> f.actorOf(Props.create(Child.class));
|
||||||
@Override public ActorRef apply(ActorRefFactory f) throws Exception {
|
|
||||||
return f.actorOf(Props.create(Child.class));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker));
|
ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker));
|
||||||
//#child-maker-prod
|
//#child-maker-prod
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ import akka.actor.Actor
|
||||||
import akka.actor.ActorRef
|
import akka.actor.ActorRef
|
||||||
import akka.testkit.ImplicitSender
|
import akka.testkit.ImplicitSender
|
||||||
import akka.testkit.TestProbe
|
import akka.testkit.TestProbe
|
||||||
import akka.testkit.TestActorRef
|
|
||||||
import akka.actor.ActorRefFactory
|
import akka.actor.ActorRefFactory
|
||||||
import akka.testkit.TestKit
|
import akka.testkit.TestKit
|
||||||
import org.scalatest.BeforeAndAfterAll
|
import org.scalatest.BeforeAndAfterAll
|
||||||
|
|
@ -45,13 +44,12 @@ class DependentChild(parent: ActorRef) extends Actor {
|
||||||
//#test-dependentchild
|
//#test-dependentchild
|
||||||
|
|
||||||
//#test-dependentparent
|
//#test-dependentparent
|
||||||
class DependentParent(childProps: Props) extends Actor {
|
class DependentParent(childProps: Props, probe: ActorRef) extends Actor {
|
||||||
val child = context.actorOf(childProps, "child")
|
val child = context.actorOf(childProps, "child")
|
||||||
var ponged = false
|
|
||||||
|
|
||||||
def receive = {
|
def receive = {
|
||||||
case "pingit" ⇒ child ! "ping"
|
case "pingit" ⇒ child ! "ping"
|
||||||
case "pong" ⇒ ponged = true
|
case "pong" ⇒ probe ! "ponged"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,7 +84,7 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase with Befor
|
||||||
"A DependentChild" should {
|
"A DependentChild" should {
|
||||||
"be tested without its parent" in {
|
"be tested without its parent" in {
|
||||||
val probe = TestProbe()
|
val probe = TestProbe()
|
||||||
val child = system.actorOf(Props(classOf[DependentChild], probe.ref))
|
val child = system.actorOf(Props(new DependentChild(probe.ref)))
|
||||||
probe.send(child, "ping")
|
probe.send(child, "ping")
|
||||||
probe.expectMsg("pong")
|
probe.expectMsg("pong")
|
||||||
}
|
}
|
||||||
|
|
@ -95,10 +93,11 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase with Befor
|
||||||
"A DependentParent" should {
|
"A DependentParent" should {
|
||||||
"be tested with custom props" in {
|
"be tested with custom props" in {
|
||||||
val probe = TestProbe()
|
val probe = TestProbe()
|
||||||
val parent = TestActorRef(new DependentParent(Props[MockedChild]))
|
val childProps = Props(new MockedChild())
|
||||||
|
val parent = system.actorOf(Props(new DependentParent(childProps, probe.ref)))
|
||||||
probe.send(parent, "pingit")
|
probe.send(parent, "pingit")
|
||||||
// test some parent state change
|
// test some parent state change
|
||||||
parent.underlyingActor.ponged should (be(true) or be(false))
|
probe.expectMsg("ponged")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,7 +106,7 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase with Befor
|
||||||
val probe = TestProbe()
|
val probe = TestProbe()
|
||||||
//#child-maker-test
|
//#child-maker-test
|
||||||
val maker = (_: ActorRefFactory) ⇒ probe.ref
|
val maker = (_: ActorRefFactory) ⇒ probe.ref
|
||||||
val parent = system.actorOf(Props(classOf[GenericDependentParent], maker))
|
val parent = system.actorOf(Props(new GenericDependentParent(maker)))
|
||||||
//#child-maker-test
|
//#child-maker-test
|
||||||
probe.send(parent, "pingit")
|
probe.send(parent, "pingit")
|
||||||
probe.expectMsg("ping")
|
probe.expectMsg("ping")
|
||||||
|
|
@ -115,8 +114,8 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase with Befor
|
||||||
|
|
||||||
"demonstrate production version of child creator" in {
|
"demonstrate production version of child creator" in {
|
||||||
//#child-maker-prod
|
//#child-maker-prod
|
||||||
val maker = (f: ActorRefFactory) ⇒ f.actorOf(Props[Child])
|
val maker = (f: ActorRefFactory) ⇒ f.actorOf(Props(new Child))
|
||||||
val parent = system.actorOf(Props(classOf[GenericDependentParent], maker))
|
val parent = system.actorOf(Props(new GenericDependentParent(maker)))
|
||||||
//#child-maker-prod
|
//#child-maker-prod
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +124,7 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase with Befor
|
||||||
"A TestProbe serving as parent" should {
|
"A TestProbe serving as parent" should {
|
||||||
"test its child responses" in {
|
"test its child responses" in {
|
||||||
val parent = TestProbe()
|
val parent = TestProbe()
|
||||||
val child = parent.childActorOf(Props[Child])
|
val child = parent.childActorOf(Props(new Child))
|
||||||
parent.send(child, "ping")
|
parent.send(child, "ping")
|
||||||
parent.expectMsg("pong")
|
parent.expectMsg("pong")
|
||||||
}
|
}
|
||||||
|
|
@ -137,7 +136,7 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase with Befor
|
||||||
"test its child responses" in {
|
"test its child responses" in {
|
||||||
val proxy = TestProbe()
|
val proxy = TestProbe()
|
||||||
val parent = system.actorOf(Props(new Actor {
|
val parent = system.actorOf(Props(new Actor {
|
||||||
val child = context.actorOf(Props[Child], "child")
|
val child = context.actorOf(Props(new Child), "child")
|
||||||
def receive = {
|
def receive = {
|
||||||
case x if sender == child ⇒ proxy.ref forward x
|
case x if sender == child ⇒ proxy.ref forward x
|
||||||
case x ⇒ child forward x
|
case x ⇒ child forward x
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue