X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FTestActorFactory.java;h=50a3c98131a4f06bbde84a5346386d74e379de0e;hp=6872c8fa4528831fda59d08ad0c3b1864054a49e;hb=dcc9827eab4f95b8e35820e8355066ff105d2720;hpb=bbaba878c38f381b0b924f89b29a1d0fcf6e2a2f diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/TestActorFactory.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/TestActorFactory.java index 6872c8fa45..50a3c98131 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/TestActorFactory.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/TestActorFactory.java @@ -5,31 +5,37 @@ * 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.ActorIdentity; import akka.actor.ActorRef; +import akka.actor.ActorSelection; import akka.actor.ActorSystem; +import akka.actor.Identify; +import akka.actor.InvalidActorNameException; import akka.actor.PoisonPill; import akka.actor.Props; +import akka.pattern.Patterns; import akka.testkit.TestActorRef; +import akka.testkit.javadsl.TestKit; +import akka.util.Timeout; +import com.google.common.base.Stopwatch; +import com.google.common.util.concurrent.Uninterruptibles; +import java.time.Duration; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.TimeUnit; +import org.junit.Assert; 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,
+ * The ideal usage for TestActorFactory is with try with resources. + *

* For example
*

  *     try (TestActorFactory factory = new TestActorFactory(getSystem())){
@@ -40,78 +46,157 @@ import org.slf4j.LoggerFactory;
  * 
*/ public class TestActorFactory implements AutoCloseable { + private static final Logger LOG = LoggerFactory.getLogger(TestActorFactory.class); + private final ActorSystem system; List createdActors = new LinkedList<>(); - Logger LOG = LoggerFactory.getLogger(getClass()); private static int actorCount = 1; - public TestActorFactory(ActorSystem system){ + public TestActorFactory(final ActorSystem system) { this.system = system; } /** - * Create a normal actor with an auto-generated name + * Create a normal actor with an auto-generated name. * - * @param props - * @return + * @param props the actor Props + * @return the ActorRef */ - public ActorRef createActor(Props props){ + public ActorRef createActor(final Props props) { ActorRef actorRef = system.actorOf(props); - createdActors.add(actorRef); - return actorRef; + return addActor(actorRef, true); } /** - * Create a normal actor with the passed in name - * @param props + * Create a normal actor with the passed in name. + * + * @param props the actor Props * @param actorId name of actor - * @return + * @return the ActorRef */ - public ActorRef createActor(Props props, String actorId){ + public ActorRef createActor(final Props props, final String actorId) { ActorRef actorRef = system.actorOf(props, actorId); - createdActors.add(actorRef); - return actorRef; + return addActor(actorRef, true); } /** - * Create a test actor with the passed in name - * @param props - * @param actorId - * @param - * @return + * Create a normal actor with the passed in name w/o verifying that the actor is ready. + * + * @param props the actor Props + * @param actorId name of actor + * @return the ActorRef */ - public TestActorRef createTestActor(Props props, String actorId){ - TestActorRef actorRef = TestActorRef.create(system, props, actorId); - createdActors.add(actorRef); - return actorRef; + public ActorRef createActorNoVerify(final Props props, final String actorId) { + ActorRef actorRef = system.actorOf(props, actorId); + return addActor(actorRef, false); } /** - * Create a test actor with an auto-generated name - * @param props - * @param - * @return + * Create a test actor with the passed in name. + * + * @param props the actor Props + * @param actorId name of actor + * @param the actor type + * @return the ActorRef */ - public TestActorRef createTestActor(Props props){ + @SuppressWarnings("unchecked") + public TestActorRef createTestActor(final Props props, final String actorId) { + InvalidActorNameException lastError = null; + for (int i = 0; i < 10; i++) { + try { + TestActorRef actorRef = TestActorRef.create(system, props, actorId); + return (TestActorRef) addActor(actorRef, true); + } catch (InvalidActorNameException e) { + lastError = e; + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); + } + } + + throw lastError; + } + + /** + * Create a test actor with an auto-generated name. + * + * @param props the actor Props + * @param the actor type + * @return the TestActorRef + */ + @SuppressWarnings("unchecked") + public TestActorRef createTestActor(final Props props) { TestActorRef actorRef = TestActorRef.create(system, props); + return (TestActorRef) addActor(actorRef, true); + } + + private ActorRef addActor(final T actorRef, final boolean verify) { createdActors.add(actorRef); + if (verify) { + verifyActorReady(actorRef); + } + return actorRef; } + @SuppressWarnings("checkstyle:IllegalCatch") + private void verifyActorReady(final 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 send an Identify message with + // retries to ensure it's ready. + + Timeout timeout = new Timeout(100, TimeUnit.MILLISECONDS); + Throwable lastError = null; + Stopwatch sw = Stopwatch.createStarted(); + while (sw.elapsed(TimeUnit.SECONDS) <= 10) { + try { + ActorSelection actorSelection = system.actorSelection(actorRef.path().toString()); + Future future = Patterns.ask(actorSelection, new Identify(""), timeout); + ActorIdentity reply = (ActorIdentity)Await.result(future, timeout.duration()); + Assert.assertTrue("Identify returned non-present", reply.getActorRef().isPresent()); + return; + } catch (Exception | AssertionError e) { + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); + lastError = e; + } + } + + throw new RuntimeException(lastError); + } + /** - * Generate a friendly but unique actor id/name - * @param prefix - * @return + * Generate a friendly but unique actor id/name. + * + * @param prefix the name prefix + * @return the actor name */ - public String generateActorId(String prefix){ + public String generateActorId(final String prefix) { return prefix + actorCount++; } + public void killActor(final ActorRef actor, final TestKit kit) { + killActor(actor, kit, true); + } + + private void killActor(final ActorRef actor, final TestKit kit, final boolean remove) { + LOG.info("Killing actor {}", actor); + kit.watch(actor); + actor.tell(PoisonPill.getInstance(), ActorRef.noSender()); + kit.expectTerminated(Duration.ofSeconds(5), actor); + + if (remove) { + createdActors.remove(actor); + } + } + + public String createTestActorPath(final String actorId) { + return "akka://test/user/" + actorId; + } + @Override - public void close() throws Exception { - for(ActorRef actor : createdActors){ - LOG.info("Killing actor {}", actor); - actor.tell(PoisonPill.getInstance(), null); + public void close() { + TestKit kit = new TestKit(system); + for (ActorRef actor : createdActors) { + killActor(actor, kit, false); } } -} \ No newline at end of file +}