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 (ARE_YOU_READY.equals(message)) {
42 getSender().tell("yes", getSelf());
43 } else if (GET_ALL_MESSAGES.equals(message)) {
44 getSender().tell(new ArrayList<>(messages), getSelf());
45 } else if (CLEAR_MESSAGES.equals(message)) {
47 } else if (message != null) {
48 messages.add(message);
56 @SuppressWarnings("unchecked")
57 private static List<Object> getAllMessages(ActorRef actor) throws Exception {
58 FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
59 Timeout operationTimeout = new Timeout(operationDuration);
60 Future<Object> future = Patterns.ask(actor, GET_ALL_MESSAGES, operationTimeout);
62 return (List<Object>) Await.result(future, operationDuration);
65 public static void clearMessages(ActorRef actor) {
66 actor.tell(CLEAR_MESSAGES, ActorRef.noSender());
70 * Get the first message that matches the specified class.
72 * @param actor the MessageCollectorActor reference
73 * @param clazz the class to match
74 * @return the first matching message
76 public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
77 List<Object> allMessages = getAllMessages(actor);
79 for (Object message : allMessages) {
80 if (message.getClass().equals(clazz)) {
81 return clazz.cast(message);
88 @SuppressWarnings("checkstyle:IllegalCatch")
89 public static <T> List<T> expectMatching(ActorRef actor, Class<T> clazz, int count) {
90 return expectMatching(actor, clazz, count, msg -> true);
93 @SuppressWarnings("checkstyle:IllegalCatch")
94 public static <T> List<T> expectMatching(ActorRef actor, Class<T> clazz, int count,
95 Predicate<T> matcher) {
97 Exception lastEx = null;
98 List<T> messages = Collections.emptyList();
99 for (int i = 0; i < timeout / 50; i++) {
101 messages = getAllMatching(actor, clazz);
102 Iterables.removeIf(messages, Predicates.not(matcher));
103 if (messages.size() >= count) {
108 } catch (Exception e) {
112 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
115 throw new AssertionError(String.format("Expected %d messages of type %s. Actual received was %d: %s", count,
116 clazz, messages.size(), messages), lastEx);
119 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
120 return expectFirstMatching(actor, clazz, 5000);
123 @SuppressWarnings("checkstyle:IllegalCatch")
124 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, long timeout) {
125 Exception lastEx = null;
126 int count = (int) (timeout / 50);
127 for (int i = 0; i < count; i++) {
129 T message = getFirstMatching(actor, clazz);
130 if (message != null) {
135 } catch (Exception e) {
139 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
142 throw new AssertionError("Did not receive message of type " + clazz, lastEx);
145 @SuppressWarnings("checkstyle:IllegalCatch")
146 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, Predicate<T> matcher) {
148 Exception lastEx = null;
149 T lastMessage = null;
150 for (int i = 0; i < timeout / 50; i++) {
152 List<T> messages = getAllMatching(actor, clazz);
153 for (T msg : messages) {
154 if (matcher.apply(msg)) {
162 } catch (Exception e) {
166 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
169 throw new AssertionError(String.format("Expected specific message of type %s. Last message received was: %s",
170 clazz, lastMessage), lastEx);
173 public static <T> void assertNoneMatching(ActorRef actor, Class<T> clazz) {
174 assertNoneMatching(actor, clazz, 5000);
177 @SuppressWarnings("checkstyle:IllegalCatch")
178 public static <T> void assertNoneMatching(ActorRef actor, Class<T> clazz, long timeout) {
179 Exception lastEx = null;
180 int count = (int) (timeout / 50);
181 for (int i = 0; i < count; i++) {
183 T message = getFirstMatching(actor, clazz);
184 if (message != null) {
185 Assert.fail("Unexpected message received" + message.toString());
190 } catch (Exception e) {
194 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
197 if (lastEx != null) {
198 Throwables.propagate(lastEx);
205 public static <T> List<T> getAllMatching(ActorRef actor, Class<T> clazz) throws Exception {
206 List<Object> allMessages = getAllMessages(actor);
208 List<T> output = Lists.newArrayList();
210 for (Object message : allMessages) {
211 if (message.getClass().equals(clazz)) {
212 output.add(clazz.cast(message));
219 public static void waitUntilReady(ActorRef actor) throws Exception {
221 FiniteDuration duration = Duration.create(timeout, TimeUnit.MILLISECONDS);
222 for (int i = 0; i < 10; i++) {
224 Await.ready(Patterns.ask(actor, ARE_YOU_READY, timeout), duration);
226 } catch (TimeoutException e) {
227 // will fall through below
231 throw new TimeoutException("Actor not ready in time.");
234 public static Props props() {
235 return Props.create(MessageCollectorActor.class);