2012-06-29 14:42:11 +02:00
|
|
|
|
/**
|
2013-01-09 01:47:48 +01:00
|
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
2012-06-25 19:30:13 +02:00
|
|
|
|
*/
|
|
|
|
|
|
package docs.testkit;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.AfterClass;
|
2012-06-29 14:42:11 +02:00
|
|
|
|
import org.junit.BeforeClass;
|
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 com.typesafe.config.Config;
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2012-06-25 19:30:13 +02:00
|
|
|
|
import akka.actor.UntypedActor;
|
2012-06-29 14:42:11 +02:00
|
|
|
|
import akka.actor.UntypedActorFactory;
|
2012-07-06 17:04:04 +02:00
|
|
|
|
import scala.concurrent.Await;
|
|
|
|
|
|
import scala.concurrent.Future;
|
2012-06-29 14:42:11 +02:00
|
|
|
|
import akka.testkit.CallingThreadDispatcher;
|
|
|
|
|
|
import akka.testkit.TestActor;
|
|
|
|
|
|
import akka.testkit.TestActor.AutoPilot;
|
2012-06-25 19:30:13 +02:00
|
|
|
|
import akka.testkit.TestActorRef;
|
2012-06-29 14:42:11 +02:00
|
|
|
|
import akka.testkit.JavaTestKit;
|
2012-10-15 16:18:52 +02:00
|
|
|
|
import scala.concurrent.duration.Duration;
|
2012-06-25 19:30:13 +02:00
|
|
|
|
|
|
|
|
|
|
public class TestKitDocTest {
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
//#test-actor-ref
|
|
|
|
|
|
static class MyActor extends UntypedActor {
|
|
|
|
|
|
public void onReceive(Object o) throws Exception {
|
|
|
|
|
|
if (o.equals("say42")) {
|
|
|
|
|
|
getSender().tell(42, getSelf());
|
|
|
|
|
|
} else if (o instanceof Exception) {
|
|
|
|
|
|
throw (Exception) o;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public boolean testMe() { return true; }
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
//#test-actor-ref
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
private static ActorSystem system;
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
@BeforeClass
|
|
|
|
|
|
public static void setup() {
|
|
|
|
|
|
final Config config = ConfigFactory.parseString(
|
2013-02-01 08:02:53 +01:00
|
|
|
|
"akka.loggers = [akka.testkit.TestEventListener]");
|
2012-06-29 14:42:11 +02:00
|
|
|
|
system = ActorSystem.create("demoSystem", config);
|
2012-06-25 19:30:13 +02:00
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-25 19:30:13 +02:00
|
|
|
|
@AfterClass
|
|
|
|
|
|
public static void cleanup() {
|
|
|
|
|
|
system.shutdown();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#test-actor-ref
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void demonstrateTestActorRef() {
|
|
|
|
|
|
final Props props = new Props(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
|
2012-06-25 19:30:13 +02:00
|
|
|
|
final Props props = new Props(MyActor.class);
|
2012-06-29 14:42:11 +02:00
|
|
|
|
final TestActorRef<MyActor> ref = TestActorRef.create(system, props, "testB");
|
2012-06-25 19:30:13 +02:00
|
|
|
|
final Future<Object> future = akka.pattern.Patterns.ask(ref, "say42", 3000);
|
|
|
|
|
|
assertTrue(future.isCompleted());
|
|
|
|
|
|
assertEquals(42, Await.result(future, Duration.Zero()));
|
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
|
2012-06-25 19:30:13 +02:00
|
|
|
|
final Props props = new Props(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"));
|
|
|
|
|
|
fail("expected an exception to be thrown");
|
|
|
|
|
|
} 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) {{
|
2012-09-19 23:55:53 +02:00
|
|
|
|
getRef().tell(42, null);
|
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) {{
|
2012-09-19 23:55:53 +02:00
|
|
|
|
getRef().tell(42, null);
|
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) {{
|
2012-09-19 23:55:53 +02:00
|
|
|
|
getRef().tell(42, null);
|
|
|
|
|
|
getRef().tell(43, null);
|
|
|
|
|
|
getRef().tell("hello", null);
|
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) {{
|
2012-09-19 23:55:53 +02:00
|
|
|
|
getRef().tell(42, null);
|
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) {{
|
|
|
|
|
|
getRef().tell(42, null);
|
|
|
|
|
|
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
|
|
|
|
|
|
@SuppressWarnings("unchecked") // due to generic varargs
|
|
|
|
|
|
public void demonstrateExpect() {
|
|
|
|
|
|
new JavaTestKit(system) {{
|
2012-09-19 23:55:53 +02:00
|
|
|
|
getRef().tell("hello", null);
|
|
|
|
|
|
getRef().tell("hello", null);
|
|
|
|
|
|
getRef().tell("hello", null);
|
|
|
|
|
|
getRef().tell("world", null);
|
|
|
|
|
|
getRef().tell(42, null);
|
|
|
|
|
|
getRef().tell(42, null);
|
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-02-12 21:43:06 +13:00
|
|
|
|
getRef().tell("receveN-1", null);
|
|
|
|
|
|
getRef().tell("receveN-2", null);
|
|
|
|
|
|
//#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;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2012-09-19 23:55:53 +02:00
|
|
|
|
getRef().tell("hello", null);
|
|
|
|
|
|
getRef().tell(42, null);
|
2012-06-29 14:42:11 +02:00
|
|
|
|
expectMsgEquals(42);
|
|
|
|
|
|
// remove message filter
|
|
|
|
|
|
ignoreNoMsg();
|
2012-09-19 23:55:53 +02:00
|
|
|
|
getRef().tell("hello", null);
|
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
|
|
|
|
|
|
// simple actor which just forwards messages
|
|
|
|
|
|
class Forwarder extends UntypedActor {
|
|
|
|
|
|
final ActorRef target;
|
|
|
|
|
|
public Forwarder(ActorRef target) {
|
|
|
|
|
|
this.target = target;
|
|
|
|
|
|
}
|
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
|
target.forward(msg, getContext());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-03-26 18:17:50 +01:00
|
|
|
|
|
2012-06-29 14:42:11 +02:00
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
|
// 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
|
|
|
|
|
|
final Props props = new Props(new UntypedActorFactory() {
|
|
|
|
|
|
private static final long serialVersionUID = 8927158735963950216L;
|
|
|
|
|
|
public UntypedActor create() {
|
|
|
|
|
|
return new Forwarder(probe.getRef());
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
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
|
|
|
|
|
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();
|
2012-09-19 23:55:53 +02:00
|
|
|
|
probe.getRef().tell("hello", null);
|
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-03-26 18:17:50 +01:00
|
|
|
|
final ActorRef target = system.actorOf(new Props(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-03-26 18:17:50 +01:00
|
|
|
|
target.tell(PoisonPill.getInstance(), null);
|
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) {
|
2012-09-19 23:55:53 +02:00
|
|
|
|
sender.tell(msg, null);
|
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(
|
|
|
|
|
|
new Props(MyActor.class)
|
|
|
|
|
|
.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) {{
|
|
|
|
|
|
assertEquals("demoSystem", system.name());
|
|
|
|
|
|
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() {
|
2012-09-19 23:55:53 +02:00
|
|
|
|
victim.tell(Kill.getInstance(), null);
|
2012-06-29 14:42:11 +02:00
|
|
|
|
return 42;
|
|
|
|
|
|
}
|
|
|
|
|
|
}.from("akka://demoSystem/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
|
|
|
|
}
|