Don't use TestActorRef for async testing #23445

This commit is contained in:
Johan Andrén 2017-10-11 15:29:02 +02:00 committed by GitHub
parent ff5e768e16
commit 4b654d637f
2 changed files with 21 additions and 35 deletions

View file

@ -3,17 +3,13 @@
*/
package jdocs.testkit;
import static org.junit.Assert.*;
import akka.actor.*;
import akka.japi.Creator;
import akka.japi.Function;
import akka.testkit.AkkaJUnitActorSystemResource;
import akka.testkit.TestActorRef;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
import com.typesafe.config.ConfigFactory;
import docs.testkit.MockedChild;
import jdocs.AbstractJavaTest;
import org.junit.ClassRule;
@ -76,17 +72,17 @@ public class ParentChildTest extends AbstractJavaTest {
//#test-dependentparent
class DependentParent extends AbstractActor {
final ActorRef child;
boolean ponged = false;
public DependentParent(Props childProps) {
final ActorRef probe;
public DependentParent(Props childProps, ActorRef probe) {
child = getContext().actorOf(childProps, "child");
this.probe = probe;
}
@Override
public Receive createReceive() {
return receiveBuilder()
.matchEquals("pingit", message -> child.tell("ping", getSelf()))
.matchEquals("pong", message -> ponged = true)
.matchEquals("pong", message -> probe.tell("ponged", getSelf()))
.build();
}
}
@ -123,14 +119,13 @@ public class ParentChildTest extends AbstractJavaTest {
@Test
public void testingWithCustomProps() {
TestProbe probe = new TestProbe(system);
Props childProps = Props.create(MockedChild.class);
TestActorRef<DependentParent> parent = TestActorRef.create(system, Props.create(DependentParent.class, childProps));
final TestProbe probe = new TestProbe(system);
final Props childProps = Props.create(MockedChild.class);
final ActorRef parent = system.actorOf(Props.create(DependentParent.class, childProps, probe.ref()));
probe.send(parent, "pingit");
// test some parent state change
assertTrue(parent.underlyingActor().ponged == true || parent.underlyingActor().ponged == false);
probe.expectMsg("ponged");
}
@ -138,11 +133,7 @@ public class ParentChildTest extends AbstractJavaTest {
public void testingWithChildProbe() throws Exception {
final TestProbe probe = new TestProbe(system);
//#child-maker-test
Function<ActorRefFactory, ActorRef> maker = new Function<ActorRefFactory, ActorRef>() {
@Override public ActorRef apply(ActorRefFactory param) throws Exception {
return probe.ref();
}
};
Function<ActorRefFactory, ActorRef> maker = param -> probe.ref();
ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker));
//#child-maker-test
probe.send(parent, "pingit");
@ -151,11 +142,7 @@ public class ParentChildTest extends AbstractJavaTest {
public void exampleProdActorFactoryFunction() throws Exception {
//#child-maker-prod
Function<ActorRefFactory, ActorRef> maker = new Function<ActorRefFactory, ActorRef>() {
@Override public ActorRef apply(ActorRefFactory f) throws Exception {
return f.actorOf(Props.create(Child.class));
}
};
Function<ActorRefFactory, ActorRef> maker = f -> f.actorOf(Props.create(Child.class));
ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker));
//#child-maker-prod
}

View file

@ -9,7 +9,6 @@ import akka.actor.Actor
import akka.actor.ActorRef
import akka.testkit.ImplicitSender
import akka.testkit.TestProbe
import akka.testkit.TestActorRef
import akka.actor.ActorRefFactory
import akka.testkit.TestKit
import org.scalatest.BeforeAndAfterAll
@ -45,13 +44,12 @@ class DependentChild(parent: ActorRef) extends Actor {
//#test-dependentchild
//#test-dependentparent
class DependentParent(childProps: Props) extends Actor {
class DependentParent(childProps: Props, probe: ActorRef) extends Actor {
val child = context.actorOf(childProps, "child")
var ponged = false
def receive = {
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 {
"be tested without its parent" in {
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.expectMsg("pong")
}
@ -95,10 +93,11 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase with Befor
"A DependentParent" should {
"be tested with custom props" in {
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")
// 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()
//#child-maker-test
val maker = (_: ActorRefFactory) probe.ref
val parent = system.actorOf(Props(classOf[GenericDependentParent], maker))
val parent = system.actorOf(Props(new GenericDependentParent(maker)))
//#child-maker-test
probe.send(parent, "pingit")
probe.expectMsg("ping")
@ -115,8 +114,8 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase with Befor
"demonstrate production version of child creator" in {
//#child-maker-prod
val maker = (f: ActorRefFactory) f.actorOf(Props[Child])
val parent = system.actorOf(Props(classOf[GenericDependentParent], maker))
val maker = (f: ActorRefFactory) f.actorOf(Props(new Child))
val parent = system.actorOf(Props(new GenericDependentParent(maker)))
//#child-maker-prod
}
}
@ -125,7 +124,7 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase with Befor
"A TestProbe serving as parent" should {
"test its child responses" in {
val parent = TestProbe()
val child = parent.childActorOf(Props[Child])
val child = parent.childActorOf(Props(new Child))
parent.send(child, "ping")
parent.expectMsg("pong")
}
@ -137,7 +136,7 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase with Befor
"test its child responses" in {
val proxy = TestProbe()
val parent = system.actorOf(Props(new Actor {
val child = context.actorOf(Props[Child], "child")
val child = context.actorOf(Props(new Child), "child")
def receive = {
case x if sender == child proxy.ref forward x
case x child forward x