df1359fc47d6d0e9738bd8150d6e78ddb8d0d85f
[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.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 ShardTestKit extends JavaTestKit {
30     private static final Logger LOG = LoggerFactory.getLogger(ShardTestKit.class);
31
32     public ShardTestKit(final ActorSystem actorSystem) {
33         super(actorSystem);
34     }
35
36     public void waitForLogMessage(final Class<?> logLevel, final ActorRef subject, final String logMessage) {
37         // Wait for a specific log message to show up
38         final boolean result =
39             new JavaTestKit.EventFilter<Boolean>(logLevel
40             ) {
41                 @Override
42                 protected Boolean run() {
43                     return true;
44                 }
45             }.from(subject.path().toString())
46                 .message(logMessage)
47                 .occurrences(1).exec();
48
49         Assert.assertEquals(true, result);
50
51     }
52
53     @SuppressWarnings("checkstyle:IllegalCatch")
54     public static String waitUntilLeader(final ActorRef shard) {
55         FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
56         for (int i = 0; i < 20 * 5; i++) {
57             Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
58             try {
59                 final Optional<String> maybeLeader = ((FindLeaderReply) Await.result(future, duration))
60                         .getLeaderActor();
61                 if (maybeLeader.isPresent()) {
62                     return maybeLeader.get();
63                 }
64             } catch (TimeoutException e) {
65                 LOG.trace("FindLeader timed out", e);
66             } catch (Exception e) {
67                 LOG.error("FindLeader failed", e);
68             }
69
70             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
71         }
72
73         Assert.fail("Leader not found for shard " + shard.path());
74         return null;
75     }
76
77     @SuppressWarnings("checkstyle:IllegalCatch")
78     public void waitUntilNoLeader(final ActorRef shard) {
79         FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
80         Object lastResponse = null;
81         for (int i = 0; i < 20 * 5; i++) {
82             Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
83             try {
84                 final Optional<String> maybeLeader = ((FindLeaderReply) Await.result(future, duration))
85                         .getLeaderActor();
86                 if (!maybeLeader.isPresent()) {
87                     return;
88                 }
89
90                 lastResponse = maybeLeader.get();
91             } catch (TimeoutException e) {
92                 lastResponse = e;
93             } catch (Exception e) {
94                 LOG.error("FindLeader failed", e);
95                 lastResponse = e;
96             }
97
98             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
99         }
100
101         if (lastResponse instanceof Throwable) {
102             throw (AssertionError)new AssertionError(
103                     String.format("Unexpected error occurred from FindLeader for shard %s", shard.path()))
104                             .initCause((Throwable)lastResponse);
105         }
106
107         Assert.fail(String.format("Unexpected leader %s found for shard %s", lastResponse, shard.path()));
108     }
109 }