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=62f163fb7d0270a2415e934d193136d610e01ca3;hp=c5acb1f2a401dfc6935bc30deb1ba993048cfb91;hb=c31509c7a6630e54a9f9749a643fed5e1a1ad380;hpb=b5c49b7c32cae050b9a91ff07c0a001d7dfb0042 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 c5acb1f2a4..62f163fb7d 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 @@ -9,6 +9,7 @@ package org.opendaylight.controller.cluster.raft.utils; import akka.actor.ActorRef; +import akka.actor.Props; import akka.actor.UntypedActor; import akka.pattern.Patterns; import akka.util.Timeout; @@ -17,6 +18,8 @@ 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 org.junit.Assert; import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.Duration; @@ -24,14 +27,21 @@ import scala.concurrent.duration.FiniteDuration; public class MessageCollectorActor extends UntypedActor { + 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); } } @@ -45,11 +55,7 @@ public class MessageCollectorActor extends UntypedActor { 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); } /** @@ -59,33 +65,67 @@ public class MessageCollectorActor extends UntypedActor { * @return */ public static T getFirstMatching(ActorRef actor, Class clazz) throws Exception { - for(int i = 0; i < 50; i++) { - List allMessages = getAllMessages(actor); + List allMessages = getAllMessages(actor); - for(Object message : allMessages){ - if(message.getClass().equals(clazz)){ - return (T) message; - } + for(Object message : allMessages){ + if(message.getClass().equals(clazz)){ + return (T) message; } + } + + return null; + } + + public static T expectFirstMatching(ActorRef actor, Class clazz) { + return expectFirstMatching(actor, clazz, 5000); + } - Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); + public static T expectFirstMatching(ActorRef actor, Class clazz, long timeout) { + int count = (int) (timeout / 50); + for(int i = 0; i < count; i++) { + try { + T message = getFirstMatching(actor, clazz); + if(message != null) { + return message; + } + } catch (Exception e) {} + + Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS); } + Assert.fail("Did not receive message of type " + clazz); return null; } - public static List getAllMatching(ActorRef actor, Class clazz) throws Exception { + public static List getAllMatching(ActorRef actor, Class clazz) throws Exception { List allMessages = getAllMessages(actor); - List output = Lists.newArrayList(); + List output = Lists.newArrayList(); for(Object message : allMessages){ if(message.getClass().equals(clazz)){ - output.add(message); + output.add((T) message); } } 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."); + } + + public static Props props() { + return Props.create(MessageCollectorActor.class); + } }