Reduce use of scala.concurrent.duration.Duration
[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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
15 import akka.pattern.Patterns;
16 import akka.testkit.javadsl.EventFilter;
17 import akka.testkit.javadsl.TestKit;
18 import akka.util.Timeout;
19 import com.google.common.util.concurrent.Uninterruptibles;
20 import java.util.Optional;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
24 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import scala.concurrent.Await;
28 import scala.concurrent.Future;
29 import scala.concurrent.duration.FiniteDuration;
30
31 public class ShardTestKit extends TestKit {
32     private static final Logger LOG = LoggerFactory.getLogger(ShardTestKit.class);
33
34     public ShardTestKit(final ActorSystem actorSystem) {
35         super(actorSystem);
36     }
37
38     public void waitForLogMessage(final Class<?> logLevel, final ActorRef subject, final String logMessage) {
39         // Wait for a specific log message to show up
40         final Boolean result = new EventFilter(logLevel, getSystem()).from(subject.path().toString())
41                 .message(logMessage).occurrences(1).intercept(() -> Boolean.TRUE);
42         assertEquals(Boolean.TRUE, result);
43     }
44
45     @SuppressWarnings("checkstyle:IllegalCatch")
46     public static String waitUntilLeader(final ActorRef shard) {
47         FiniteDuration duration = FiniteDuration.create(100, TimeUnit.MILLISECONDS);
48         for (int i = 0; i < 20 * 5; i++) {
49             Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
50             try {
51                 final Optional<String> maybeLeader = ((FindLeaderReply) Await.result(future, duration))
52                         .getLeaderActor();
53                 if (maybeLeader.isPresent()) {
54                     return maybeLeader.get();
55                 }
56             } catch (TimeoutException e) {
57                 LOG.trace("FindLeader timed out", e);
58             } catch (Exception e) {
59                 LOG.error("FindLeader failed", e);
60             }
61
62             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
63         }
64
65         fail("Leader not found for shard " + shard.path());
66         return null;
67     }
68
69     @SuppressWarnings("checkstyle:IllegalCatch")
70     public void waitUntilNoLeader(final ActorRef shard) {
71         FiniteDuration duration = FiniteDuration.create(100, TimeUnit.MILLISECONDS);
72         Object lastResponse = null;
73         for (int i = 0; i < 20 * 5; i++) {
74             Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
75             try {
76                 final Optional<String> maybeLeader = ((FindLeaderReply) Await.result(future, duration))
77                         .getLeaderActor();
78                 if (!maybeLeader.isPresent()) {
79                     return;
80                 }
81
82                 lastResponse = maybeLeader.get();
83             } catch (TimeoutException e) {
84                 lastResponse = e;
85             } catch (Exception e) {
86                 LOG.error("FindLeader failed", e);
87                 lastResponse = e;
88             }
89
90             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
91         }
92
93         if (lastResponse instanceof Throwable) {
94             throw (AssertionError)new AssertionError(
95                     String.format("Unexpected error occurred from FindLeader for shard %s", shard.path()))
96                             .initCause((Throwable)lastResponse);
97         }
98
99         fail(String.format("Unexpected leader %s found for shard %s", lastResponse, shard.path()));
100     }
101 }