1
votes

Can someone help in write a test case for this following abstract Akka actor pseudo code.

Problem I'm facing is all the supervisions strategy messages (send as part of test case )are being consumed by the parent actor rather than applying its supervision strategy to its children.

Parent Abstract actor creates children .

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  
} 

Child class which extends Parent class

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
}

//Test case

    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

1 Answers

1
votes

The parent concept in Akka is not the parent in the class hierarchy but the parent in the tree of actors, so in your sample code the class Child is not really a child of the class Parent. In the normal case a child actor would not extend the class of the parent.

An actor can be a parent by starting a child using getContext().actorOf(), after this any unhandled exception in the child will end up in the supervision logic of the parent.

Read more in the docs here http://doc.akka.io/docs/akka/2.4/general/actor-systems.html#Hierarchical_Structure and here http://doc.akka.io/docs/akka/2.4/java/fault-tolerance.html