2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.raft;
12 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
14 * This program and the accompanying materials are made available under the
15 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
16 * and is available at http://www.eclipse.org/legal/epl-v10.html
19 import akka.actor.Actor;
20 import akka.actor.ActorIdentity;
21 import akka.actor.ActorRef;
22 import akka.actor.ActorSelection;
23 import akka.actor.ActorSystem;
24 import akka.actor.Identify;
25 import akka.actor.PoisonPill;
26 import akka.actor.Props;
27 import akka.pattern.Patterns;
28 import akka.testkit.JavaTestKit;
29 import akka.testkit.TestActorRef;
30 import akka.util.Timeout;
31 import com.google.common.base.Stopwatch;
32 import com.google.common.util.concurrent.Uninterruptibles;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.Assert;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import scala.concurrent.Await;
40 import scala.concurrent.Future;
43 * TestActorFactory provides methods to create both normal and test actors and to kill them when the factory is closed
44 * The ideal usage for TestActorFactory is with try with resources.
48 * try (TestActorFactory factory = new TestActorFactory(getSystem())){
49 * factory.createActor(props);
50 * factory.createTestActor(props);
51 * factory.generateActorId("leader-");
55 public class TestActorFactory implements AutoCloseable {
56 private static final Logger LOG = LoggerFactory.getLogger(TestActorFactory.class);
58 private final ActorSystem system;
59 List<ActorRef> createdActors = new LinkedList<>();
60 private static int actorCount = 1;
62 public TestActorFactory(ActorSystem system) {
67 * Create a normal actor with an auto-generated name.
69 * @param props the actor Props
70 * @return the ActorRef
72 public ActorRef createActor(Props props) {
73 ActorRef actorRef = system.actorOf(props);
74 return addActor(actorRef);
78 * Create a normal actor with the passed in name.
80 * @param props the actor Props
81 * @param actorId name of actor
82 * @return the ActorRef
84 public ActorRef createActor(Props props, String actorId) {
85 ActorRef actorRef = system.actorOf(props, actorId);
86 return addActor(actorRef);
90 * Create a test actor with the passed in name.
92 * @param props the actor Props
93 * @param actorId name of actor
94 * @param <T> the actor type
95 * @return the ActorRef
97 @SuppressWarnings("unchecked")
98 public <T extends Actor> TestActorRef<T> createTestActor(Props props, String actorId) {
99 TestActorRef<T> actorRef = TestActorRef.create(system, props, actorId);
100 return (TestActorRef<T>) addActor(actorRef);
104 * Create a test actor with an auto-generated name.
106 * @param props the actor Props
107 * @param <T> the actor type
108 * @return the TestActorRef
110 @SuppressWarnings("unchecked")
111 public <T extends Actor> TestActorRef<T> createTestActor(Props props) {
112 TestActorRef<T> actorRef = TestActorRef.create(system, props);
113 return (TestActorRef<T>) addActor(actorRef);
116 private <T extends ActorRef> ActorRef addActor(T actorRef) {
117 createdActors.add(actorRef);
118 verifyActorReady(actorRef);
122 @SuppressWarnings("checkstyle:IllegalCatch")
123 private void verifyActorReady(ActorRef actorRef) {
124 // Sometimes we see messages go to dead letters soon after creation - it seems the actor isn't quite
125 // in a state yet to receive messages or isn't actually created yet. This seems to happen with
126 // actorSelection so, to alleviate it, we use an actorSelection and send an Identify message with
127 // retries to ensure it's ready.
129 Timeout timeout = new Timeout(100, TimeUnit.MILLISECONDS);
130 Throwable lastError = null;
131 Stopwatch sw = Stopwatch.createStarted();
132 while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
134 ActorSelection actorSelection = system.actorSelection(actorRef.path().toString());
135 Future<Object> future = Patterns.ask(actorSelection, new Identify(""), timeout);
136 ActorIdentity reply = (ActorIdentity)Await.result(future, timeout.duration());
137 Assert.assertNotNull("Identify returned null", reply.getRef());
139 } catch (Exception | AssertionError e) {
140 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
145 throw new RuntimeException(lastError);
149 * Generate a friendly but unique actor id/name.
151 * @param prefix the name prefix
152 * @return the actor name
154 public String generateActorId(String prefix) {
155 return prefix + actorCount++;
158 public void killActor(ActorRef actor, JavaTestKit kit) {
159 killActor(actor, kit, true);
162 private void killActor(ActorRef actor, JavaTestKit kit, boolean remove) {
163 LOG.info("Killing actor {}", actor);
165 actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
166 kit.expectTerminated(JavaTestKit.duration("5 seconds"), actor);
169 createdActors.remove(actor);
173 public String createTestActorPath(String actorId) {
174 return "akka://test/user/" + actorId;
178 public void close() {
179 JavaTestKit kit = new JavaTestKit(system);
180 for (ActorRef actor : createdActors) {
181 killActor(actor, kit, false);