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.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.junit.Assert;
19 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
20 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
21 import scala.concurrent.Await;
22 import scala.concurrent.Future;
23 import scala.concurrent.duration.Duration;
24 import scala.concurrent.duration.FiniteDuration;
26 public class ShardTestKit extends JavaTestKit {
28 public ShardTestKit(ActorSystem actorSystem) {
32 public void waitForLogMessage(final Class<?> logLevel, ActorRef subject, String logMessage){
33 // Wait for a specific log message to show up
34 final boolean result =
35 new JavaTestKit.EventFilter<Boolean>(logLevel
38 protected Boolean run() {
41 }.from(subject.path().toString())
43 .occurrences(1).exec();
45 Assert.assertEquals(true, result);
49 public static String waitUntilLeader(ActorRef shard) {
50 FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
51 for(int i = 0; i < 20 * 5; i++) {
52 Future<Object> future = Patterns.ask(shard, new FindLeader(), new Timeout(duration));
54 FindLeaderReply resp = (FindLeaderReply)Await.result(future, duration);
55 if(resp.getLeaderActor() != null) {
56 return resp.getLeaderActor();
58 } catch(TimeoutException e) {
59 } catch(Exception e) {
60 System.err.println("FindLeader threw ex");
65 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
68 Assert.fail("Leader not found for shard " + shard.path());
72 public void waitUntilNoLeader(ActorRef shard) {
73 FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
74 Object lastResponse = null;
75 for(int i = 0; i < 20 * 5; i++) {
76 Future<Object> future = Patterns.ask(shard, new FindLeader(), new Timeout(duration));
78 FindLeaderReply resp = (FindLeaderReply)Await.result(future, duration);
79 if(resp.getLeaderActor() == null) {
83 lastResponse = resp.getLeaderActor();
84 } catch(TimeoutException e) {
86 } catch(Exception e) {
87 System.err.println("FindLeader threw ex");
92 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
95 if(lastResponse instanceof Throwable) {
96 throw (AssertionError)new AssertionError(
97 String.format("Unexpected error occurred from FindLeader for shard %s", shard.path())).
98 initCause((Throwable)lastResponse);
101 Assert.fail(String.format("Unexpected leader %s found for shard %s", lastResponse, shard.path()));