Bug 4992: Removed old leader's candidates on leader change
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / MemberNode.java
index e966c95..5ae5331 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
@@ -17,6 +17,7 @@ import akka.cluster.Cluster;
 import akka.cluster.ClusterEvent.CurrentClusterState;
 import akka.cluster.Member;
 import akka.cluster.MemberStatus;
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Stopwatch;
 import com.google.common.collect.Sets;
@@ -99,6 +100,28 @@ public class MemberNode {
         fail("Member(s) " + otherMembersSet + " are not Up");
     }
 
+    public void waitForMemberDown(String member) {
+        Stopwatch sw = Stopwatch.createStarted();
+        while(sw.elapsed(TimeUnit.SECONDS) <= 10) {
+            CurrentClusterState state = Cluster.get(kit.getSystem()).state();
+            for(Member m: state.getUnreachable()) {
+                if(member.equals(m.getRoles().iterator().next())) {
+                    return;
+                }
+            }
+
+            for(Member m: state.getMembers()) {
+                if(m.status() != MemberStatus.up() && member.equals(m.getRoles().iterator().next())) {
+                    return;
+                }
+            }
+
+            Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
+        }
+
+        fail("Member " + member + " is now down");
+    }
+
     public void cleanup() {
         if(!cleanedUp) {
             cleanedUp = true;
@@ -138,19 +161,31 @@ public class MemberNode {
         final Set<String> peerIds = Sets.newHashSet();
         for(String p: peerMemberNames) {
             peerIds.add(ShardIdentifier.builder().memberName(p).shardName(shardName).
-                type(datastore.getActorContext().getDataStoreType()).build().toString());
+                type(datastore.getActorContext().getDataStoreName()).build().toString());
         }
 
         verifyRaftState(datastore, shardName, new RaftStateVerifier() {
             @Override
             public void verify(OnDemandRaftState raftState) {
-                assertTrue(String.format("Peer(s) %s not found for shard %s. Actual: %s", peerIds, shardName,
-                        raftState.getPeerAddresses().keySet()),
-                        raftState.getPeerAddresses().keySet().containsAll(peerIds));
+                assertEquals("Peers for shard " + shardName, peerIds, raftState.getPeerAddresses().keySet());
             }
         });
     }
 
+    public static void verifyNoShardPresent(DistributedDataStore datastore, String shardName) {
+        Stopwatch sw = Stopwatch.createStarted();
+        while(sw.elapsed(TimeUnit.SECONDS) <= 5) {
+            Optional<ActorRef> shardReply = datastore.getActorContext().findLocalShard(shardName);
+            if(!shardReply.isPresent()) {
+                return;
+            }
+
+            Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
+        }
+
+        fail("Shard " + shardName + " is present");
+    }
+
     public static class Builder {
         private final List<MemberNode> members;
         private String moduleShardsConfig;