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 protected ShardTestKit(ActorSystem actorSystem) {
32 protected 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 protected void 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) {
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());
71 protected void waitUntilNoLeader(ActorRef shard) {
72 FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
73 for(int i = 0; i < 20 * 5; i++) {
74 Future<Object> future = Patterns.ask(shard, new FindLeader(), new Timeout(duration));
76 FindLeaderReply resp = (FindLeaderReply)Await.result(future, duration);
77 if(resp.getLeaderActor() == null) {
80 } catch(TimeoutException e) {
81 } catch(Exception e) {
82 System.err.println("FindLeader threw ex");
87 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
90 Assert.fail("Unexpected leader found for shard " + shard.path());