3e747e387ee1f881ba0141f1042cd837430b1ae2
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorTestKit.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.cluster.raft;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.pattern.Patterns;
13 import akka.testkit.JavaTestKit;
14 import akka.util.Timeout;
15 import com.google.common.base.Optional;
16 import com.google.common.util.concurrent.Uninterruptibles;
17 import java.util.Collections;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
20 import org.junit.Assert;
21 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
22 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
23 import scala.concurrent.Await;
24 import scala.concurrent.Future;
25 import scala.concurrent.duration.Duration;
26 import scala.concurrent.duration.FiniteDuration;
27
28 public class RaftActorTestKit extends JavaTestKit {
29     private final ActorRef raftActor;
30
31     public RaftActorTestKit(ActorSystem actorSystem, String actorName) {
32         super(actorSystem);
33
34         raftActor = this.getSystem().actorOf(MockRaftActor.props(actorName,
35                 Collections.<String,String>emptyMap(), Optional.<ConfigParams>absent()), actorName);
36
37     }
38
39
40     public ActorRef getRaftActor() {
41         return raftActor;
42     }
43
44     public boolean waitForLogMessage(final Class<?> logEventClass, String message){
45         // Wait for a specific log message to show up
46         return
47             new JavaTestKit.EventFilter<Boolean>(logEventClass
48             ) {
49                 @Override
50                 protected Boolean run() {
51                     return true;
52                 }
53             }.from(raftActor.path().toString())
54                 .message(message)
55                 .occurrences(1).exec();
56
57
58     }
59
60     protected void waitUntilLeader(){
61         waitUntilLeader(raftActor);
62     }
63
64     public static void waitUntilLeader(ActorRef actorRef) {
65         FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
66         for(int i = 0; i < 20 * 5; i++) {
67             Future<Object> future = Patterns.ask(actorRef, new FindLeader(), new Timeout(duration));
68             try {
69                 FindLeaderReply resp = (FindLeaderReply) Await.result(future, duration);
70                 if(resp.getLeaderActor() != null) {
71                     return;
72                 }
73             } catch(TimeoutException e) {
74             } catch(Exception e) {
75                 System.err.println("FindLeader threw ex");
76                 e.printStackTrace();
77             }
78
79
80             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
81         }
82
83         Assert.fail("Leader not found for actorRef " + actorRef.path());
84     }
85
86 }