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 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;
29 public class ShardTestKit extends JavaTestKit {
30 private static final Logger LOG = LoggerFactory.getLogger(ShardTestKit.class);
32 public ShardTestKit(ActorSystem actorSystem) {
36 public void waitForLogMessage(final Class<?> logLevel, ActorRef subject, String logMessage){
37 // Wait for a specific log message to show up
38 final boolean result =
39 new JavaTestKit.EventFilter<Boolean>(logLevel
42 protected Boolean run() {
45 }.from(subject.path().toString())
47 .occurrences(1).exec();
49 Assert.assertEquals(true, result);
53 public static String waitUntilLeader(ActorRef shard) {
54 FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
55 for(int i = 0; i < 20 * 5; i++) {
56 Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
58 final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
59 if (maybeLeader.isPresent()) {
60 return maybeLeader.get();
62 } catch(TimeoutException e) {
63 LOG.trace("FindLeader timed out", e);
64 } catch(Exception e) {
65 LOG.error("FindLeader failed", e);
68 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
71 Assert.fail("Leader not found for shard " + shard.path());
75 public void waitUntilNoLeader(ActorRef shard) {
76 FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
77 Object lastResponse = null;
78 for(int i = 0; i < 20 * 5; i++) {
79 Future<Object> future = Patterns.ask(shard, FindLeader.INSTANCE, new Timeout(duration));
81 final Optional<String> maybeLeader = ((FindLeaderReply)Await.result(future, duration)).getLeaderActor();
82 if (!maybeLeader.isPresent()) {
86 lastResponse = maybeLeader.get();
87 } catch(TimeoutException e) {
89 } catch(Exception e) {
90 LOG.error("FindLeader failed", e);
94 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
97 if(lastResponse instanceof Throwable) {
98 throw (AssertionError)new AssertionError(
99 String.format("Unexpected error occurred from FindLeader for shard %s", shard.path())).
100 initCause((Throwable)lastResponse);
103 Assert.fail(String.format("Unexpected leader %s found for shard %s", lastResponse, shard.path()));