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.collect.Iterables;
19 import com.google.common.collect.Lists;
20 import com.google.common.util.concurrent.Uninterruptibles;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26 import org.junit.Assert;
27 import scala.concurrent.Await;
28 import scala.concurrent.Future;
29 import scala.concurrent.duration.Duration;
30 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 private static List<Object> getAllMessages(ActorRef actor) throws Exception {
60 FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
61 Timeout operationTimeout = new Timeout(operationDuration);
62 Future<Object> future = Patterns.ask(actor, GET_ALL_MESSAGES, operationTimeout);
64 return (List<Object>) Await.result(future, operationDuration);
67 public static void clearMessages(ActorRef actor) {
68 actor.tell(CLEAR_MESSAGES, ActorRef.noSender());
72 * Get the first message that matches the specified class
77 public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
78 List<Object> allMessages = getAllMessages(actor);
80 for(Object message : allMessages){
81 if(message.getClass().equals(clazz)){
82 return clazz.cast(message);
89 public static <T> List<T> expectMatching(ActorRef actor, Class<T> clazz, int count) {
90 return expectMatching(actor, clazz, count, msg -> true);
93 public static <T> List<T> expectMatching(ActorRef actor, Class<T> clazz, int count,
94 Predicate<T> matcher) {
96 List<T> messages = Collections.emptyList();
97 for(int i = 0; i < timeout / 50; i++) {
99 messages = getAllMatching(actor, clazz);
100 Iterables.removeIf(messages, Predicates.not(matcher));
101 if(messages.size() >= count) {
104 } catch (Exception e) {}
106 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
109 Assert.fail(String.format("Expected %d messages of type %s. Actual received was %d: %s", count, clazz,
110 messages.size(), messages));
114 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
115 return expectFirstMatching(actor, clazz, 5000);
119 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, long timeout) {
120 int count = (int) (timeout / 50);
121 for(int i = 0; i < count; i++) {
123 T message = getFirstMatching(actor, clazz);
124 if(message != null) {
127 } catch (Exception e) {}
129 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
132 Assert.fail("Did not receive message of type " + clazz);
136 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, Predicate<T> matcher) {
138 T lastMessage = null;
139 for(int i = 0; i < timeout / 50; i++) {
141 List<T> messages = getAllMatching(actor, clazz);
142 for(T msg: messages) {
143 if(matcher.apply(msg)) {
149 } catch (Exception e) {}
151 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
154 Assert.fail(String.format("Expected specific message of type %s. Last message received was: %s", clazz, lastMessage));
158 public static <T> void assertNoneMatching(ActorRef actor, Class<T> clazz) {
159 assertNoneMatching(actor, clazz, 5000);
162 public static <T> void assertNoneMatching(ActorRef actor, Class<T> clazz, long timeout) {
163 int count = (int) (timeout / 50);
164 for(int i = 0; i < count; i++) {
166 T message = getFirstMatching(actor, clazz);
167 if(message != null) {
168 Assert.fail("Unexpected message received" + message.toString());
171 } catch (Exception e) {}
173 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
180 public static <T> List<T> getAllMatching(ActorRef actor, Class<T> clazz) throws Exception {
181 List<Object> allMessages = getAllMessages(actor);
183 List<T> output = Lists.newArrayList();
185 for(Object message : allMessages){
186 if(message.getClass().equals(clazz)){
187 output.add(clazz.cast(message));
194 public static void waitUntilReady(ActorRef actor) throws Exception {
196 FiniteDuration duration = Duration.create(timeout, TimeUnit.MILLISECONDS);
197 for(int i = 0; i < 10; i++) {
199 Await.ready(Patterns.ask(actor, ARE_YOU_READY, timeout), duration);
201 } catch (TimeoutException e) {
205 throw new TimeoutException("Actor not ready in time.");
208 public static Props props() {
209 return Props.create(MessageCollectorActor.class);