【发布时间】: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