Merge "BUG-2627: do not duplicate descriptions"
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / utils / MessageCollectorActor.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft.utils;
10
11 import akka.actor.ActorRef;
12 import akka.actor.UntypedActor;
13 import akka.pattern.Patterns;
14 import akka.util.Timeout;
15 import com.google.common.collect.Lists;
16 import com.google.common.util.concurrent.Uninterruptibles;
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;
24
25
26 public class MessageCollectorActor extends UntypedActor {
27     private final List<Object> messages = new ArrayList<>();
28
29     @Override public void onReceive(Object message) throws Exception {
30         if(message instanceof String){
31             if("get-all-messages".equals(message)){
32                 getSender().tell(new ArrayList(messages), getSelf());
33             }
34         } else {
35             messages.add(message);
36         }
37     }
38
39     public void clear() {
40         messages.clear();
41     }
42
43     public static List<Object> getAllMessages(ActorRef actor) throws Exception {
44         FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
45         Timeout operationTimeout = new Timeout(operationDuration);
46         Future<Object> future = Patterns.ask(actor, "get-all-messages", operationTimeout);
47
48         try {
49             return (List<Object>) Await.result(future, operationDuration);
50         } catch (Exception e) {
51             throw e;
52         }
53     }
54
55     /**
56      * Get the first message that matches the specified class
57      * @param actor
58      * @param clazz
59      * @return
60      */
61     public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
62         for(int i = 0; i < 50; i++) {
63             List<Object> allMessages = getAllMessages(actor);
64
65             for(Object message : allMessages){
66                 if(message.getClass().equals(clazz)){
67                     return (T) message;
68                 }
69             }
70
71             Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
72         }
73
74         return null;
75     }
76
77     public static List<Object> getAllMatching(ActorRef actor, Class<?> clazz) throws Exception {
78         List<Object> allMessages = getAllMessages(actor);
79
80         List<Object> output = Lists.newArrayList();
81
82         for(Object message : allMessages){
83             if(message.getClass().equals(clazz)){
84                 output.add(message);
85             }
86         }
87
88         return output;
89     }
90
91 }