BUG 4213 : Candidate should switch to Follower when it receives AppendEntries from...
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / AbstractRaftActorIntegrationTest.java
index 579dea26387059f30d7bbc0f0064d91f195006ad..7cd893691284b7349e4d09efacd9911fdeae4025 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.controller.cluster.raft;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import akka.actor.ActorRef;
 import akka.actor.InvalidActorNameException;
 import akka.actor.PoisonPill;
@@ -49,6 +50,24 @@ import scala.concurrent.duration.FiniteDuration;
  */
 public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest {
 
+    public static class SetPeerAddress {
+        private final String peerId;
+        private final String peerAddress;
+
+        public SetPeerAddress(String peerId, String peerAddress) {
+            this.peerId = peerId;
+            this.peerAddress = peerAddress;
+        }
+
+        public String getPeerId() {
+            return peerId;
+        }
+
+        public String getPeerAddress() {
+            return peerAddress;
+        }
+    }
+
     public static class TestRaftActor extends MockRaftActor {
 
         private final TestActorRef<MessageCollectorActor> collectorActor;
@@ -96,6 +115,12 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest
                 return;
             }
 
+            if(message instanceof SetPeerAddress) {
+                setPeerAddress(((SetPeerAddress) message).getPeerId().toString(),
+                        ((SetPeerAddress) message).getPeerAddress());
+                return;
+            }
+
             try {
                 if(!dropMessages.containsKey(message.getClass())) {
                     super.handleCommand(message);
@@ -153,7 +178,9 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest
     protected long initialTerm = 5;
     protected long currentTerm;
 
-    protected List<Object> expSnapshotState = new ArrayList<>();
+    protected int snapshotBatchCount = 4;
+
+    protected List<MockPayload> expSnapshotState = new ArrayList<>();
 
     @After
     public void tearDown() {
@@ -166,7 +193,7 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest
         DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
         configParams.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
         configParams.setElectionTimeoutFactor(1);
-        configParams.setSnapshotBatchCount(4);
+        configParams.setSnapshotBatchCount(snapshotBatchCount);
         configParams.setSnapshotDataThresholdPercentage(70);
         configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
         return configParams;
@@ -201,6 +228,7 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest
             }
         }
 
+        assertNotNull(lastEx);
         throw lastEx;
     }
 
@@ -225,7 +253,7 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest
 
     @SuppressWarnings("unchecked")
     protected void verifySnapshot(String prefix, Snapshot snapshot, long lastAppliedTerm,
-            int lastAppliedIndex, long lastTerm, long lastIndex)
+            long lastAppliedIndex, long lastTerm, long lastIndex)
                     throws Exception {
         assertEquals(prefix + " Snapshot getLastAppliedTerm", lastAppliedTerm, snapshot.getLastAppliedTerm());
         assertEquals(prefix + " Snapshot getLastAppliedIndex", lastAppliedIndex, snapshot.getLastAppliedIndex());
@@ -233,7 +261,8 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest
         assertEquals(prefix + " Snapshot getLastIndex", lastIndex, snapshot.getLastIndex());
 
         List<Object> actualState = (List<Object>)MockRaftActor.toObject(snapshot.getState());
-        assertEquals(prefix + " Snapshot getState size", expSnapshotState.size(), actualState.size());
+        assertEquals(String.format("%s Snapshot getState size. Expected %s: . Actual: %s", prefix, expSnapshotState,
+                actualState), expSnapshotState.size(), actualState.size());
         for(int i = 0; i < expSnapshotState.size(); i++) {
             assertEquals(prefix + " Snapshot state " + i, expSnapshotState.get(i), actualState.get(i));
         }
@@ -283,4 +312,32 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest
     protected String testActorPath(String id){
         return "akka://test/user" + id;
     }
+
+    protected void verifyLeadersTrimmedLog(long lastIndex) {
+        verifyTrimmedLog("Leader", leaderActor, lastIndex, lastIndex - 1);
+    }
+
+    protected void verifyLeadersTrimmedLog(long lastIndex, long replicatedToAllIndex) {
+        verifyTrimmedLog("Leader", leaderActor, lastIndex, replicatedToAllIndex);
+    }
+
+    protected void verifyFollowersTrimmedLog(int num, TestActorRef<TestRaftActor> actorRef, long lastIndex) {
+        verifyTrimmedLog("Follower " + num, actorRef, lastIndex, lastIndex - 1);
+    }
+
+    protected void verifyTrimmedLog(String name, TestActorRef<TestRaftActor> actorRef, long lastIndex,
+            long replicatedToAllIndex) {
+        TestRaftActor actor = actorRef.underlyingActor();
+        RaftActorContext context = actor.getRaftActorContext();
+        long snapshotIndex = lastIndex - 1;
+        assertEquals(name + " snapshot term", snapshotIndex < 0 ? -1 : currentTerm,
+                context.getReplicatedLog().getSnapshotTerm());
+        assertEquals(name + " snapshot index", snapshotIndex, context.getReplicatedLog().getSnapshotIndex());
+        assertEquals(name + " journal log size", 1, context.getReplicatedLog().size());
+        assertEquals(name + " journal last index", lastIndex, context.getReplicatedLog().lastIndex());
+        assertEquals(name + " commit index", lastIndex, context.getCommitIndex());
+        assertEquals(name + " last applied", lastIndex, context.getLastApplied());
+        assertEquals(name + " replicatedToAllIndex", replicatedToAllIndex,
+                actor.getCurrentBehavior().getReplicatedToAllIndex());
+    }
 }