2013-12-17 11:23:18 -05:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2014-2017 Lightbend Inc. <http://www.lightbend.com>
|
2013-12-17 11:23:18 -05:00
|
|
|
*/
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.testkit;
|
2013-12-17 11:23:18 -05:00
|
|
|
|
|
|
|
|
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;
|
2016-09-01 11:38:07 +02:00
|
|
|
|
2017-03-17 03:02:47 +08:00
|
|
|
import akka.testkit.javadsl.TestKit;
|
2013-12-17 11:23:18 -05:00
|
|
|
import com.typesafe.config.ConfigFactory;
|
2016-09-01 11:38:07 +02:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
import docs.testkit.MockedChild;
|
|
|
|
|
import jdocs.AbstractJavaTest;
|
2013-12-17 11:23:18 -05:00
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2017-03-02 11:55:03 +01:00
|
|
|
public class ParentChildTest extends AbstractJavaTest {
|
2013-12-17 11:23:18 -05:00
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
|
|
|
|
new AkkaJUnitActorSystemResource("TestKitDocTest",
|
|
|
|
|
ConfigFactory.parseString("akka.loggers = [akka.testkit.TestEventListener]"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
|
|
|
|
|
|
|
|
|
//#test-example
|
2017-02-04 11:51:30 +05:00
|
|
|
static class Parent extends AbstractActor {
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
final ActorRef child = getContext().actorOf(Props.create(Child.class), "child");
|
2013-12-17 11:23:18 -05:00
|
|
|
boolean ponged = false;
|
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
2017-03-16 09:30:00 +01:00
|
|
|
.matchEquals("pingit", message -> child.tell("ping", getSelf()))
|
2017-02-04 11:51:30 +05:00
|
|
|
.matchEquals("pong", message -> ponged = true)
|
|
|
|
|
.build();
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
static class Child extends AbstractActor {
|
|
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.matchEquals("ping", message -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
getContext().getParent().tell("pong", getSelf());
|
2017-02-04 11:51:30 +05:00
|
|
|
})
|
|
|
|
|
.build();
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#test-example
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
//#test-dependentchild
|
2017-02-04 11:51:30 +05:00
|
|
|
class DependentChild extends AbstractActor {
|
2013-12-17 11:23:18 -05:00
|
|
|
private final ActorRef parent;
|
|
|
|
|
|
|
|
|
|
public DependentChild(ActorRef parent) {
|
|
|
|
|
this.parent = parent;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
2017-03-16 09:30:00 +01:00
|
|
|
.matchEquals("ping", message -> parent.tell("pong", getSelf()))
|
2017-02-04 11:51:30 +05:00
|
|
|
.build();
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#test-dependentchild
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
//#test-dependentparent
|
2017-02-04 11:51:30 +05:00
|
|
|
class DependentParent extends AbstractActor {
|
2013-12-17 11:23:18 -05:00
|
|
|
final ActorRef child;
|
|
|
|
|
boolean ponged = false;
|
|
|
|
|
|
|
|
|
|
public DependentParent(Props childProps) {
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
child = getContext().actorOf(childProps, "child");
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
2017-03-16 09:30:00 +01:00
|
|
|
.matchEquals("pingit", message -> child.tell("ping", getSelf()))
|
2017-02-04 11:51:30 +05:00
|
|
|
.matchEquals("pong", message -> ponged = true)
|
|
|
|
|
.build();
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#test-dependentparent
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
//#test-dependentparent-generic
|
2017-02-04 11:51:30 +05:00
|
|
|
class GenericDependentParent extends AbstractActor {
|
2013-12-17 11:23:18 -05:00
|
|
|
final ActorRef child;
|
|
|
|
|
boolean ponged = false;
|
|
|
|
|
|
|
|
|
|
public GenericDependentParent(Function<ActorRefFactory, ActorRef> childMaker)
|
|
|
|
|
throws Exception {
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
child = childMaker.apply(getContext());
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
2017-03-16 09:30:00 +01:00
|
|
|
.matchEquals("pingit", message -> child.tell("ping", getSelf()))
|
2017-02-04 11:51:30 +05:00
|
|
|
.matchEquals("pong", message -> ponged = true)
|
|
|
|
|
.build();
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#test-dependentparent-generic
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testingWithoutParent() {
|
|
|
|
|
TestProbe probe = new TestProbe(system);
|
|
|
|
|
ActorRef child = system.actorOf(Props.create(DependentChild.class, probe.ref()));
|
|
|
|
|
probe.send(child, "ping");
|
|
|
|
|
probe.expectMsg("pong");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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));
|
|
|
|
|
|
|
|
|
|
probe.send(parent, "pingit");
|
|
|
|
|
|
|
|
|
|
// test some parent state change
|
|
|
|
|
assertTrue(parent.underlyingActor().ponged == true || parent.underlyingActor().ponged == false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker));
|
|
|
|
|
//#child-maker-test
|
|
|
|
|
probe.send(parent, "pingit");
|
|
|
|
|
probe.expectMsg("ping");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker));
|
|
|
|
|
//#child-maker-prod
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
//#test-fabricated-parent-creator
|
|
|
|
|
class FabricatedParentCreator implements Creator<Actor> {
|
|
|
|
|
private final TestProbe proxy;
|
|
|
|
|
|
|
|
|
|
public FabricatedParentCreator(TestProbe proxy) {
|
|
|
|
|
this.proxy = proxy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override public Actor create() throws Exception {
|
2017-02-04 11:51:30 +05:00
|
|
|
return new AbstractActor() {
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
final ActorRef child = getContext().actorOf(Props.create(Child.class), "child");
|
2013-12-17 11:23:18 -05:00
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.matchAny(message -> {
|
2017-03-17 10:18:15 +01:00
|
|
|
if (getSender().equals(child)) {
|
2017-02-04 11:51:30 +05:00
|
|
|
proxy.ref().forward(message, getContext());
|
|
|
|
|
} else {
|
|
|
|
|
child.forward(message, getContext());
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.build();
|
|
|
|
|
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#test-fabricated-parent-creator
|
|
|
|
|
|
2016-09-01 11:38:07 +02:00
|
|
|
@Test
|
|
|
|
|
public void testProbeParentTest() throws Exception {
|
|
|
|
|
//#test-TestProbe-parent
|
2017-03-17 03:02:47 +08:00
|
|
|
TestKit parent = new TestKit(system);
|
2016-09-01 11:38:07 +02:00
|
|
|
ActorRef child = parent.childActorOf(Props.create(Child.class));
|
|
|
|
|
|
|
|
|
|
parent.send(child, "ping");
|
|
|
|
|
parent.expectMsgEquals("pong");
|
|
|
|
|
//#test-TestProbe-parent
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-17 11:23:18 -05:00
|
|
|
@Test
|
|
|
|
|
public void fabricatedParentTestsItsChildResponses() throws Exception {
|
|
|
|
|
// didn't put final on these in order to make the parent fit in one line in the html docs
|
|
|
|
|
//#test-fabricated-parent
|
|
|
|
|
TestProbe proxy = new TestProbe(system);
|
|
|
|
|
ActorRef parent = system.actorOf(Props.create(new FabricatedParentCreator(proxy)));
|
|
|
|
|
|
|
|
|
|
proxy.send(parent, "ping");
|
|
|
|
|
proxy.expectMsg("pong");
|
|
|
|
|
//#test-fabricated-parent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|