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=0b709aa038d5e12ad4a897c95a7aa5b448feb451;hp=79df92b7d4d64280f9795dfa64e0e0cd507d8fc1;hb=5e8721fd675825ec5c9f826aed61c97e22188960;hpb=e46efc1aec2ec777a876d8fae0b7598e76291302 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 79df92b7d4..0b709aa038 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 @@ -17,16 +17,23 @@ package org.opendaylight.controller.cluster.raft; */ 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.PoisonPill; import akka.actor.Props; +import akka.pattern.Patterns; import akka.testkit.JavaTestKit; import akka.testkit.TestActorRef; import akka.util.Timeout; +import com.google.common.base.Stopwatch; +import com.google.common.util.concurrent.Uninterruptibles; 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; @@ -98,22 +105,26 @@ public class TestActorFactory implements AutoCloseable { 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. + // actorSelection so, to alleviate it, we use an actorSelection and send an Identify message with + // retries to ensure it's ready. - int tries = 1; - while(true) { + Timeout timeout = new Timeout(100, TimeUnit.MILLISECONDS); + Throwable lastError = null; + Stopwatch sw = Stopwatch.createStarted(); + while(sw.elapsed(TimeUnit.SECONDS) <= 10) { 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); - } + ActorSelection actorSelection = system.actorSelection(actorRef.path().toString()); + Future future = Patterns.ask(actorSelection, new Identify(""), timeout); + ActorIdentity reply = (ActorIdentity)Await.result(future, timeout.duration()); + Assert.assertNotNull("Identify returned null", reply.getRef()); + return; + } catch (Exception | AssertionError e) { + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); + lastError = e; } } + + throw new RuntimeException(lastError); } /** @@ -141,6 +152,10 @@ public class TestActorFactory implements AutoCloseable { killActor(actor, kit, true); } + public String createTestActorPath(String actorId){ + return "akka://test/user/" + actorId; + } + private void killActor(ActorRef actor, JavaTestKit kit, boolean remove) { LOG.info("Killing actor {}", actor); kit.watch(actor);