2016-07-19 27 views
1

有人可以幫助爲這個抽象Akka actor僞代碼編寫測試用例。如何爲抽象Akka actor的監督策略編寫測試用例

我面對的問題是所有的監督策略信息(作爲測試用例的一部分發送)正在被父母所使用,而不是將其監督策略應用於其子女。

父抽象演員創建孩子。

Abstract class Parent extends UntypedActor { 

    String name; 
    int noOfChildActors; 
    //this set is used to manage the children i.e noOfChildActors = children.size() 
    Set<ActorRef> children;  

    public Parent(String name, int noOfChildActors, Props childProps){ 
    this.name=name; 
    this.noOfChildActors = noOfChildActors; 
    createChildern(childProps); 
    } 

    public void createChildern(Props props){ 
    for(int i = 0 ; i< no_of_child_actors;i++) 
    final ActorRef actor = getContext().actorOf(props, "worker"+i); 
    getContext().watch(actor); 
    children.add(actor); 
    } 

    @Override 
    public void onReceive(final Object message) { 
    //actor functionalities goes here. 
    } 

    @Override 
    public SupervisorStrategy supervisorStrategy() { 
     return defaultSupervisorStrategy(); 
    } 

    private SupervisorStrategy defaultSupervisorStrategy() { 
     return new AllForOneStrategy(-1, Duration.Inf(), new Strategy()); 
    } 

    private class Strategy implements Function<Throwable, Directive> { 
    @Override 
     public Directive apply(final Throwable t) { 
    //my own strategies. 
    } 
    } 
//child managing methods. 
//Abstract functions 
} 
延伸父類

兒童類

class Child extends Parent{ 

    String name; 

    public static Props props(final String name, int noOfChildActors, Props childProps) { 
     return Props.create(Child.class, name, noOfChildActors, childProps); 
    } 

    public Child(String name, int noOfChildActors, Props childProps){ 
    super(name, noOfChildActors, childProps); 
    } 

//followed by my abstract function definition 
} 

//測試用例

private static final FiniteDuration WAIT_TIME = Duration.create(5, TimeUnit.SECONDS); 
    JavaTestKit sender = new JavaTestKit(system); 
    final Props props = Child.Props("name", 3, Props.empty()); 
    ActorRef consumer = system.actorOf(props, "test-supervision"); 
    final TestProbe probe = new TestProbe(system); 
    probe.watch(consumer); 
    consumer.tell(ActorInitializationException.class, sender.getRef()); 
    probe.expectMsgClass(WAIT_TIME, Terminated.class); 

回答

1

parent概念阿卡是不是在類層次中的父,但在樹中的父的演員,所以在你的示例代碼中,類Child並不是真正的類Parent的孩子。在正常情況下,小孩演員不會延伸父母的班級。

一個演員可以通過使用getContext().actorOf()開始一個孩子的父母,在這之後,任何未處理的孩子的異常將最終在父母的監督邏輯中。

詳情請閱讀文檔這裏 http://doc.akka.io/docs/akka/2.4/general/actor-systems.html#Hierarchical_Structure這裏http://doc.akka.io/docs/akka/2.4/java/fault-tolerance.html

+0

感謝約翰回答這一問題。我明白Akka的父母與OO模特不同。我提供的僞碼是一個API。抽象類「Parent」創建子actor(它們是Kafka集羣的生產者和消費者)。你能幫助如何檢驗對其「家長」的監督嗎? –

+0

看看這部分的文檔:http://doc.akka.io/docs/akka/2.4/java/testing.html#Testing_parent-child_relationships它應該給你一些想法 – johanandren