Merge "Try to eliminate random failures in RaftActorTest"
[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 java.util.concurrent.TimeoutException;
21 import scala.concurrent.Await;
22 import scala.concurrent.Future;
23 import scala.concurrent.duration.Duration;
24 import scala.concurrent.duration.FiniteDuration;
25
26
27 public class MessageCollectorActor extends UntypedActor {
28     private static final String ARE_YOU_READY = "ARE_YOU_READY";
29
30     private final List<Object> messages = new ArrayList<>();
31
32     @Override public void onReceive(Object message) throws Exception {
33         if(message.equals(ARE_YOU_READY)) {
34             getSender().tell("yes", getSelf());
35             return;
36         }
37
38         if(message instanceof String){
39             if("get-all-messages".equals(message)){
40                 getSender().tell(new ArrayList<>(messages), getSelf());
41             }
42         } else if(message != null) {
43             messages.add(message);
44         }
45     }
46
47     public void clear() {
48         messages.clear();
49     }
50
51     public static List<Object> getAllMessages(ActorRef actor) throws Exception {
52         FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
53         Timeout operationTimeout = new Timeout(operationDuration);
54         Future<Object> future = Patterns.ask(actor, "get-all-messages", operationTimeout);
55
56         return (List<Object>) Await.result(future, operationDuration);
57     }
58
59     /**
60      * Get the first message that matches the specified class
61      * @param actor
62      * @param clazz
63      * @return
64      */
65     public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
66         for(int i = 0; i < 50; i++) {
67             List<Object> allMessages = getAllMessages(actor);
68
69             for(Object message : allMessages){
70                 if(message.getClass().equals(clazz)){
71                     return (T) message;
72                 }
73             }
74
75             Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
76         }
77
78         return null;
79     }
80
81     public static List<Object> getAllMatching(ActorRef actor, Class<?> clazz) throws Exception {
82         List<Object> allMessages = getAllMessages(actor);
83
84         List<Object> output = Lists.newArrayList();
85
86         for(Object message : allMessages){
87             if(message.getClass().equals(clazz)){
88                 output.add(message);
89             }
90         }
91
92         return output;
93     }
94
95     public static void waitUntilReady(ActorRef actor) throws Exception {
96         long timeout = 500;
97         FiniteDuration duration = Duration.create(timeout, TimeUnit.MILLISECONDS);
98         for(int i = 0; i < 10; i++) {
99             try {
100                 Await.ready(Patterns.ask(actor, ARE_YOU_READY, timeout), duration);
101                 return;
102             } catch (TimeoutException e) {
103             }
104         }
105
106         throw new TimeoutException("Actor not ready in time.");
107     }
108 }