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=6ea7a20924d4480b0497c02e715eefddee81556c;hp=79c90cf051cc928ac50e563a136c62030809bbc2;hb=73dc2546de1386c977a9e4efd8e589e3db223cc9;hpb=c64ef5f44f131976c20fcf8ced56627f81091838 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 79c90cf051..6ea7a20924 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,15 +9,20 @@ 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; +import com.google.common.base.Predicate; import com.google.common.collect.Lists; import com.google.common.util.concurrent.Uninterruptibles; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import org.junit.Assert; +import org.opendaylight.controller.cluster.raft.SerializationUtils; import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.Duration; @@ -26,6 +31,8 @@ import scala.concurrent.duration.FiniteDuration; public class MessageCollectorActor extends UntypedActor { private static final String ARE_YOU_READY = "ARE_YOU_READY"; + private static final String GET_ALL_MESSAGES = "get-all-messages"; + private static final String CLEAR_MESSAGES = "clear-messages"; private final List messages = new ArrayList<>(); @@ -35,12 +42,12 @@ public class MessageCollectorActor extends UntypedActor { return; } - if(message instanceof String){ - if("get-all-messages".equals(message)){ - getSender().tell(new ArrayList<>(messages), getSelf()); - } + if(GET_ALL_MESSAGES.equals(message)) { + getSender().tell(new ArrayList<>(messages), getSelf()); + } else if(CLEAR_MESSAGES.equals(message)) { + messages.clear(); } else if(message != null) { - messages.add(message); + messages.add(SerializationUtils.fromSerializable(message)); } } @@ -51,11 +58,15 @@ public class MessageCollectorActor extends UntypedActor { 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); + Future future = Patterns.ask(actor, GET_ALL_MESSAGES, operationTimeout); return (List) Await.result(future, operationDuration); } + public static void clearMessages(ActorRef actor) { + actor.tell(CLEAR_MESSAGES, ActorRef.noSender()); + } + /** * Get the first message that matches the specified class * @param actor @@ -63,29 +74,87 @@ 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 List expectMatching(ActorRef actor, Class clazz, int count) { + int timeout = 5000; + List messages = Collections.emptyList(); + for(int i = 0; i < timeout / 50; i++) { + try { + messages = getAllMatching(actor, clazz); + if(messages.size() >= count) { + return messages; + } + } catch (Exception e) {} - Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); + Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS); } + Assert.fail(String.format("Expected %d messages of type %s. Actual received was %d: %s", count, clazz, + messages.size(), messages)); return null; } - public static List getAllMatching(ActorRef actor, Class clazz) throws Exception { + public static T expectFirstMatching(ActorRef actor, Class clazz) { + return expectFirstMatching(actor, clazz, 5000); + } + + 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 T expectFirstMatching(ActorRef actor, Class clazz, Predicate matcher) { + int timeout = 5000; + T lastMessage = null; + for(int i = 0; i < timeout / 50; i++) { + try { + List messages = getAllMatching(actor, clazz); + for(T msg: messages) { + if(matcher.apply(msg)) { + return msg; + } + + lastMessage = msg; + } + } catch (Exception e) {} + + Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS); + } + + Assert.fail(String.format("Expected specific message of type %s. Last message received was: %s", clazz, lastMessage)); + return null; + } + + 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); } } @@ -105,4 +174,8 @@ public class MessageCollectorActor extends UntypedActor { throw new TimeoutException("Actor not ready in time."); } + + public static Props props() { + return Props.create(MessageCollectorActor.class); + } }