Akka Java 使用路径创建测试探针引用?
Akka Java create testprobe ref with path ?
我对使用 akka 进行测试还是有点陌生。在我正在构建的系统中,我正在做类似
的事情
private void tellPublisherAboutUpdates(Map<String,Update> updates){
if(updates.isEmpty()){
getContext().actorSelection(ActorSelectionPath.UPDATE_PUBLISHER.path()).tell(new InfoMessage<Map<String,Update>>(updates), getSelf());
}
}
现在,我的第一个想法是使用 TestProbe 创建具有相关路径的测试引用,但我不确定该怎么做?如果有更适合测试此类交互的替代方法,我也很想了解它。
我用来解决这个问题的模式涉及创建一个转发 actor,如下所示:
**
* Simple actor that takes another actor and forwards all messages to it.
* Useful in unit testing for capturing and testing if a message was received.
* Simply pass in an Akka JavaTestKit probe into the constructor, and all messages
* that are sent to this actor are forwarded to the JavaTestKit probe
* Ref: https://gist.github.com/jconwell/8153535
*/
public class ForwardingActor extends UntypedActor {
final ActorRef target;
public ForwardingActor(ActorRef target) {
this.target = target;
}
@Override
public void onReceive(Object msg) {
target.forward(msg, getContext());
}
}
然后您可以像这样使用它来注入您的探针引用:
JavaTestKit probe = new JavaTestKit(actorSystem);
actorSystem.actorOf(Props.create(ForwardingActor.class, probe.getRef()), "myActor");
当您希望您的 actor 是当前 actor 的子级或顶级 actor 时,这可以正常工作,但如果您的 actor 路径引用嵌套在层次结构中的 actor,则可能会有点棘手。我结合使用 ForwardingActor 和 ChildCreationActor (https://gist.github.com/jconwell/8154233) 来解决这个问题。
我通过这个博客了解到上述技术问题:http://geekswithblogs.net/johnsPerfBlog/archive/2014/01/02/akka-test-patterns-for-java.aspx
我对使用 akka 进行测试还是有点陌生。在我正在构建的系统中,我正在做类似
的事情private void tellPublisherAboutUpdates(Map<String,Update> updates){
if(updates.isEmpty()){
getContext().actorSelection(ActorSelectionPath.UPDATE_PUBLISHER.path()).tell(new InfoMessage<Map<String,Update>>(updates), getSelf());
}
}
现在,我的第一个想法是使用 TestProbe 创建具有相关路径的测试引用,但我不确定该怎么做?如果有更适合测试此类交互的替代方法,我也很想了解它。
我用来解决这个问题的模式涉及创建一个转发 actor,如下所示:
**
* Simple actor that takes another actor and forwards all messages to it.
* Useful in unit testing for capturing and testing if a message was received.
* Simply pass in an Akka JavaTestKit probe into the constructor, and all messages
* that are sent to this actor are forwarded to the JavaTestKit probe
* Ref: https://gist.github.com/jconwell/8153535
*/
public class ForwardingActor extends UntypedActor {
final ActorRef target;
public ForwardingActor(ActorRef target) {
this.target = target;
}
@Override
public void onReceive(Object msg) {
target.forward(msg, getContext());
}
}
然后您可以像这样使用它来注入您的探针引用:
JavaTestKit probe = new JavaTestKit(actorSystem);
actorSystem.actorOf(Props.create(ForwardingActor.class, probe.getRef()), "myActor");
当您希望您的 actor 是当前 actor 的子级或顶级 actor 时,这可以正常工作,但如果您的 actor 路径引用嵌套在层次结构中的 actor,则可能会有点棘手。我结合使用 ForwardingActor 和 ChildCreationActor (https://gist.github.com/jconwell/8154233) 来解决这个问题。
我通过这个博客了解到上述技术问题:http://geekswithblogs.net/johnsPerfBlog/archive/2014/01/02/akka-test-patterns-for-java.aspx