user1229873
user1229873

Reputation: 97

Junit testing an Akka singleton actor: preStart() hook not called

I would like to test a singleton actor using java in Scala IDE build of Eclipse SDK (Build id: 3.0.2-vfinal-20131028-1923-Typesafe) and Akka is 2.3.1.

public class WorkerTest {

static ActorSystem system;

@BeforeClass
public static void setup() {
    system = ActorSystem.create("ClusterSystem");
}

@AfterClass
public static void teardown() {
    JavaTestKit.shutdownActorSystem(system);
    system = null;
}

@Test
public void testWorkers() throws Exception {
    new JavaTestKit(system) {{

        system.actorOf(ClusterSingletonManager.defaultProps(
                Props.create(ClassSingleton.class), "class",
                PoisonPill.getInstance(),"backend"), "classsingleton");

        ActorRef selection = system.actorOf(ClusterSingletonProxy.defaultProps("user/classsingleton/class", "backend"), "proxy");
        System.out.println(selection);
    }};
}
}

the ClassSingleton.java:

public class ClassSingleton extends UntypedActor {

LoggingAdapter log = Logging.getLogger(getContext().system(), this);

public ClassSingleton() {
    System.out.println("Constructor is done");
}

public static Props props() {
    return Props.create(ClassOperator.class);
}

@Override
public void preStart() throws Exception {
    ActorRef selection = getSelf();
    System.out.println("ClassSingleton ActorRef...  " + selection);
}   

@Override
public void onReceive(Object message) {
}

@Override
public void postStop() throws Exception {
    System.out.println("postStop ...  ");
}   


}

The ClassSingleton actor is doing nothing, the printout is: Actor[akka://ClusterSystem/user/proxy#-893814405] only, which is printed from the ClusterSingletonProxy. No exception and Junit is done, green flag. In debugging ClassSingleton is not called (including contructor and preStart()). Sure it is me, but what is the mistake? Even more confusing that the same ClassSingleton ClusterSingletonManager code is working fine outside of javatestkit and junit.

I suspect that the cluster setup might be reponsible, so I tried to include and exclude the following code (no effect). However I would like to understand why we need it, if we need it (it is from an example code). Many thanks for your help.

Address clusterAddress = Cluster.get(system).selfAddress();
Cluster.get(system).join(clusterAddress);

Upvotes: 1

Views: 1142

Answers (1)

almendar
almendar

Reputation: 1813

Proxy pattern standard behavior is to locate the oldest node and deploy the 'real' actor there and the proxy actors are started on all nodes. I suspect that the cluster configuration did not complete and thus why your actor never got started.

The join method makes the node to become a member of the cluster. So if no one joins the cluster the actor with proxy cannot be created.

The question is are your configuration files that are read during junit test have all the information to create a cluster? Seed-nodes? Is the port set to the same as the seed node?

Upvotes: 2

Related Questions