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-29 14:42:11 +02:00
|
|
|
*/
|
|
|
|
|
package docs.testkit;
|
|
|
|
|
|
|
|
|
|
//#fullsample
|
2017-03-02 11:55:03 +01:00
|
|
|
import docs.AbstractJavaTest;
|
2012-06-29 14:42:11 +02:00
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.Props;
|
2017-02-04 11:51:30 +05:00
|
|
|
import akka.actor.AbstractActor;
|
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-29 14:42:11 +02:00
|
|
|
|
2017-03-02 11:55:03 +01:00
|
|
|
public class TestKitSampleTest extends AbstractJavaTest {
|
2012-06-29 14:42:11 +02:00
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
public static class SomeActor extends AbstractActor {
|
2012-06-29 14:42:11 +02:00
|
|
|
ActorRef target = null;
|
2017-02-04 11:51:30 +05:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.matchEquals("hello", message -> {
|
|
|
|
|
sender().tell("world", self());
|
|
|
|
|
if (target != null) target.forward(message, getContext());
|
|
|
|
|
})
|
|
|
|
|
.match(ActorRef.class, actorRef -> {
|
|
|
|
|
target = actorRef;
|
|
|
|
|
sender().tell("done", self());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2012-06-29 14:42:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ActorSystem system;
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void setup() {
|
|
|
|
|
system = ActorSystem.create();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void teardown() {
|
2013-05-02 17:12:36 +02:00
|
|
|
JavaTestKit.shutdownActorSystem(system);
|
|
|
|
|
system = null;
|
2012-06-29 14:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testIt() {
|
|
|
|
|
/*
|
|
|
|
|
* Wrap the whole test procedure within a testkit constructor
|
|
|
|
|
* if you want to receive actor replies or use Within(), etc.
|
|
|
|
|
*/
|
|
|
|
|
new JavaTestKit(system) {{
|
2013-04-14 22:56:41 +02:00
|
|
|
final Props props = Props.create(SomeActor.class);
|
2012-06-29 14:42:11 +02:00
|
|
|
final ActorRef subject = system.actorOf(props);
|
|
|
|
|
|
|
|
|
|
// can also use JavaTestKit “from the outside”
|
|
|
|
|
final JavaTestKit probe = new JavaTestKit(system);
|
|
|
|
|
// “inject” the probe by passing it to the test subject
|
|
|
|
|
// like a real resource would be passed in production
|
|
|
|
|
subject.tell(probe.getRef(), getRef());
|
|
|
|
|
// await the correct response
|
|
|
|
|
expectMsgEquals(duration("1 second"), "done");
|
|
|
|
|
|
|
|
|
|
// the run() method needs to finish within 3 seconds
|
|
|
|
|
new Within(duration("3 seconds")) {
|
|
|
|
|
protected void run() {
|
|
|
|
|
|
|
|
|
|
subject.tell("hello", getRef());
|
|
|
|
|
|
|
|
|
|
// This is a demo: would normally use expectMsgEquals().
|
|
|
|
|
// Wait time is bounded by 3-second deadline above.
|
|
|
|
|
new AwaitCond() {
|
|
|
|
|
protected boolean cond() {
|
|
|
|
|
return probe.msgAvailable();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// response must have been enqueued to us before probe
|
|
|
|
|
expectMsgEquals(Duration.Zero(), "world");
|
|
|
|
|
// check that the probe we injected earlier got the msg
|
|
|
|
|
probe.expectMsgEquals(Duration.Zero(), "hello");
|
|
|
|
|
Assert.assertEquals(getRef(), probe.getLastSender());
|
|
|
|
|
|
|
|
|
|
// Will wait for the rest of the 3 seconds
|
|
|
|
|
expectNoMsg();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//#fullsample
|