2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
2013-04-16 22:31:09 +02:00
|
|
|
*/
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.actor;
|
2013-04-16 22:31:09 +02:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.AbstractJavaTest;
|
2017-03-17 03:02:47 +08:00
|
|
|
import akka.testkit.javadsl.TestKit;
|
2013-05-02 17:12:36 +02:00
|
|
|
import org.junit.ClassRule;
|
2013-04-16 22:31:09 +02:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.Inbox;
|
|
|
|
|
import akka.actor.PoisonPill;
|
|
|
|
|
import akka.actor.Terminated;
|
|
|
|
|
import akka.testkit.AkkaSpec;
|
|
|
|
|
|
2018-04-11 16:47:36 +02:00
|
|
|
import java.time.Duration;
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public class InboxDocTest extends AbstractJavaTest {
|
2013-04-16 22:31:09 +02:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
|
|
|
|
new AkkaJUnitActorSystemResource("InboxDocTest", AkkaSpec.testConf());
|
2013-04-16 22:31:09 +02:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateInbox() {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2013-04-16 22:31:09 +02:00
|
|
|
final ActorRef target = probe.getRef();
|
|
|
|
|
//#inbox
|
|
|
|
|
final Inbox inbox = Inbox.create(system);
|
|
|
|
|
inbox.send(target, "hello");
|
|
|
|
|
//#inbox
|
|
|
|
|
probe.expectMsgEquals("hello");
|
|
|
|
|
probe.send(probe.getLastSender(), "world");
|
|
|
|
|
//#inbox
|
2014-06-25 17:08:28 +02:00
|
|
|
try {
|
2018-04-11 16:47:36 +02:00
|
|
|
assert inbox.receive(Duration.ofSeconds(1)).equals("world");
|
2014-06-25 17:08:28 +02:00
|
|
|
} catch (java.util.concurrent.TimeoutException e) {
|
|
|
|
|
// timeout
|
|
|
|
|
}
|
2013-04-16 22:31:09 +02:00
|
|
|
//#inbox
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateWatch() {
|
2017-03-17 03:02:47 +08:00
|
|
|
final TestKit probe = new TestKit(system);
|
2013-04-16 22:31:09 +02:00
|
|
|
final ActorRef target = probe.getRef();
|
|
|
|
|
//#watch
|
|
|
|
|
final Inbox inbox = Inbox.create(system);
|
|
|
|
|
inbox.watch(target);
|
2013-06-05 16:59:25 +02:00
|
|
|
target.tell(PoisonPill.getInstance(), ActorRef.noSender());
|
2014-06-25 17:08:28 +02:00
|
|
|
try {
|
2018-04-11 16:47:36 +02:00
|
|
|
assert inbox.receive(Duration.ofSeconds(1)) instanceof Terminated;
|
2014-06-25 17:08:28 +02:00
|
|
|
} catch (java.util.concurrent.TimeoutException e) {
|
|
|
|
|
// timeout
|
|
|
|
|
}
|
2013-04-16 22:31:09 +02:00
|
|
|
//#watch
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|