Merge "Cleanup RpcRoutingStrategy definition"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTestKit.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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.datastore;
9
10 import java.util.concurrent.TimeUnit;
11 import org.junit.Assert;
12 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
13 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
14 import com.google.common.util.concurrent.Uninterruptibles;
15 import scala.concurrent.Await;
16 import scala.concurrent.Future;
17 import scala.concurrent.duration.Duration;
18 import akka.actor.ActorRef;
19 import akka.actor.ActorSystem;
20 import akka.pattern.Patterns;
21 import akka.testkit.JavaTestKit;
22 import akka.util.Timeout;
23
24 class ShardTestKit extends JavaTestKit {
25
26     ShardTestKit(ActorSystem actorSystem) {
27         super(actorSystem);
28     }
29
30     protected void waitForLogMessage(final Class logLevel, ActorRef subject, String logMessage){
31         // Wait for a specific log message to show up
32         final boolean result =
33             new JavaTestKit.EventFilter<Boolean>(logLevel
34             ) {
35                 @Override
36                 protected Boolean run() {
37                     return true;
38                 }
39             }.from(subject.path().toString())
40                 .message(logMessage)
41                 .occurrences(1).exec();
42
43         Assert.assertEquals(true, result);
44
45     }
46
47     protected void waitUntilLeader(ActorRef shard) {
48         for(int i = 0; i < 20 * 5; i++) {
49             Future<Object> future = Patterns.ask(shard, new FindLeader(), new Timeout(5, TimeUnit.SECONDS));
50             try {
51                 FindLeaderReply resp = (FindLeaderReply)Await.result(future, Duration.create(5, TimeUnit.SECONDS));
52                 if(resp.getLeaderActor() != null) {
53                     return;
54                 }
55             } catch (Exception e) {
56                 e.printStackTrace();
57             }
58
59             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
60         }
61
62         Assert.fail("Leader not found for shard " + shard.path());
63     }
64 }