Migrate RaftActorTestKit to javadsl.TestKit
[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.javadsl.EventFilter;
14 import akka.testkit.javadsl.TestKit;
15 import akka.util.Timeout;
16 import com.google.common.util.concurrent.Uninterruptibles;
17 import java.util.Optional;
18 import java.util.concurrent.TimeUnit;
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 TestKit {
30     private static final Logger LOG = LoggerFactory.getLogger(RaftActorTestKit.class);
31     private final ActorRef raftActor;
32
33     public RaftActorTestKit(final ActorSystem actorSystem, final String actorName) {
34         super(actorSystem);
35         raftActor = this.getSystem().actorOf(MockRaftActor.builder().id(actorName).props(), actorName);
36     }
37
38     public ActorRef getRaftActor() {
39         return raftActor;
40     }
41
42     public boolean waitForLogMessage(final Class<?> logEventClass, final String message) {
43         // Wait for a specific log message to show up
44         return new EventFilter(logEventClass, getSystem()).from(raftActor.path().toString()).message(message)
45                 .occurrences(1).intercept(() -> Boolean.TRUE);
46     }
47
48     protected void waitUntilLeader() {
49         waitUntilLeader(raftActor);
50     }
51
52     @SuppressWarnings("checkstyle:IllegalCatch")
53     public static void waitUntilLeader(final ActorRef actorRef) {
54         FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
55         for (int i = 0; i < 20 * 5; i++) {
56             Future<Object> future = Patterns.ask(actorRef, FindLeader.INSTANCE, new Timeout(duration));
57             try {
58                 final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
59                 if (maybeLeader.isPresent()) {
60                     return;
61                 }
62             } catch (Exception e) {
63                 LOG.error("FindLeader failed", e);
64             }
65
66             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
67         }
68
69         Assert.fail("Leader not found for actorRef " + actorRef.path());
70     }
71 }