2 * Copyright (c) 2014 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.datastore.utils;
11 import akka.actor.ActorRef;
12 import akka.actor.UntypedActor;
14 import akka.pattern.Patterns;
15 import akka.util.Timeout;
16 import com.google.common.collect.Lists;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.concurrent.TimeUnit;
20 import scala.concurrent.Await;
21 import scala.concurrent.Future;
22 import scala.concurrent.duration.Duration;
23 import scala.concurrent.duration.FiniteDuration;
26 * MessageCollectorActor collects messages as it receives them. It can send
27 * those collected messages to any sender which sends it the "messages" message
29 * This class would be useful as a mock to test whether messages were sent
30 * to a remote actor or not.
33 public class MessageCollectorActor extends UntypedActor {
34 private List<Object> messages = new ArrayList<>();
36 @Override public void onReceive(Object message) throws Exception {
37 if(message instanceof String){
38 if("messages".equals(message)){
39 getSender().tell(new ArrayList(messages), getSelf());
42 messages.add(message);
46 public static List<Object> getAllMessages(ActorRef actor) throws Exception {
47 FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
48 Timeout operationTimeout = new Timeout(operationDuration);
49 Future<Object> future = Patterns.ask(actor, "messages", operationTimeout);
52 return (List<Object>) Await.result(future, operationDuration);
53 } catch (Exception e) {
59 * Get the first message that matches the specified class
64 public static Object getFirstMatching(ActorRef actor, Class<?> clazz) throws Exception {
65 List<Object> allMessages = getAllMessages(actor);
67 for(Object message : allMessages){
68 if(message.getClass().equals(clazz)){
76 public static List<Object> getAllMatching(ActorRef actor, Class<?> clazz) throws Exception {
77 List<Object> allMessages = getAllMessages(actor);
79 List<Object> output = Lists.newArrayList();
81 for(Object message : allMessages){
82 if(message.getClass().equals(clazz)){