Bug 6540: EOS - handle edge case with pruning pending owner change commits
[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.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;
31
32
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";
37
38     private final List<Object> messages = new ArrayList<>();
39
40     @Override public void onReceive(Object message) throws Exception {
41         if(message.equals(ARE_YOU_READY)) {
42             getSender().tell("yes", getSelf());
43             return;
44         }
45
46         if(GET_ALL_MESSAGES.equals(message)) {
47             getSender().tell(new ArrayList<>(messages), getSelf());
48         } else if(CLEAR_MESSAGES.equals(message)) {
49             clear();
50         } else if(message != null) {
51             messages.add(message);
52         }
53     }
54
55     public void clear() {
56         messages.clear();
57     }
58
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);
63
64         return (List<Object>) Await.result(future, operationDuration);
65     }
66
67     public static void clearMessages(ActorRef actor) {
68         actor.tell(CLEAR_MESSAGES, ActorRef.noSender());
69     }
70
71     /**
72      * Get the first message that matches the specified class
73      * @param actor
74      * @param clazz
75      * @return
76      */
77     public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
78         List<Object> allMessages = getAllMessages(actor);
79
80         for(Object message : allMessages){
81             if(message.getClass().equals(clazz)){
82                 return clazz.cast(message);
83             }
84         }
85
86         return null;
87     }
88
89     public static <T> List<T> expectMatching(ActorRef actor, Class<T> clazz, int count) {
90         return expectMatching(actor, clazz, count, msg -> true);
91     }
92
93     public static <T> List<T> expectMatching(ActorRef actor, Class<T> clazz, int count,
94             Predicate<T> matcher) {
95         int timeout = 5000;
96         List<T> messages = Collections.emptyList();
97         for(int i = 0; i < timeout / 50; i++) {
98             try {
99                 messages = getAllMatching(actor, clazz);
100                 Iterables.removeIf(messages, Predicates.not(matcher));
101                 if(messages.size() >= count) {
102                     return messages;
103                 }
104             } catch (Exception e) {}
105
106             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
107         }
108
109         Assert.fail(String.format("Expected %d messages of type %s. Actual received was %d: %s", count, clazz,
110                 messages.size(), messages));
111         return null;
112     }
113
114     public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
115         return expectFirstMatching(actor, clazz, 5000);
116     }
117
118
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++) {
122             try {
123                 T message = getFirstMatching(actor, clazz);
124                 if(message != null) {
125                     return message;
126                 }
127             } catch (Exception e) {}
128
129             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
130         }
131
132         Assert.fail("Did not receive message of type " + clazz);
133         return null;
134     }
135
136     public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, Predicate<T> matcher) {
137         int timeout = 5000;
138         T lastMessage = null;
139         for(int i = 0; i < timeout / 50; i++) {
140             try {
141                 List<T> messages = getAllMatching(actor, clazz);
142                 for(T msg: messages) {
143                     if(matcher.apply(msg)) {
144                         return msg;
145                     }
146
147                     lastMessage = msg;
148                 }
149             } catch (Exception e) {}
150
151             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
152         }
153
154         Assert.fail(String.format("Expected specific message of type %s. Last message received was: %s", clazz, lastMessage));
155         return null;
156     }
157
158     public static <T> void assertNoneMatching(ActorRef actor, Class<T> clazz) {
159         assertNoneMatching(actor, clazz, 5000);
160     }
161
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++) {
165             try {
166                 T message = getFirstMatching(actor, clazz);
167                 if(message != null) {
168                     Assert.fail("Unexpected message received" +  message.toString());
169                     return;
170                 }
171             } catch (Exception e) {}
172
173             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
174         }
175
176         return;
177     }
178
179
180     public static <T> List<T> getAllMatching(ActorRef actor, Class<T> clazz) throws Exception {
181         List<Object> allMessages = getAllMessages(actor);
182
183         List<T> output = Lists.newArrayList();
184
185         for(Object message : allMessages){
186             if(message.getClass().equals(clazz)){
187                 output.add(clazz.cast(message));
188             }
189         }
190
191         return output;
192     }
193
194     public static void waitUntilReady(ActorRef actor) throws Exception {
195         long timeout = 500;
196         FiniteDuration duration = Duration.create(timeout, TimeUnit.MILLISECONDS);
197         for(int i = 0; i < 10; i++) {
198             try {
199                 Await.ready(Patterns.ask(actor, ARE_YOU_READY, timeout), duration);
200                 return;
201             } catch (TimeoutException e) {
202             }
203         }
204
205         throw new TimeoutException("Actor not ready in time.");
206     }
207
208     public static Props props() {
209         return Props.create(MessageCollectorActor.class);
210     }
211 }