Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Akka Java创建带有路径的testprobe引用?_Java_Testing_Akka - Fatal编程技术网

Akka Java创建带有路径的testprobe引用?

Akka Java创建带有路径的testprobe引用?,java,testing,akka,Java,Testing,Akka,我对akka的测试还是有点陌生。在我正在构建的系统中,我正在做一些 private void tellPublisherAboutUpdates(Map<String,Update> updates){ if(updates.isEmpty()){ getContext().actorSelection(ActorSelectionPath.UPDATE_PUBLISHER.path()).tell(new InfoMessage<Ma

我对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());
        }
    }
private void tellisheraboutupdates(地图更新){
if(updates.isEmpty()){
getContext().actorSelection(ActorSelectionPath.UPDATE_PUBLISHER.path()).tell(newinfomessage(updates),getSelf());
}
}

现在,我的第一个想法是,使用TestProbe创建一个带有相关路径的测试引用,但我不知道如何做到这一点?如果有一种替代方法更适合测试这种交互,我也很想了解它

我用来解决这个问题的模式包括创建一个转发参与者,如下所示:

**
 * 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");
当您希望您的角色是当前角色的子角色或顶级角色时,这可以正常工作,但如果您的角色路径引用嵌套在层次结构中的角色,则可能会有点棘手。我结合使用ForwardingActor和ChildCreationActor()来解决这个问题


我通过这个博客了解到了上述技术:

是的,我以前用过几次这种模式。我希望有更地道的方法,谢谢,这是我目前知道的唯一方法……我也对这种方法的任何改进感兴趣