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.collect.Lists;
17 import com.google.common.util.concurrent.Uninterruptibles;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import org.junit.Assert;
23 import scala.concurrent.Await;
24 import scala.concurrent.Future;
25 import scala.concurrent.duration.Duration;
26 import scala.concurrent.duration.FiniteDuration;
29 public class MessageCollectorActor extends UntypedActor {
30 private static final String ARE_YOU_READY = "ARE_YOU_READY";
32 private final List<Object> messages = new ArrayList<>();
34 @Override public void onReceive(Object message) throws Exception {
35 if(message.equals(ARE_YOU_READY)) {
36 getSender().tell("yes", getSelf());
40 if(message instanceof String){
41 if("get-all-messages".equals(message)){
42 getSender().tell(new ArrayList<>(messages), getSelf());
44 } else if(message != null) {
45 messages.add(message);
53 public static List<Object> getAllMessages(ActorRef actor) throws Exception {
54 FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
55 Timeout operationTimeout = new Timeout(operationDuration);
56 Future<Object> future = Patterns.ask(actor, "get-all-messages", operationTimeout);
58 return (List<Object>) Await.result(future, operationDuration);
62 * Get the first message that matches the specified class
67 public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
68 List<Object> allMessages = getAllMessages(actor);
70 for(Object message : allMessages){
71 if(message.getClass().equals(clazz)){
79 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
80 return expectFirstMatching(actor, clazz, 5000);
83 public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, long timeout) {
84 int count = (int) (timeout / 50);
85 for(int i = 0; i < count; i++) {
87 T message = getFirstMatching(actor, clazz);
91 } catch (Exception e) {}
93 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
96 Assert.fail("Did not receive message of type " + clazz);
100 public static <T> List<T> getAllMatching(ActorRef actor, Class<T> clazz) throws Exception {
101 List<Object> allMessages = getAllMessages(actor);
103 List<T> output = Lists.newArrayList();
105 for(Object message : allMessages){
106 if(message.getClass().equals(clazz)){
107 output.add((T) message);
114 public static void waitUntilReady(ActorRef actor) throws Exception {
116 FiniteDuration duration = Duration.create(timeout, TimeUnit.MILLISECONDS);
117 for(int i = 0; i < 10; i++) {
119 Await.ready(Patterns.ask(actor, ARE_YOU_READY, timeout), duration);
121 } catch (TimeoutException e) {
125 throw new TimeoutException("Actor not ready in time.");
128 public static Props props() {
129 return Props.create(MessageCollectorActor.class);