Bug 8206: Prevent decr follower next index beyong -1
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / FollowerLogInformationImplTest.java
index 843b9bebbd3956a3bda56c251a03e697bbc72867..d68339ee8664f445b02a54cc46fb705bb3bf559e 100644 (file)
@@ -7,8 +7,10 @@
  */
 package org.opendaylight.controller.cluster.raft;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+
 import com.google.common.base.Stopwatch;
 import com.google.common.util.concurrent.Uninterruptibles;
 import java.util.concurrent.TimeUnit;
@@ -62,7 +64,7 @@ public class FollowerLogInformationImplTest {
     }
 
     @Test
-    public void testOkToReplicate(){
+    public void testOkToReplicate() {
         MockRaftActorContext context = new MockRaftActorContext();
         context.setCommitIndex(0);
         FollowerLogInformation followerLogInformation =
@@ -111,4 +113,21 @@ public class FollowerLogInformationImplTest {
         followerLogInformation.markFollowerActive();
         assertTrue(followerLogInformation.isFollowerActive());
     }
+
+    @Test
+    public void testDecrNextIndex() {
+        MockRaftActorContext context = new MockRaftActorContext();
+        context.setCommitIndex(1);
+        FollowerLogInformation followerLogInformation =
+                new FollowerLogInformationImpl(new PeerInfo("follower1", null, VotingState.VOTING), 1, context);
+
+        assertTrue(followerLogInformation.decrNextIndex());
+        assertEquals("getNextIndex", 0, followerLogInformation.getNextIndex());
+
+        assertTrue(followerLogInformation.decrNextIndex());
+        assertEquals("getNextIndex", -1, followerLogInformation.getNextIndex());
+
+        assertFalse(followerLogInformation.decrNextIndex());
+        assertEquals("getNextIndex", -1, followerLogInformation.getNextIndex());
+    }
 }