Merge "Fix for Bug 2727 - Upgrade Akka from 2.3.4 to 2.3.9"
[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 org.junit.Assert;
22 import scala.concurrent.Await;
23 import scala.concurrent.Future;
24 import scala.concurrent.duration.Duration;
25 import scala.concurrent.duration.FiniteDuration;
26
27
28 public class MessageCollectorActor extends UntypedActor {
29     private static final String ARE_YOU_READY = "ARE_YOU_READY";
30
31     private final List<Object> messages = new ArrayList<>();
32
33     @Override public void onReceive(Object message) throws Exception {
34         if(message.equals(ARE_YOU_READY)) {
35             getSender().tell("yes", getSelf());
36             return;
37         }
38
39         if(message instanceof String){
40             if("get-all-messages".equals(message)){
41                 getSender().tell(new ArrayList<>(messages), getSelf());
42             }
43         } else if(message != null) {
44             messages.add(message);
45         }
46     }
47
48     public void clear() {
49         messages.clear();
50     }
51
52     public static List<Object> getAllMessages(ActorRef actor) throws Exception {
53         FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
54         Timeout operationTimeout = new Timeout(operationDuration);
55         Future<Object> future = Patterns.ask(actor, "get-all-messages", operationTimeout);
56
57         return (List<Object>) Await.result(future, operationDuration);
58     }
59
60     /**
61      * Get the first message that matches the specified class
62      * @param actor
63      * @param clazz
64      * @return
65      */
66     public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
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         return null;
76     }
77
78     public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
79         return expectFirstMatching(actor, clazz, 5000);
80     }
81
82     public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz, long timeout) {
83         int count = (int) (timeout / 50);
84         for(int i = 0; i < count; i++) {
85             try {
86                 T message = getFirstMatching(actor, clazz);
87                 if(message != null) {
88                     return message;
89                 }
90             } catch (Exception e) {}
91
92             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
93         }
94
95         Assert.fail("Did not receive message of type " + clazz);
96         return null;
97     }
98
99     public static <T> List<T> getAllMatching(ActorRef actor, Class<T> clazz) throws Exception {
100         List<Object> allMessages = getAllMessages(actor);
101
102         List<T> output = Lists.newArrayList();
103
104         for(Object message : allMessages){
105             if(message.getClass().equals(clazz)){
106                 output.add((T) message);
107             }
108         }
109
110         return output;
111     }
112
113     public static void waitUntilReady(ActorRef actor) throws Exception {
114         long timeout = 500;
115         FiniteDuration duration = Duration.create(timeout, TimeUnit.MILLISECONDS);
116         for(int i = 0; i < 10; i++) {
117             try {
118                 Await.ready(Patterns.ask(actor, ARE_YOU_READY, timeout), duration);
119                 return;
120             } catch (TimeoutException e) {
121             }
122         }
123
124         throw new TimeoutException("Actor not ready in time.");
125     }
126 }