2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.raft.utils;
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import akka.actor.UntypedActor;
14 import akka.pattern.Patterns;
15 import akka.util.Timeout;
16 import com.google.common.base.Predicate;
17 import com.google.common.base.Predicates;
18 import com.google.common.base.Throwables;
19 import com.google.common.collect.Iterables;
20 import com.google.common.collect.Lists;
21 import com.google.common.util.concurrent.Uninterruptibles;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
27 import org.junit.Assert;
28 import scala.concurrent.Await;
29 import scala.concurrent.Future;
30 import scala.concurrent.duration.Duration;
31 import scala.concurrent.duration.FiniteDuration;
33 public class MessageCollectorActor extends UntypedActor {
34 private static final String ARE_YOU_READY = "ARE_YOU_READY";
35 public static final String GET_ALL_MESSAGES = "messages";
36 private static final String CLEAR_MESSAGES = "clear-messages";
38 private final List<Object> messages = new ArrayList<>();
40 @Override public void onReceive(Object message) throws Exception {
41 if (message.equals(ARE_YOU_READY)) {
42 getSender().tell("yes", getSelf());
46 if (GET_ALL_MESSAGES.equals(message)) {
47 getSender().tell(new ArrayList<>(messages), getSelf());
48 } else if (CLEAR_MESSAGES.equals(message)) {
50 } else if (message != null) {
51 messages.add(message);
59 @SuppressWarnings("unchecked")
60 private static List<Object> getAllMessages(ActorRef actor) throws Exception {
61 FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
62 Timeout operationTimeout = new Timeout(operationDuration);
63 Future<Object> future = Patterns.ask(actor, GET_ALL_MESSAGES, operationTimeout);
65 return (List<Object>) Await.result(future, operationDuration);
68 public static void clearMessages(ActorRef actor) {
69 actor.tell(CLEAR_MESSAGES, ActorRef.noSender());
73 * Get the first message that matches the specified class.
75 * @param actor the MessageCollectorActor reference
76 * @param clazz the class to match
77 * @return the first matching message
79 public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
80 List<Object> allMessages = getAllMessages(actor);
82 for (Object message : allMessages) {
83 if (message.getClass().equals(clazz)) {
84 return clazz.cast(message);
91 @SuppressWarnings("checkstyle:IllegalCatch")
92 public static <T> List<T> expectMatching(ActorRef actor, Class<T> clazz, int count) {
93 return expectMatching(actor, clazz, count, msg -> true);
96 @SuppressWarnings("checkstyle:IllegalCatch")
97 public static <T> List<T> expectMatching(ActorRef actor, Class<T> clazz, int count,
98 Predicate<T> matcher) {
100 Exception lastEx = null;
101 List<T> messages = Collections.emptyList();
102 for (int i = 0; i < timeout / 50; i++) {
104 messages = getAllMatching(actor, clazz);
105 Iterables.removeIf(messages, Predicates.not(matcher));
106 if (messages.size() >= count) {
111 } catch (Exception e) {
115 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
118 throw new AssertionError(String.format("Expected %d messages of type %s. Actual received was %d: %s", count,
119 clazz, messages.size(), messages), lastEx);
122 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
123 return expectFirstMatching(actor, clazz, 5000);
126 @SuppressWarnings("checkstyle:IllegalCatch")
127 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, long timeout) {
128 Exception lastEx = null;
129 int count = (int) (timeout / 50);
130 for (int i = 0; i < count; i++) {
132 T message = getFirstMatching(actor, clazz);
133 if (message != null) {
138 } catch (Exception e) {
142 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
145 throw new AssertionError("Did not receive message of type " + clazz, lastEx);
148 @SuppressWarnings("checkstyle:IllegalCatch")
149 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, Predicate<T> matcher) {
151 Exception lastEx = null;
152 T lastMessage = null;
153 for (int i = 0; i < timeout / 50; i++) {
155 List<T> messages = getAllMatching(actor, clazz);
156 for (T msg : messages) {
157 if (matcher.apply(msg)) {
165 } catch (Exception e) {
169 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
172 throw new AssertionError(String.format("Expected specific message of type %s. Last message received was: %s",
173 clazz, lastMessage), lastEx);
176 public static <T> void assertNoneMatching(ActorRef actor, Class<T> clazz) {
177 assertNoneMatching(actor, clazz, 5000);
180 @SuppressWarnings("checkstyle:IllegalCatch")
181 public static <T> void assertNoneMatching(ActorRef actor, Class<T> clazz, long timeout) {
182 Exception lastEx = null;
183 int count = (int) (timeout / 50);
184 for (int i = 0; i < count; i++) {
186 T message = getFirstMatching(actor, clazz);
187 if (message != null) {
188 Assert.fail("Unexpected message received" + message.toString());
193 } catch (Exception e) {
197 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
200 if (lastEx != null) {
201 Throwables.propagate(lastEx);
208 public static <T> List<T> getAllMatching(ActorRef actor, Class<T> clazz) throws Exception {
209 List<Object> allMessages = getAllMessages(actor);
211 List<T> output = Lists.newArrayList();
213 for (Object message : allMessages) {
214 if (message.getClass().equals(clazz)) {
215 output.add(clazz.cast(message));
222 public static void waitUntilReady(ActorRef actor) throws Exception {
224 FiniteDuration duration = Duration.create(timeout, TimeUnit.MILLISECONDS);
225 for (int i = 0; i < 10; i++) {
227 Await.ready(Patterns.ask(actor, ARE_YOU_READY, timeout), duration);
229 } catch (TimeoutException e) {
230 // will fall through below
234 throw new TimeoutException("Actor not ready in time.");
237 public static Props props() {
238 return Props.create(MessageCollectorActor.class);