Implement suspend leader in Shard
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTestKit.java
index ae607b9b3da214094d44833de1aea900ecb36d69..8f3231b4179ce2c3b086fe083c61fcf55f084574 100644 (file)
@@ -46,7 +46,7 @@ public class ShardTestKit extends JavaTestKit {
 
     }
 
-    public String waitUntilLeader(ActorRef shard) {
+    public static String waitUntilLeader(ActorRef shard) {
         FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
         for(int i = 0; i < 20 * 5; i++) {
             Future<Object> future = Patterns.ask(shard, new FindLeader(), new Timeout(duration));
@@ -71,6 +71,7 @@ public class ShardTestKit extends JavaTestKit {
 
     public void waitUntilNoLeader(ActorRef shard) {
         FiniteDuration duration = Duration.create(100, TimeUnit.MILLISECONDS);
+        Object lastResponse = null;
         for(int i = 0; i < 20 * 5; i++) {
             Future<Object> future = Patterns.ask(shard, new FindLeader(), new Timeout(duration));
             try {
@@ -78,16 +79,25 @@ public class ShardTestKit extends JavaTestKit {
                 if(resp.getLeaderActor() == null) {
                     return;
                 }
+
+                lastResponse = resp.getLeaderActor();
             } catch(TimeoutException e) {
+                lastResponse = e;
             } catch(Exception e) {
                 System.err.println("FindLeader threw ex");
                 e.printStackTrace();
+                lastResponse = e;
             }
 
-
             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
         }
 
-        Assert.fail("Unexpected leader found for shard " + shard.path());
+        if(lastResponse instanceof Throwable) {
+            throw (AssertionError)new AssertionError(
+                    String.format("Unexpected error occurred from FindLeader for shard %s", shard.path())).
+                            initCause((Throwable)lastResponse);
+        }
+
+        Assert.fail(String.format("Unexpected leader %s found for shard %s", lastResponse, shard.path()));
     }
 }
\ No newline at end of file