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