2012-06-29 14:42:11 +02:00
|
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
2012-06-25 19:30:13 +02: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.*;
|
|
|
|
|
|
|
2017-03-14 15:51:44 +05:00
|
|
|
|
import akka.pattern.PatternsCS;
|
2013-05-02 17:12:36 +02:00
|
|
|
|
import akka.testkit.*;
|
2017-03-16 09:30:00 +01:00
|
|
|
|
import jdocs.AbstractJavaTest;
|
2017-03-02 11:55:03 +01:00
|
|
|
|
import org.junit.Assert;
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorKilledException;
|
|
|
|
|
|
import akka.actor.ActorRef;
|
2012-06-25 19:30:13 +02:00
|
|
|
|
import akka.actor.ActorSystem;
|
2012-06-29 14:42:11 +02:00
|
|
|
|
import akka.actor.Kill;
|
2013-03-26 18:17:50 +01:00
|
|
|
|
import akka.actor.PoisonPill;
|
2012-06-25 19:30:13 +02:00
|
|
|
|
import akka.actor.Props;
|
2013-01-23 23:38:22 +01:00
|
|
|
|
import akka.actor.Terminated;
|
2017-02-04 11:51:30 +05:00
|
|
|
|
import akka.actor.AbstractActor;
|
2012-06-29 14:42:11 +02:00
|
|
|
|
import akka.testkit.TestActor.AutoPilot;
|
2012-10-15 16:18:52 +02:00
|
|
|
|
import scala.concurrent.duration.Duration;
|
2012-06-25 19:30:13 +02:00
|
|
|
|
|
2017-03-14 15:51:44 +05:00
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
|
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
|
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
|
|
|
|
|
new AkkaJUnitActorSystemResource("TestKitDocTest",
|
|
|
|
|
|
ConfigFactory.parseString("akka.loggers = [akka.testkit.TestEventListener]"));
|
|
|
|
|
|
|
|
|
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
|
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
//#test-actor-ref
|
2017-02-04 11:51:30 +05:00
|
|
|
|
static class MyActor extends AbstractActor {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
|
.matchEquals("say42", message -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
|
getSender().tell(42, getSelf());
|
2017-02-04 11:51:30 +05:00
|
|
|
|
})
|
|
|
|
|
|
.match(Exception.class, (Exception ex) -> {
|
|
|
|
|
|
throw ex;
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
2012-06-25 19:30:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
public boolean testMe() { return true; }
|
|
|
|
|
|
}
|
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());
|
|
|
|
|
|
}
|
|
|
|
|
|
//#test-actor-ref
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateAsk() throws Exception {
|
2012-06-29 14:42:11 +02: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");
|
2017-03-14 15:51:44 +05:00
|
|
|
|
final CompletableFuture<Object> future = PatternsCS.ask(ref, "say42", 3000).toCompletableFuture();
|
|
|
|
|
|
assertTrue(future.isDone());
|
|
|
|
|
|
assertEquals(42, future.get());
|
2012-06-29 14:42:11 +02: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() {
|
2012-06-29 14:42:11 +02: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());
|
|
|
|
|
|
}
|
2012-06-29 14:42:11 +02: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() {
|
2012-06-29 14:42:11 +02:00
|
|
|
|
//#test-within
|
|
|
|
|
|
new JavaTestKit(system) {{
|
2013-06-05 16:59:25 +02:00
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
2012-09-14 10:08:40 +02:00
|
|
|
|
new Within(Duration.Zero(), Duration.create(1, "second")) {
|
2012-06-25 19:30:13 +02:00
|
|
|
|
// do not put code outside this method, will run afterwards
|
|
|
|
|
|
public void run() {
|
|
|
|
|
|
assertEquals((Integer) 42, expectMsgClass(Integer.class));
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}};
|
2012-06-29 14:42:11 +02:00
|
|
|
|
//#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() {
|
|
|
|
|
|
//#test-expectmsg
|
|
|
|
|
|
new JavaTestKit(system) {{
|
2013-06-05 16:59:25 +02:00
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
2012-06-29 14:42:11 +02:00
|
|
|
|
final String out = new ExpectMsg<String>("match hint") {
|
|
|
|
|
|
// do not put code outside this method, will run afterwards
|
|
|
|
|
|
protected String match(Object in) {
|
|
|
|
|
|
if (in instanceof Integer) {
|
2012-06-25 19:30:13 +02:00
|
|
|
|
return "match";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw noMatch();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-06-29 14:42:11 +02:00
|
|
|
|
}.get(); // this extracts the received message
|
2012-06-25 19:30:13 +02:00
|
|
|
|
assertEquals("match", out);
|
|
|
|
|
|
}};
|
2012-06-29 14:42:11 +02:00
|
|
|
|
//#test-expectmsg
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateReceiveWhile() {
|
|
|
|
|
|
//#test-receivewhile
|
|
|
|
|
|
new JavaTestKit(system) {{
|
2013-06-05 16:59:25 +02:00
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
|
|
|
|
|
getRef().tell(43, ActorRef.noSender());
|
|
|
|
|
|
getRef().tell("hello", ActorRef.noSender());
|
2013-03-26 18:17:50 +01:00
|
|
|
|
final String[] out =
|
2012-06-29 14:42:11 +02:00
|
|
|
|
new ReceiveWhile<String>(String.class, duration("1 second")) {
|
|
|
|
|
|
// do not put code outside this method, will run afterwards
|
|
|
|
|
|
protected String match(Object in) {
|
|
|
|
|
|
if (in instanceof Integer) {
|
|
|
|
|
|
return in.toString();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw noMatch();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}.get(); // this extracts the received messages
|
|
|
|
|
|
assertArrayEquals(new String[] {"42", "43"}, out);
|
|
|
|
|
|
expectMsgEquals("hello");
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-receivewhile
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
//#test-receivewhile-full
|
|
|
|
|
|
new ReceiveWhile<String>( // type of array to be created must match ...
|
|
|
|
|
|
String.class, // ... this class which is needed to that end
|
|
|
|
|
|
duration("100 millis"), // maximum collect time
|
|
|
|
|
|
duration("50 millis"), // maximum time between messages
|
|
|
|
|
|
12 // maximum number of messages to collect
|
|
|
|
|
|
) {
|
|
|
|
|
|
//#match-elided
|
|
|
|
|
|
protected String match(Object in) {
|
|
|
|
|
|
throw noMatch();
|
|
|
|
|
|
}
|
|
|
|
|
|
//#match-elided
|
|
|
|
|
|
};
|
|
|
|
|
|
//#test-receivewhile-full
|
|
|
|
|
|
}};
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateAwaitCond() {
|
|
|
|
|
|
//#test-awaitCond
|
|
|
|
|
|
new JavaTestKit(system) {{
|
2013-06-05 16:59:25 +02:00
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
2012-06-29 14:42:11 +02:00
|
|
|
|
new AwaitCond(
|
|
|
|
|
|
duration("1 second"), // maximum wait time
|
|
|
|
|
|
duration("100 millis") // interval at which to check the condition
|
|
|
|
|
|
) {
|
|
|
|
|
|
// do not put code outside this method, will run afterwards
|
|
|
|
|
|
protected boolean cond() {
|
|
|
|
|
|
// typically used to wait for something to start up
|
|
|
|
|
|
return msgAvailable();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-awaitCond
|
|
|
|
|
|
}
|
2013-03-22 18:33:14 +01:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateAwaitAssert() {
|
|
|
|
|
|
//#test-awaitAssert
|
|
|
|
|
|
new JavaTestKit(system) {{
|
2013-06-05 16:59:25 +02:00
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
2013-03-22 18:33:14 +01:00
|
|
|
|
new AwaitAssert(
|
|
|
|
|
|
duration("1 second"), // maximum wait time
|
|
|
|
|
|
duration("100 millis") // interval at which to check the condition
|
|
|
|
|
|
) {
|
|
|
|
|
|
// do not put code outside this method, will run afterwards
|
|
|
|
|
|
protected void check() {
|
|
|
|
|
|
assertEquals(msgAvailable(), true);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-awaitAssert
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
2013-04-14 22:56:41 +02:00
|
|
|
|
@SuppressWarnings({ "unchecked", "unused" }) // due to generic varargs
|
2012-06-29 14:42:11 +02:00
|
|
|
|
public void demonstrateExpect() {
|
|
|
|
|
|
new JavaTestKit(system) {{
|
2013-06-05 16:59:25 +02:00
|
|
|
|
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());
|
2012-06-29 14:42:11 +02:00
|
|
|
|
//#test-expect
|
|
|
|
|
|
final String hello = expectMsgEquals("hello");
|
|
|
|
|
|
final Object any = expectMsgAnyOf("hello", "world");
|
|
|
|
|
|
final Object[] all = expectMsgAllOf("hello", "world");
|
|
|
|
|
|
final int i = expectMsgClass(Integer.class);
|
|
|
|
|
|
final Number j = expectMsgAnyClassOf(Integer.class, Long.class);
|
|
|
|
|
|
expectNoMsg();
|
|
|
|
|
|
//#test-expect
|
2013-06-05 16:59:25 +02:00
|
|
|
|
getRef().tell("receveN-1", ActorRef.noSender());
|
|
|
|
|
|
getRef().tell("receveN-2", ActorRef.noSender());
|
2013-02-12 21:43:06 +13:00
|
|
|
|
//#test-expect
|
|
|
|
|
|
final Object[] two = receiveN(2);
|
|
|
|
|
|
//#test-expect
|
2012-06-29 14:42:11 +02:00
|
|
|
|
assertEquals("hello", hello);
|
|
|
|
|
|
assertEquals("hello", any);
|
|
|
|
|
|
assertEquals(42, i);
|
|
|
|
|
|
assertEquals(42, j);
|
|
|
|
|
|
assertArrayEquals(new String[] {"hello", "world"}, all);
|
|
|
|
|
|
}};
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateIgnoreMsg() {
|
|
|
|
|
|
//#test-ignoreMsg
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
// ignore all Strings
|
|
|
|
|
|
new IgnoreMsg() {
|
|
|
|
|
|
protected boolean ignore(Object msg) {
|
|
|
|
|
|
return msg instanceof String;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2013-06-05 16:59:25 +02:00
|
|
|
|
getRef().tell("hello", ActorRef.noSender());
|
|
|
|
|
|
getRef().tell(42, ActorRef.noSender());
|
2012-06-29 14:42:11 +02:00
|
|
|
|
expectMsgEquals(42);
|
|
|
|
|
|
// remove message filter
|
|
|
|
|
|
ignoreNoMsg();
|
2013-06-05 16:59:25 +02:00
|
|
|
|
getRef().tell("hello", ActorRef.noSender());
|
2012-06-29 14:42:11 +02:00
|
|
|
|
expectMsgEquals("hello");
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-ignoreMsg
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateDilated() {
|
|
|
|
|
|
//#duration-dilation
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
final Duration original = duration("1 second");
|
|
|
|
|
|
final Duration stretched = dilated(original);
|
|
|
|
|
|
assertTrue("dilated", stretched.gteq(original));
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#duration-dilation
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateProbe() {
|
|
|
|
|
|
//#test-probe
|
|
|
|
|
|
new JavaTestKit(system) {{
|
2013-04-14 22:56:41 +02:00
|
|
|
|
// simple actor which just forwards messages
|
2017-02-04 11:51:30 +05:00
|
|
|
|
class Forwarder extends AbstractActor {
|
2013-04-14 22:56:41 +02:00
|
|
|
|
final ActorRef target;
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
|
public Forwarder(ActorRef target) {
|
|
|
|
|
|
this.target = target;
|
|
|
|
|
|
}
|
2017-02-04 11:51:30 +05:00
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
|
.matchAny(message -> target.forward(message, getContext()))
|
|
|
|
|
|
.build();
|
2013-04-14 22:56:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
// create a test probe
|
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
// create a forwarder, injecting the probe’s testActor
|
2013-04-14 22:56:41 +02:00
|
|
|
|
final Props props = Props.create(Forwarder.class, this, probe.getRef());
|
2012-06-29 14:42:11 +02:00
|
|
|
|
final ActorRef forwarder = system.actorOf(props, "forwarder");
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
// verify correct forwarding
|
|
|
|
|
|
forwarder.tell(42, getRef());
|
|
|
|
|
|
probe.expectMsgEquals(42);
|
|
|
|
|
|
assertEquals(getRef(), probe.getLastSender());
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-probe
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2014-12-04 22:42:40 +01:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateTestProbeWithCustomName() {
|
|
|
|
|
|
//#test-probe-with-custom-name
|
|
|
|
|
|
new JavaTestKit(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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateSpecialProbe() {
|
|
|
|
|
|
//#test-special-probe
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
class MyProbe extends JavaTestKit {
|
|
|
|
|
|
public MyProbe() {
|
|
|
|
|
|
super(system);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void assertHello() {
|
|
|
|
|
|
expectMsgEquals("hello");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final MyProbe probe = new MyProbe();
|
2013-06-05 16:59:25 +02:00
|
|
|
|
probe.getRef().tell("hello", ActorRef.noSender());
|
2012-06-29 14:42:11 +02:00
|
|
|
|
probe.assertHello();
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-special-probe
|
|
|
|
|
|
}
|
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));
|
2013-01-23 23:38:22 +01:00
|
|
|
|
//#test-probe-watch
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
|
probe.watch(target);
|
2013-06-05 16:59:25 +02:00
|
|
|
|
target.tell(PoisonPill.getInstance(), ActorRef.noSender());
|
2013-01-23 23:38:22 +01:00
|
|
|
|
final Terminated msg = probe.expectMsgClass(Terminated.class);
|
|
|
|
|
|
assertEquals(msg.getActor(), target);
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-probe-watch
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateReply() {
|
|
|
|
|
|
//#test-probe-reply
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
|
probe.getRef().tell("hello", getRef());
|
|
|
|
|
|
probe.expectMsgEquals("hello");
|
|
|
|
|
|
probe.reply("world");
|
|
|
|
|
|
expectMsgEquals("world");
|
|
|
|
|
|
assertEquals(probe.getRef(), getLastSender());
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-probe-reply
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateForward() {
|
|
|
|
|
|
//#test-probe-forward
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
|
probe.getRef().tell("hello", getRef());
|
|
|
|
|
|
probe.expectMsgEquals("hello");
|
|
|
|
|
|
probe.forward(getRef());
|
|
|
|
|
|
expectMsgEquals("hello");
|
|
|
|
|
|
assertEquals(getRef(), getLastSender());
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-probe-forward
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateWithinProbe() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
//#test-within-probe
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
|
new Within(duration("1 second")) {
|
|
|
|
|
|
public void run() {
|
|
|
|
|
|
probe.expectMsgEquals("hello");
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-within-probe
|
|
|
|
|
|
} 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() {
|
|
|
|
|
|
//#test-auto-pilot
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
|
// install auto-pilot
|
|
|
|
|
|
probe.setAutoPilot(new TestActor.AutoPilot() {
|
|
|
|
|
|
public AutoPilot run(ActorRef sender, Object msg) {
|
2013-06-05 16:59:25 +02:00
|
|
|
|
sender.tell(msg, ActorRef.noSender());
|
2012-06-29 14:42:11 +02:00
|
|
|
|
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());
|
|
|
|
|
|
expectNoMsg();
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-auto-pilot
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
// only compilation
|
|
|
|
|
|
public void demonstrateCTD() {
|
|
|
|
|
|
//#calling-thread-dispatcher
|
|
|
|
|
|
system.actorOf(
|
2013-04-14 22:56:41 +02:00
|
|
|
|
Props.create(MyActor.class)
|
2012-06-29 14:42:11 +02:00
|
|
|
|
.withDispatcher(CallingThreadDispatcher.Id()));
|
|
|
|
|
|
//#calling-thread-dispatcher
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateEventFilter() {
|
|
|
|
|
|
//#test-event-filter
|
|
|
|
|
|
new JavaTestKit(system) {{
|
2013-05-02 17:12:36 +02:00
|
|
|
|
assertEquals("TestKitDocTest", system.name());
|
2012-06-29 14:42:11 +02:00
|
|
|
|
final ActorRef victim = system.actorOf(Props.empty(), "victim");
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
final int result = new EventFilter<Integer>(ActorKilledException.class) {
|
|
|
|
|
|
protected Integer run() {
|
2013-06-05 16:59:25 +02:00
|
|
|
|
victim.tell(Kill.getInstance(), ActorRef.noSender());
|
2012-06-29 14:42:11 +02:00
|
|
|
|
return 42;
|
|
|
|
|
|
}
|
2013-05-02 17:12:36 +02:00
|
|
|
|
}.from("akka://TestKitDocTest/user/victim").occurrences(1).exec();
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
assertEquals(42, result);
|
|
|
|
|
|
}};
|
|
|
|
|
|
//#test-event-filter
|
2012-06-25 19:30:13 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
}
|