mardi 3 novembre 2015

Akka test ActorRef creation

I have a Master actor class which receives a Creator as constructor parameter to obtain through it his dependent actors. The Creator allows me to test the Master actor using a mock that returns Testkit objets instead of real actors.

My question are;

  • How can I know that the created actors are the expected ones?
  • Is there a way to know if an ActorRef references the expected actor?
  • Is there a way to test this kind if Creator implementation?

Here are some classes showed as example.

The Master actor:

public class MasterActor extends UntypedActor {
private final Creator creator;
private final ActorRef comm1;
private final ActorRef comm2;
private final ActorRef controller;

public MasterActor(Creator creator) throws Exception {
    this.creator = creator;
    comm1 = creator.comm1Function().apply(context().system());
    comm2 = creator.comm2Function().apply(context().system());
    controller = creator.controlerFunction().apply(context().system());
}

@Override
public void onReceive(Object o) throws Exception {
    if (o.equals("ExpectedMsg")) {
        // Do something with the actors.
    } else {
        unhandled(o);
    }
  }
}

TheCreator interface:

public interface Creator {
    public Function<ActorRefFactory, ActorRef> comm1Function();
    public Function<ActorRefFactory, ActorRef> comm2Function();
    public Function<ActorRefFactory, ActorRef> controlerFunction();
}

And finally a Creator implementation:

public class CreatorImpl implements Creator {
    private static final int ID_SYSTEM_1 = 1;
    private static final int ID_SYSTEM_2 = 2;

    private final Map<String, String> _Configuration;

    public CreatorImpl(Map<String, String> _conf) {
        _Configuration = _conf;
    }

    @Override
    public Function<ActorRefFactory, ActorRef> comm1Function() {
        return commFunction(_Configuration, ID_SYSTEM_1);
    }

    public Function<ActorRefFactory, ActorRef> comm2Function() {
        return commFunction(_Configuration, ID_SYSTEM_2);
    }

    @Override
    public Function<ActorRefFactory, ActorRef> controlerFunction() {
        return new Function<ActorRefFactory, ActorRef>() {
            @Override
            public ActorRef apply(ActorRefFactory _actorRefFactory) throws Exception {
                return _actorRefFactory.actorOf(
                        Props.create(LifeControlActor.class), "ControlerActor");
            }
        };
    }

    private Function<ActorRefFactory, ActorRef> commFunction(final Map<String, String> _configuration, final int _id) {
        return new Function<ActorRefFactory, ActorRef>() {

            @Override
            public ActorRef apply(ActorRefFactory _actorRefFactory) throws Exception {
                return _actorRefFactory.actorOf(
                        Props.create(CommunicationActor.class, _configuration), "CommActor" + _id);
            }
        };
    }
}

Aucun commentaire:

Enregistrer un commentaire