/* * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.cluster.raft; /* * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ import akka.actor.Actor; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.PoisonPill; import akka.actor.Props; import akka.testkit.JavaTestKit; import akka.testkit.TestActorRef; import akka.util.Timeout; import java.util.LinkedList; import java.util.List; import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import scala.concurrent.Await; import scala.concurrent.Future; /** * TestActorFactory provides methods to create both normal and test actors and to kill them when the factory is closed * The ideal usage for TestActorFactory is with try with resources,
* For example
*
 *     try (TestActorFactory factory = new TestActorFactory(getSystem())){
 *         factory.createActor(props);
 *         factory.createTestActor(props);
 *         factory.generateActorId("leader-");
 *     }
 * 
*/ public class TestActorFactory implements AutoCloseable { private final ActorSystem system; List createdActors = new LinkedList<>(); Logger LOG = LoggerFactory.getLogger(getClass()); private static int actorCount = 1; public TestActorFactory(ActorSystem system){ this.system = system; } /** * Create a normal actor with an auto-generated name * * @param props * @return */ public ActorRef createActor(Props props){ ActorRef actorRef = system.actorOf(props); return addActor(actorRef); } /** * Create a normal actor with the passed in name * @param props * @param actorId name of actor * @return */ public ActorRef createActor(Props props, String actorId){ ActorRef actorRef = system.actorOf(props, actorId); return addActor(actorRef); } /** * Create a test actor with the passed in name * @param props * @param actorId * @param * @return */ @SuppressWarnings("unchecked") public TestActorRef createTestActor(Props props, String actorId){ TestActorRef actorRef = TestActorRef.create(system, props, actorId); return (TestActorRef) addActor(actorRef); } private ActorRef addActor(T actorRef) { createdActors.add(actorRef); verifyActorReady(actorRef); return actorRef; } private void verifyActorReady(ActorRef actorRef) { // Sometimes we see messages go to dead letters soon after creation - it seems the actor isn't quite // in a state yet to receive messages or isn't actually created yet. This seems to happen with // actorSelection so, to alleviate it, we use an actorSelection and call resolveOne with retries to // ensure it's ready. int tries = 1; while(true) { try { Timeout timeout = new Timeout(100, TimeUnit.MILLISECONDS); Future future = system.actorSelection(actorRef.path()).resolveOne(timeout); Await.ready(future, timeout.duration()); break; } catch (Exception e) { if(tries++ > 20) { throw new RuntimeException(e); } } } } /** * Create a test actor with an auto-generated name * @param props * @param * @return */ @SuppressWarnings("unchecked") public TestActorRef createTestActor(Props props){ TestActorRef actorRef = TestActorRef.create(system, props); return (TestActorRef) addActor(actorRef); } /** * Generate a friendly but unique actor id/name * @param prefix * @return */ public String generateActorId(String prefix){ return prefix + actorCount++; } public void killActor(ActorRef actor, JavaTestKit kit) { killActor(actor, kit, true); } private void killActor(ActorRef actor, JavaTestKit kit, boolean remove) { LOG.info("Killing actor {}", actor); kit.watch(actor); actor.tell(PoisonPill.getInstance(), ActorRef.noSender()); kit.expectTerminated(JavaTestKit.duration("5 seconds"), actor); if(remove) { createdActors.remove(actor); } } @Override public void close() { JavaTestKit kit = new JavaTestKit(system); for(ActorRef actor : createdActors) { killActor(actor, kit, false); } } }