78872907070415657f61b35397b4ee8d9677e582
[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 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 ShardTestKit extends JavaTestKit {
27
28     protected ShardTestKit(ActorSystem actorSystem) {
29         super(actorSystem);
30     }
31
32     protected void waitForLogMessage(final Class<?> logLevel, ActorRef subject, String logMessage){
33         // Wait for a specific log message to show up
34         final boolean result =
35             new JavaTestKit.EventFilter<Boolean>(logLevel
36             ) {
37                 @Override
38                 protected Boolean run() {
39                     return true;
40                 }
41             }.from(subject.path().toString())
42                 .message(logMessage)
43                 .occurrences(1).exec();
44
45         Assert.assertEquals(true, result);
46
47     }
48
49     protected void waitUntilLeader(ActorRef shard) {
50         FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
51         for(int i = 0; i < 20 * 5; i++) {
52             Future<Object> future = Patterns.ask(shard, new FindLeader(), new Timeout(duration));
53             try {
54                 FindLeaderReply resp = (FindLeaderReply)Await.result(future, duration);
55                 if(resp.getLeaderActor() != null) {
56                     return;
57                 }
58             } catch(TimeoutException e) {
59             } catch(Exception e) {
60                 System.err.println("FindLeader threw ex");
61                 e.printStackTrace();
62             }
63
64
65             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
66         }
67
68         Assert.fail("Leader not found for shard " + shard.path());
69     }
70
71     protected void waitUntilNoLeader(ActorRef shard) {
72         FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
73         for(int i = 0; i < 20 * 5; i++) {
74             Future<Object> future = Patterns.ask(shard, new FindLeader(), new Timeout(duration));
75             try {
76                 FindLeaderReply resp = (FindLeaderReply)Await.result(future, duration);
77                 if(resp.getLeaderActor() == null) {
78                     return;
79                 }
80             } catch(TimeoutException e) {
81             } catch(Exception e) {
82                 System.err.println("FindLeader threw ex");
83                 e.printStackTrace();
84             }
85
86
87             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
88         }
89
90         Assert.fail("Unexpected leader found for shard " + shard.path());
91     }
92 }