Merge "Create odl-nsf-service feature, which excludes neutron feature"
[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.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;
27
28
29 public class MessageCollectorActor extends UntypedActor {
30     private static final String ARE_YOU_READY = "ARE_YOU_READY";
31
32     private final List<Object> messages = new ArrayList<>();
33
34     @Override public void onReceive(Object message) throws Exception {
35         if(message.equals(ARE_YOU_READY)) {
36             getSender().tell("yes", getSelf());
37             return;
38         }
39
40         if(message instanceof String){
41             if("get-all-messages".equals(message)){
42                 getSender().tell(new ArrayList<>(messages), getSelf());
43             }
44         } else if(message != null) {
45             messages.add(message);
46         }
47     }
48
49     public void clear() {
50         messages.clear();
51     }
52
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);
57
58         return (List<Object>) Await.result(future, operationDuration);
59     }
60
61     /**
62      * Get the first message that matches the specified class
63      * @param actor
64      * @param clazz
65      * @return
66      */
67     public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
68         List<Object> allMessages = getAllMessages(actor);
69
70         for(Object message : allMessages){
71             if(message.getClass().equals(clazz)){
72                 return (T) message;
73             }
74         }
75
76         return null;
77     }
78
79     public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
80         return expectFirstMatching(actor, clazz, 5000);
81     }
82
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++) {
86             try {
87                 T message = getFirstMatching(actor, clazz);
88                 if(message != null) {
89                     return message;
90                 }
91             } catch (Exception e) {}
92
93             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
94         }
95
96         Assert.fail("Did not receive message of type " + clazz);
97         return null;
98     }
99
100     public static <T> List<T> getAllMatching(ActorRef actor, Class<T> clazz) throws Exception {
101         List<Object> allMessages = getAllMessages(actor);
102
103         List<T> output = Lists.newArrayList();
104
105         for(Object message : allMessages){
106             if(message.getClass().equals(clazz)){
107                 output.add((T) message);
108             }
109         }
110
111         return output;
112     }
113
114     public static void waitUntilReady(ActorRef actor) throws Exception {
115         long timeout = 500;
116         FiniteDuration duration = Duration.create(timeout, TimeUnit.MILLISECONDS);
117         for(int i = 0; i < 10; i++) {
118             try {
119                 Await.ready(Patterns.ask(actor, ARE_YOU_READY, timeout), duration);
120                 return;
121             } catch (TimeoutException e) {
122             }
123         }
124
125         throw new TimeoutException("Actor not ready in time.");
126     }
127
128     public static Props props() {
129         return Props.create(MessageCollectorActor.class);
130     }
131 }