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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.