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.Duration;
30 import scala.concurrent.duration.FiniteDuration;
32 public class ShardTestKit extends TestKit {
33 private static final Logger LOG = LoggerFactory.getLogger(ShardTestKit.class);
35 public ShardTestKit(final ActorSystem actorSystem) {
39 public void waitForLogMessage(final Class<?> logLevel, final ActorRef subject, final String logMessage) {
40 // Wait for a specific log message to show up
41 final Boolean result = new EventFilter(logLevel, getSystem()).from(subject.path().toString())
42 .message(logMessage).occurrences(1).intercept(() -> Boolean.TRUE);
43 assertEquals(Boolean.TRUE, result);
46 @SuppressWarnings("checkstyle:IllegalCatch")
47 public static String waitUntilLeader(final ActorRef shard) {
48 FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
49 for (int i = 0; i < 20 * 5; i++) {
50 Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
52 final Optional<String> maybeLeader = ((FindLeaderReply) Await.result(future, duration))
54 if (maybeLeader.isPresent()) {
55 return maybeLeader.get();
57 } catch (TimeoutException e) {
58 LOG.trace("FindLeader timed out", e);
59 } catch (Exception e) {
60 LOG.error("FindLeader failed", e);
63 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
66 fail("Leader not found for shard " + shard.path());
70 @SuppressWarnings("checkstyle:IllegalCatch")
71 public void waitUntilNoLeader(final ActorRef shard) {
72 FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
73 Object lastResponse = null;
74 for (int i = 0; i < 20 * 5; i++) {
75 Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
77 final Optional<String> maybeLeader = ((FindLeaderReply) Await.result(future, duration))
79 if (!maybeLeader.isPresent()) {
83 lastResponse = maybeLeader.get();
84 } catch (TimeoutException e) {
86 } catch (Exception e) {
87 LOG.error("FindLeader failed", e);
91 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
94 if (lastResponse instanceof Throwable) {
95 throw (AssertionError)new AssertionError(
96 String.format("Unexpected error occurred from FindLeader for shard %s", shard.path()))
97 .initCause((Throwable)lastResponse);
100 fail(String.format("Unexpected leader %s found for shard %s", lastResponse, shard.path()));