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 org.junit.Assert;
17 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
18 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
19 import scala.concurrent.Await;
20 import scala.concurrent.Future;
21 import scala.concurrent.duration.Duration;
22 import scala.concurrent.duration.FiniteDuration;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
27 class ShardTestKit extends JavaTestKit {
29 ShardTestKit(ActorSystem actorSystem) {
33 protected void waitForLogMessage(final Class<?> logLevel, ActorRef subject, String logMessage){
34 // Wait for a specific log message to show up
35 final boolean result =
36 new JavaTestKit.EventFilter<Boolean>(logLevel
39 protected Boolean run() {
42 }.from(subject.path().toString())
44 .occurrences(1).exec();
46 Assert.assertEquals(true, result);
50 protected void waitUntilLeader(ActorRef shard) {
51 FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
52 for(int i = 0; i < 20 * 5; i++) {
53 Future<Object> future = Patterns.ask(shard, new FindLeader(), new Timeout(duration));
55 FindLeaderReply resp = (FindLeaderReply)Await.result(future, duration);
56 if(resp.getLeaderActor() != null) {
59 } catch(TimeoutException e) {
60 } catch(Exception e) {
61 System.err.println("FindLeader threw ex");
66 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
69 Assert.fail("Leader not found for shard " + shard.path());