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