【问题标题】:how to write a test case for abstract Akka actor's supervision strategy如何为抽象的 Akka actor 的监督策略编写测试用例
【发布时间】:2016-07-19 14:14:23
【问题描述】:

有人可以帮助为以下抽象的 Akka actor 伪代码编写一个测试用例。

我面临的问题是所有监督策略消息(作为测试用例的一部分发送)都被父参与者使用,而不是将其监督策略应用于其子角色。

父抽象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);

【问题讨论】:

    标签: akka


    【解决方案1】:

    Akka 中的parent 概念不是类层次结构中的父级,而是演员树中的父级,因此在您的示例代码中,类Child 并不是真正的类Parent 的子级。在正常情况下,子actor不会扩展父actor的类。

    一个actor可以通过使用getContext().actorOf()来启动一个child作为一个父级,之后子级中任何未处理的异常都会在父级的监督逻辑中结束。

    在此处阅读更多文档 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

    【讨论】:

    • 感谢约翰回答这个问题。我了解 Akka 中的父级与 OO 模型不同。我提供的伪代码是一个 API。抽象类Parent 创建子actor(它们是Kafka 集群的生产者和消费者)。你能帮忙测试一下它对Parent 是它的孩子的监督吗?
    • 看看这部分文档:doc.akka.io/docs/akka/2.4/java/… 它应该会给你一些想法
    猜你喜欢
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 2019-03-12
    • 2020-09-19
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多