2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2014-2022 Lightbend Inc. <https://www.lightbend.com>
|
2013-12-17 11:23:18 -05:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.testkit;
|
2013-12-17 11:23:18 -05:00
|
|
|
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.actor.*;
|
|
|
|
|
import org.apache.pekko.japi.Creator;
|
|
|
|
|
import org.apache.pekko.japi.Function;
|
2022-12-02 14:49:16 +01:00
|
|
|
import org.apache.pekko.testkit.PekkoJUnitActorSystemResource;
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.testkit.TestProbe;
|
|
|
|
|
import org.apache.pekko.testkit.javadsl.TestKit;
|
2013-12-17 11:23:18 -05:00
|
|
|
import com.typesafe.config.ConfigFactory;
|
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
|
2022-12-02 14:49:16 +01:00
|
|
|
public static PekkoJUnitActorSystemResource actorSystemResource =
|
|
|
|
|
new PekkoJUnitActorSystemResource(
|
2019-01-12 04:00:53 +08:00
|
|
|
"TestKitDocTest",
|
2022-12-02 04:53:48 -08:00
|
|
|
ConfigFactory.parseString(
|
|
|
|
|
"pekko.loggers = [org.apache.pekko.testkit.TestEventListener]"));
|
2013-12-17 11:23:18 -05:00
|
|
|
|
|
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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()
|
2019-01-12 04:00:53 +08:00
|
|
|
.matchEquals("pingit", message -> child.tell("ping", getSelf()))
|
|
|
|
|
.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()
|
2019-01-12 04:00:53 +08:00
|
|
|
.matchEquals(
|
|
|
|
|
"ping",
|
|
|
|
|
message -> {
|
|
|
|
|
getContext().getParent().tell("pong", getSelf());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #test-example
|
2013-12-17 11:23:18 -05:00
|
|
|
|
|
|
|
|
static
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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()
|
2019-01-12 04:00:53 +08:00
|
|
|
.matchEquals("ping", message -> parent.tell("pong", getSelf()))
|
|
|
|
|
.build();
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #test-dependentchild
|
2013-12-17 11:23:18 -05:00
|
|
|
|
|
|
|
|
static
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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;
|
2017-10-11 15:29:02 +02:00
|
|
|
final ActorRef probe;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2017-10-11 15:29:02 +02:00
|
|
|
public DependentParent(Props childProps, ActorRef probe) {
|
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");
|
2017-10-11 15:29:02 +02:00
|
|
|
this.probe = probe;
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
.matchEquals("pingit", message -> child.tell("ping", getSelf()))
|
|
|
|
|
.matchEquals("pong", message -> probe.tell("ponged", getSelf()))
|
|
|
|
|
.build();
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #test-dependentparent
|
2013-12-17 11:23:18 -05:00
|
|
|
|
|
|
|
|
static
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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;
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
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()
|
2019-01-12 04:00:53 +08:00
|
|
|
.matchEquals("pingit", message -> child.tell("ping", getSelf()))
|
|
|
|
|
.matchEquals("pong", message -> ponged = true)
|
|
|
|
|
.build();
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #test-dependentparent-generic
|
2013-12-17 11:23:18 -05:00
|
|
|
|
|
|
|
|
@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() {
|
2017-10-11 15:29:02 +02:00
|
|
|
final TestProbe probe = new TestProbe(system);
|
|
|
|
|
final Props childProps = Props.create(MockedChild.class);
|
2019-01-12 04:00:53 +08:00
|
|
|
final ActorRef parent =
|
|
|
|
|
system.actorOf(Props.create(DependentParent.class, childProps, probe.ref()));
|
2013-12-17 11:23:18 -05:00
|
|
|
|
|
|
|
|
probe.send(parent, "pingit");
|
|
|
|
|
|
2017-10-11 15:29:02 +02:00
|
|
|
probe.expectMsg("ponged");
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testingWithChildProbe() throws Exception {
|
|
|
|
|
final TestProbe probe = new TestProbe(system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #child-maker-test
|
2017-10-11 15:29:02 +02:00
|
|
|
Function<ActorRefFactory, ActorRef> maker = param -> probe.ref();
|
2013-12-17 11:23:18 -05:00
|
|
|
ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker));
|
2019-01-12 04:00:53 +08:00
|
|
|
// #child-maker-test
|
2013-12-17 11:23:18 -05:00
|
|
|
probe.send(parent, "pingit");
|
|
|
|
|
probe.expectMsg("ping");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void exampleProdActorFactoryFunction() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #child-maker-prod
|
2017-10-11 15:29:02 +02:00
|
|
|
Function<ActorRefFactory, ActorRef> maker = f -> f.actorOf(Props.create(Child.class));
|
2013-12-17 11:23:18 -05:00
|
|
|
ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker));
|
2019-01-12 04:00:53 +08:00
|
|
|
// #child-maker-prod
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
2019-01-12 04:00:53 +08:00
|
|
|
// #test-fabricated-parent-creator
|
2013-12-17 11:23:18 -05:00
|
|
|
class FabricatedParentCreator implements Creator<Actor> {
|
|
|
|
|
private final TestProbe proxy;
|
|
|
|
|
|
|
|
|
|
public FabricatedParentCreator(TestProbe proxy) {
|
|
|
|
|
this.proxy = proxy;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
@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()
|
2019-01-12 04:00:53 +08:00
|
|
|
.matchAny(
|
|
|
|
|
message -> {
|
|
|
|
|
if (getSender().equals(child)) {
|
|
|
|
|
proxy.ref().forward(message, getContext());
|
|
|
|
|
} else {
|
|
|
|
|
child.forward(message, getContext());
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #test-fabricated-parent-creator
|
2013-12-17 11:23:18 -05:00
|
|
|
|
2016-09-01 11:38:07 +02:00
|
|
|
@Test
|
|
|
|
|
public void testProbeParentTest() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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));
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2016-09-01 11:38:07 +02:00
|
|
|
parent.send(child, "ping");
|
|
|
|
|
parent.expectMsgEquals("pong");
|
2019-01-12 04:00:53 +08:00
|
|
|
// #test-TestProbe-parent
|
2016-09-01 11:38:07 +02:00
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
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
|
2019-01-12 04:00:53 +08:00
|
|
|
// #test-fabricated-parent
|
2013-12-17 11:23:18 -05:00
|
|
|
TestProbe proxy = new TestProbe(system);
|
2018-10-15 18:12:41 +02:00
|
|
|
ActorRef parent = system.actorOf(Props.create(Actor.class, new FabricatedParentCreator(proxy)));
|
2013-12-17 11:23:18 -05:00
|
|
|
|
|
|
|
|
proxy.send(parent, "ping");
|
|
|
|
|
proxy.expectMsg("pong");
|
2019-01-12 04:00:53 +08:00
|
|
|
// #test-fabricated-parent
|
2013-12-17 11:23:18 -05:00
|
|
|
}
|
|
|
|
|
}
|