2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
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;
31 public class ShardTestKit extends TestKit {
32 private static final Logger LOG = LoggerFactory.getLogger(ShardTestKit.class);
34 public ShardTestKit(final ActorSystem actorSystem) {
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);
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));
51 final Optional<String> maybeLeader = ((FindLeaderReply) Await.result(future, duration))
53 if (maybeLeader.isPresent()) {
54 return maybeLeader.get();
56 } catch (TimeoutException e) {
57 LOG.trace("FindLeader timed out", e);
58 } catch (Exception e) {
59 LOG.error("FindLeader failed", e);
62 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
65 fail("Leader not found for shard " + shard.path());
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));
76 final Optional<String> maybeLeader = ((FindLeaderReply) Await.result(future, duration))
78 if (!maybeLeader.isPresent()) {
82 lastResponse = maybeLeader.get();
83 } catch (TimeoutException e) {
85 } catch (Exception e) {
86 LOG.error("FindLeader failed", e);
90 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
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);
99 fail(String.format("Unexpected leader %s found for shard %s", lastResponse, shard.path()));