2023-01-08 17:13:31 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
|
* license agreements; and to You under the Apache License, version 2.0:
|
|
|
|
|
|
*
|
|
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
*
|
|
|
|
|
|
* This file is part of the Apache Pekko project, derived from Akka.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2018-10-29 17:19:37 +08:00
|
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
|
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
2012-06-25 19:30:13 +02:00
|
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
|
package jdocs.testkit;
|
2012-06-25 19:30:13 +02:00
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
2022-11-12 10:21:24 +01:00
|
|
|
|
import org.apache.pekko.pattern.Patterns;
|
2017-03-16 09:30:00 +01:00
|
|
|
|
import jdocs.AbstractJavaTest;
|
2017-03-02 11:55:03 +01:00
|
|
|
|
import org.junit.Assert;
|
2022-11-12 10:21:24 +01:00
|
|
|
|
import org.apache.pekko.japi.JavaPartialFunction;
|
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.CallingThreadDispatcher;
|
|
|
|
|
|
import org.apache.pekko.testkit.TestActor;
|
|
|
|
|
|
import org.apache.pekko.testkit.TestActorRef;
|
|
|
|
|
|
import org.apache.pekko.testkit.TestProbe;
|
|
|
|
|
|
import org.apache.pekko.testkit.javadsl.EventFilter;
|
|
|
|
|
|
import org.apache.pekko.testkit.javadsl.TestKit;
|
2013-05-02 17:12:36 +02:00
|
|
|
|
import org.junit.ClassRule;
|
2012-06-25 19:30:13 +02:00
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
import com.typesafe.config.ConfigFactory;
|
|
|
|
|
|
|
2022-11-12 10:21:24 +01:00
|
|
|
|
import org.apache.pekko.actor.AbstractActorWithTimers;
|
|
|
|
|
|
import org.apache.pekko.actor.ActorKilledException;
|
|
|
|
|
|
import org.apache.pekko.actor.ActorRef;
|
|
|
|
|
|
import org.apache.pekko.actor.ActorSystem;
|
|
|
|
|
|
import org.apache.pekko.actor.Kill;
|
|
|
|
|
|
import org.apache.pekko.actor.PoisonPill;
|
|
|
|
|
|
import org.apache.pekko.actor.Props;
|
|
|
|
|
|
import org.apache.pekko.actor.Terminated;
|
|
|
|
|
|
import org.apache.pekko.actor.AbstractActor;
|
|
|
|
|
|
import org.apache.pekko.testkit.TestActor.AutoPilot;
|
2017-03-17 03:02:47 +08:00
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
2012-06-25 19:30:13 +02:00
|
|
|
|
|
2017-03-14 15:51:44 +05:00
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
2018-05-29 18:17:48 +08:00
|
|
|
|
import java.time.Duration;
|
2017-03-14 15:51:44 +05:00
|
|
|
|
|
2017-03-02 11:55:03 +01:00
|
|
|
|
public class TestKitDocTest extends AbstractJavaTest {
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2013-05-02 17:12:36 +02: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-05-02 17:12:36 +02:00
|
|
|
|
|
|
|
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-actor-ref
|
2017-02-04 11:51:30 +05:00
|
|
|
|
static class MyActor extends AbstractActor {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"say42",
|
|
|
|
|
|
message -> {
|
|
|
|
|
|
getSender().tell(42, getSelf());
|
|
|
|
|
|
})
|
|
|
|
|
|
.match(
|
|
|
|
|
|
Exception.class,
|
|
|
|
|
|
(Exception ex) -> {
|
|
|
|
|
|
throw ex;
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean testMe() {
|
|
|
|
|
|
return true;
|
2012-06-25 19:30:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateTestActorRef() {
|
2013-04-14 22:56:41 +02:00
|
|
|
|
final Props props = Props.create(MyActor.class);
|
2012-06-29 14:42:11 +02:00
|
|
|
|
final TestActorRef<MyActor> ref = TestActorRef.create(system, props, "testA");
|
2012-06-25 19:30:13 +02:00
|
|
|
|
final MyActor actor = ref.underlyingActor();
|
|
|
|
|
|
assertTrue(actor.testMe());
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-actor-ref
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #timer
|
2018-02-13 19:29:30 +01:00
|
|
|
|
static class TestTimerActor extends AbstractActorWithTimers {
|
|
|
|
|
|
private static Object SCHED_KEY = "SchedKey";
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
|
|
|
|
|
static final class TriggerScheduling {}
|
|
|
|
|
|
|
|
|
|
|
|
static final class ScheduledMessage {}
|
|
|
|
|
|
|
2018-02-13 19:29:30 +01:00
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
return receiveBuilder().match(TriggerScheduling.class, msg -> triggerScheduling()).build();
|
2018-02-13 19:29:30 +01:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2018-02-13 19:29:30 +01:00
|
|
|
|
void triggerScheduling() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getTimers().startSingleTimer(SCHED_KEY, new ScheduledMessage(), Duration.ofMillis(500));
|
2018-02-13 19:29:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #timer
|
2018-02-13 19:29:30 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateAsk() throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-behavior
|
2013-04-14 22:56:41 +02:00
|
|
|
|
final Props props = Props.create(MyActor.class);
|
2012-06-29 14:42:11 +02:00
|
|
|
|
final TestActorRef<MyActor> ref = TestActorRef.create(system, props, "testB");
|
2019-01-12 04:00:53 +08:00
|
|
|
|
final CompletableFuture<Object> future =
|
|
|
|
|
|
Patterns.ask(ref, "say42", Duration.ofMillis(3000)).toCompletableFuture();
|
2017-03-14 15:51:44 +05:00
|
|
|
|
assertTrue(future.isDone());
|
|
|
|
|
|
assertEquals(42, future.get());
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-behavior
|
2012-06-25 19:30:13 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateExceptions() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-expecting-exceptions
|
2013-04-14 22:56:41 +02:00
|
|
|
|
final Props props = Props.create(MyActor.class);
|
2012-06-29 14:42:11 +02:00
|
|
|
|
final TestActorRef<MyActor> ref = TestActorRef.create(system, props, "myActor");
|
2012-06-25 19:30:13 +02:00
|
|
|
|
try {
|
|
|
|
|
|
ref.receive(new Exception("expected"));
|
2017-03-02 11:55:03 +01:00
|
|
|
|
Assert.fail("expected an exception to be thrown");
|
2012-06-25 19:30:13 +02:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
assertEquals("expected", e.getMessage());
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-expecting-exceptions
|
2012-06-25 19:30:13 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateWithin() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-within
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
|
|
|
|
|
within(
|
|
|
|
|
|
Duration.ZERO,
|
|
|
|
|
|
Duration.ofSeconds(1),
|
|
|
|
|
|
() -> {
|
|
|
|
|
|
assertEquals((Integer) 42, expectMsgClass(Integer.class));
|
|
|
|
|
|
return null;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-within
|
2012-06-25 19:30:13 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
@Test
|
2012-06-29 14:42:11 +02:00
|
|
|
|
public void demonstrateExpectMsg() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-expectmsg
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
|
|
|
|
|
final String out =
|
|
|
|
|
|
expectMsgPF(
|
|
|
|
|
|
"match hint",
|
|
|
|
|
|
in -> {
|
|
|
|
|
|
if (in instanceof Integer) {
|
|
|
|
|
|
return "match";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw JavaPartialFunction.noMatch();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
assertEquals("match", out);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-expectmsg
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateReceiveWhile() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-receivewhile
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
|
|
|
|
|
getRef().tell(43, ActorRef.noSender());
|
|
|
|
|
|
getRef().tell("hello", ActorRef.noSender());
|
|
|
|
|
|
|
|
|
|
|
|
final List<String> out =
|
|
|
|
|
|
receiveWhile(
|
|
|
|
|
|
Duration.ofSeconds(1),
|
|
|
|
|
|
in -> {
|
|
|
|
|
|
if (in instanceof Integer) {
|
|
|
|
|
|
return in.toString();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw JavaPartialFunction.noMatch();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
assertArrayEquals(new String[] {"42", "43"}, out.toArray());
|
|
|
|
|
|
expectMsgEquals("hello");
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-receivewhile
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
// #test-receivewhile-full
|
|
|
|
|
|
receiveWhile(
|
|
|
|
|
|
Duration.ofMillis(100),
|
|
|
|
|
|
Duration.ofMillis(50),
|
|
|
|
|
|
12,
|
|
|
|
|
|
in -> {
|
|
|
|
|
|
// #match-elided
|
|
|
|
|
|
throw JavaPartialFunction.noMatch();
|
|
|
|
|
|
// #match-elided
|
|
|
|
|
|
});
|
|
|
|
|
|
// #test-receivewhile-full
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateAwaitCond() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-awaitCond
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
|
|
|
|
|
awaitCond(Duration.ofSeconds(1), Duration.ofMillis(100), this::msgAvailable);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-awaitCond
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-22 18:33:14 +01:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateAwaitAssert() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-awaitAssert
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
|
|
|
|
|
awaitAssert(
|
|
|
|
|
|
Duration.ofSeconds(1),
|
|
|
|
|
|
Duration.ofMillis(100),
|
|
|
|
|
|
() -> {
|
|
|
|
|
|
assertEquals(msgAvailable(), true);
|
|
|
|
|
|
return null;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-awaitAssert
|
2013-03-22 18:33:14 +01:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
2019-01-12 04:00:53 +08:00
|
|
|
|
@SuppressWarnings({"unchecked", "unused"}) // due to generic varargs
|
2012-06-29 14:42:11 +02:00
|
|
|
|
public void demonstrateExpect() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
getRef().tell("hello", ActorRef.noSender());
|
|
|
|
|
|
getRef().tell("hello", ActorRef.noSender());
|
|
|
|
|
|
getRef().tell("hello", ActorRef.noSender());
|
|
|
|
|
|
getRef().tell("world", ActorRef.noSender());
|
|
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
|
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
|
|
|
|
|
// #test-expect
|
|
|
|
|
|
final String hello = expectMsgEquals("hello");
|
|
|
|
|
|
final String any = expectMsgAnyOf("hello", "world");
|
|
|
|
|
|
final List<String> all = expectMsgAllOf("hello", "world");
|
|
|
|
|
|
final int i = expectMsgClass(Integer.class);
|
|
|
|
|
|
final Number j = expectMsgAnyClassOf(Integer.class, Long.class);
|
|
|
|
|
|
expectNoMessage();
|
|
|
|
|
|
// #test-expect
|
|
|
|
|
|
getRef().tell("receveN-1", ActorRef.noSender());
|
|
|
|
|
|
getRef().tell("receveN-2", ActorRef.noSender());
|
|
|
|
|
|
// #test-expect
|
|
|
|
|
|
final List<Object> two = receiveN(2);
|
|
|
|
|
|
// #test-expect
|
|
|
|
|
|
assertEquals("hello", hello);
|
|
|
|
|
|
assertEquals("hello", any);
|
|
|
|
|
|
assertEquals(42, i);
|
|
|
|
|
|
assertEquals(42, j);
|
|
|
|
|
|
assertArrayEquals(new String[] {"hello", "world"}, all.toArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateIgnoreMsg() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-ignoreMsg
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
// ignore all Strings
|
|
|
|
|
|
ignoreMsg(msg -> msg instanceof String);
|
|
|
|
|
|
getRef().tell("hello", ActorRef.noSender());
|
|
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
|
|
|
|
|
expectMsgEquals(42);
|
|
|
|
|
|
// remove message filter
|
|
|
|
|
|
ignoreNoMsg();
|
|
|
|
|
|
getRef().tell("hello", ActorRef.noSender());
|
|
|
|
|
|
expectMsgEquals("hello");
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-ignoreMsg
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateDilated() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #duration-dilation
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
final Duration original = Duration.ofSeconds(1);
|
|
|
|
|
|
final Duration stretched = dilated(original);
|
|
|
|
|
|
assertTrue("dilated", stretched.compareTo(original) >= 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #duration-dilation
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateProbe() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-probe
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
// simple actor which only forwards messages
|
|
|
|
|
|
class Forwarder extends AbstractActor {
|
|
|
|
|
|
final ActorRef target;
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
|
public Forwarder(ActorRef target) {
|
|
|
|
|
|
this.target = target;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
|
.matchAny(message -> target.forward(message, getContext()))
|
|
|
|
|
|
.build();
|
|
|
|
|
|
}
|
2013-04-14 22:56:41 +02:00
|
|
|
|
}
|
2018-02-13 19:29:30 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// create a test probe
|
|
|
|
|
|
final TestKit probe = new TestKit(system);
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// create a forwarder, injecting the probe’s testActor
|
|
|
|
|
|
final Props props = Props.create(Forwarder.class, this, probe.getRef());
|
|
|
|
|
|
final ActorRef forwarder = system.actorOf(props, "forwarder");
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// verify correct forwarding
|
|
|
|
|
|
forwarder.tell(42, getRef());
|
|
|
|
|
|
probe.expectMsgEquals(42);
|
|
|
|
|
|
assertEquals(getRef(), probe.getLastSender());
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-probe
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2014-12-04 22:42:40 +01:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateTestProbeWithCustomName() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-probe-with-custom-name
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
final TestProbe worker = new TestProbe(system, "worker");
|
|
|
|
|
|
final TestProbe aggregator = new TestProbe(system, "aggregator");
|
|
|
|
|
|
|
|
|
|
|
|
assertTrue(worker.ref().path().name().startsWith("worker"));
|
|
|
|
|
|
assertTrue(aggregator.ref().path().name().startsWith("aggregator"));
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-probe-with-custom-name
|
2014-12-04 22:42:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateSpecialProbe() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-special-probe
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
class MyProbe extends TestKit {
|
|
|
|
|
|
public MyProbe() {
|
|
|
|
|
|
super(system);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void assertHello() {
|
|
|
|
|
|
expectMsgEquals("hello");
|
|
|
|
|
|
}
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
final MyProbe probe = new MyProbe();
|
|
|
|
|
|
probe.getRef().tell("hello", ActorRef.noSender());
|
|
|
|
|
|
probe.assertHello();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-special-probe
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2013-01-23 23:38:22 +01:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateWatch() {
|
2013-04-14 22:56:41 +02:00
|
|
|
|
final ActorRef target = system.actorOf(Props.create(MyActor.class));
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-probe-watch
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
final TestKit probe = new TestKit(system);
|
|
|
|
|
|
probe.watch(target);
|
|
|
|
|
|
target.tell(PoisonPill.getInstance(), ActorRef.noSender());
|
|
|
|
|
|
final Terminated msg = probe.expectMsgClass(Terminated.class);
|
|
|
|
|
|
assertEquals(msg.getActor(), target);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-probe-watch
|
2013-01-23 23:38:22 +01:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateReply() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-probe-reply
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
final TestKit probe = new TestKit(system);
|
|
|
|
|
|
probe.getRef().tell("hello", getRef());
|
|
|
|
|
|
probe.expectMsgEquals("hello");
|
|
|
|
|
|
probe.reply("world");
|
|
|
|
|
|
expectMsgEquals("world");
|
|
|
|
|
|
assertEquals(probe.getRef(), getLastSender());
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-probe-reply
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateForward() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-probe-forward
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
final TestKit probe = new TestKit(system);
|
|
|
|
|
|
probe.getRef().tell("hello", getRef());
|
|
|
|
|
|
probe.expectMsgEquals("hello");
|
|
|
|
|
|
probe.forward(getRef());
|
|
|
|
|
|
expectMsgEquals("hello");
|
|
|
|
|
|
assertEquals(getRef(), getLastSender());
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-probe-forward
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2018-02-13 19:29:30 +01:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateUsingInheritenceToTestTimers() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #timer-test
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
final TestKit probe = new TestKit(system);
|
|
|
|
|
|
final ActorRef target =
|
|
|
|
|
|
system.actorOf(
|
|
|
|
|
|
Props.create(
|
|
|
|
|
|
TestTimerActor.class,
|
|
|
|
|
|
() ->
|
|
|
|
|
|
new TestTimerActor() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
void triggerScheduling() {
|
|
|
|
|
|
probe.getRef().tell(new ScheduledMessage(), getSelf());
|
|
|
|
|
|
}
|
|
|
|
|
|
}));
|
|
|
|
|
|
target.tell(new TestTimerActor.TriggerScheduling(), ActorRef.noSender());
|
|
|
|
|
|
probe.expectMsgClass(TestTimerActor.ScheduledMessage.class);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #timer-test
|
2018-02-13 19:29:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateWithinProbe() {
|
|
|
|
|
|
try {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-within-probe
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
final TestKit probe = new TestKit(system);
|
|
|
|
|
|
within(Duration.ofSeconds(1), () -> probe.expectMsgEquals("hello"));
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-within-probe
|
2012-06-29 14:42:11 +02:00
|
|
|
|
} catch (AssertionError e) {
|
|
|
|
|
|
// expected to fail
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateAutoPilot() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-auto-pilot
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
final TestKit probe = new TestKit(system);
|
|
|
|
|
|
// install auto-pilot
|
|
|
|
|
|
probe.setAutoPilot(
|
|
|
|
|
|
new TestActor.AutoPilot() {
|
|
|
|
|
|
public AutoPilot run(ActorRef sender, Object msg) {
|
|
|
|
|
|
sender.tell(msg, ActorRef.noSender());
|
|
|
|
|
|
return noAutoPilot();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
// first one is replied to directly ...
|
|
|
|
|
|
probe.getRef().tell("hello", getRef());
|
|
|
|
|
|
expectMsgEquals("hello");
|
|
|
|
|
|
// ... but then the auto-pilot switched itself off
|
|
|
|
|
|
probe.getRef().tell("world", getRef());
|
|
|
|
|
|
expectNoMessage();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-auto-pilot
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
// only compilation
|
|
|
|
|
|
public void demonstrateCTD() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #calling-thread-dispatcher
|
|
|
|
|
|
system.actorOf(Props.create(MyActor.class).withDispatcher(CallingThreadDispatcher.Id()));
|
|
|
|
|
|
// #calling-thread-dispatcher
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateEventFilter() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #test-event-filter
|
|
|
|
|
|
new TestKit(system) {
|
|
|
|
|
|
{
|
|
|
|
|
|
assertEquals("TestKitDocTest", system.name());
|
|
|
|
|
|
final ActorRef victim = system.actorOf(Props.empty(), "victim");
|
|
|
|
|
|
|
|
|
|
|
|
final int result =
|
|
|
|
|
|
new EventFilter(ActorKilledException.class, system)
|
2023-02-22 12:48:15 +01:00
|
|
|
|
.from("pekko://TestKitDocTest/user/victim")
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.occurrences(1)
|
|
|
|
|
|
.intercept(
|
|
|
|
|
|
() -> {
|
|
|
|
|
|
victim.tell(Kill.getInstance(), ActorRef.noSender());
|
|
|
|
|
|
return 42;
|
|
|
|
|
|
});
|
|
|
|
|
|
assertEquals(42, result);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
// #test-event-filter
|
2012-06-25 19:30:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|