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%2Futils%2FMessageCollectorActor.java;h=79c90cf051cc928ac50e563a136c62030809bbc2;hp=9fbdd4587f2c5e9f5b4a53e53876ca6981fb8af9;hb=5c86921f0a4a77deb74389a5b229d506368c1684;hpb=0e0835991cb08eedfe0be4863f2557528d4573dc diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/MessageCollectorActor.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/MessageCollectorActor.java index 9fbdd4587f..79c90cf051 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/MessageCollectorActor.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/MessageCollectorActor.java @@ -13,39 +13,47 @@ import akka.actor.UntypedActor; import akka.pattern.Patterns; import akka.util.Timeout; import com.google.common.collect.Lists; +import com.google.common.util.concurrent.Uninterruptibles; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.Duration; import scala.concurrent.duration.FiniteDuration; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - public class MessageCollectorActor extends UntypedActor { - private List messages = new ArrayList<>(); + private static final String ARE_YOU_READY = "ARE_YOU_READY"; + + private final List messages = new ArrayList<>(); @Override public void onReceive(Object message) throws Exception { + if(message.equals(ARE_YOU_READY)) { + getSender().tell("yes", getSelf()); + return; + } + if(message instanceof String){ if("get-all-messages".equals(message)){ - getSender().tell(new ArrayList(messages), getSelf()); + getSender().tell(new ArrayList<>(messages), getSelf()); } - } else { + } else if(message != null) { messages.add(message); } } + public void clear() { + messages.clear(); + } + public static List getAllMessages(ActorRef actor) throws Exception { FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS); Timeout operationTimeout = new Timeout(operationDuration); Future future = Patterns.ask(actor, "get-all-messages", operationTimeout); - try { - return (List) Await.result(future, operationDuration); - } catch (Exception e) { - throw e; - } + return (List) Await.result(future, operationDuration); } /** @@ -54,13 +62,17 @@ public class MessageCollectorActor extends UntypedActor { * @param clazz * @return */ - public static Object getFirstMatching(ActorRef actor, Class clazz) throws Exception { - List allMessages = getAllMessages(actor); - - for(Object message : allMessages){ - if(message.getClass().equals(clazz)){ - return message; + public static T getFirstMatching(ActorRef actor, Class clazz) throws Exception { + for(int i = 0; i < 50; i++) { + List allMessages = getAllMessages(actor); + + for(Object message : allMessages){ + if(message.getClass().equals(clazz)){ + return (T) message; + } } + + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); } return null; @@ -80,4 +92,17 @@ public class MessageCollectorActor extends UntypedActor { return output; } + public static void waitUntilReady(ActorRef actor) throws Exception { + long timeout = 500; + FiniteDuration duration = Duration.create(timeout, TimeUnit.MILLISECONDS); + for(int i = 0; i < 10; i++) { + try { + Await.ready(Patterns.ask(actor, ARE_YOU_READY, timeout), duration); + return; + } catch (TimeoutException e) { + } + } + + throw new TimeoutException("Actor not ready in time."); + } }