我正试图在java中测试一个Akka Actor。这是测试试图做的,代码在
之下在将消息发布到事件流之前,我可以睡觉或等待片刻,但是没有“更清洁”的方法来检查演员是否完全启动并运行?我可以在事件流上听一些事件,告诉我演员已经完成了自己的创作吗?
@Test
public void testListenOnEventStreamAndForward() throws InterruptedException
{
// given
final TestProbe actorToForwardMessageTo = new TestProbe(this.actorSystem);
final Function<ActorRefFactory, ActorRef> actorToForwardMessageTosFactoryFunction = (actorRefFactory) -> actorToForwardMessageTo.ref();
this.actorSystem.actorOf(Props.create(ActorUnderTest.class, () -> new ActorUnderTest(actorToForwardMessageTosFactoryFunction)));
//The test is green, depending on the whether we sleep here or not
//Thread.sleep(1000);
// when
this.eventStream.publish("someStringMessageTheActorUnderTestShouldPickUp");
// then
actorToForwardMessageTo.expectMsg("someStringMessageTheActorUnderTestShouldPickUp");
}
private static class ActorUnderTest extends AbstractActor
{
public ActorUnderTest(final Function<ActorRefFactory, ActorRef> actorToForwardMessageToFactoryFunction) throws Exception
{
context().system().eventStream().subscribe(self(), String.class);
final ActorRef actorToForwardMessageTo = actorToForwardMessageToFactoryFunction.apply(getContext());
receive(ReceiveBuilder
.match(
String.class,
stringMessage -> actorToForwardMessageTo.tell(stringMessage, self()))
.matchAny(
message -> unhandled(message))
.build());
}
}
答案 0 :(得分:0)
您可以使用awaitAssert
重试发布/预期消息,直到超时才会失败。请参阅此处文档中的awaitAssert部分:http://doc.akka.io/docs/akka/2.4/java/testing.html