X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FFollowerLogInformationImplTest.java;h=d68339ee8664f445b02a54cc46fb705bb3bf559e;hp=a092c46533d251414ee7f22cf843c4fc3765f691;hb=29bebec0bb224893a163cb5df9336c37d41c350f;hpb=f90815e94e325bb2f80c2f6228a2a8d04a2706ab diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/FollowerLogInformationImplTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/FollowerLogInformationImplTest.java index a092c46533..d68339ee86 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/FollowerLogInformationImplTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/FollowerLogInformationImplTest.java @@ -7,36 +7,38 @@ */ 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; import org.junit.Test; import scala.concurrent.duration.FiniteDuration; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - public class FollowerLogInformationImplTest { @Test public void testIsFollowerActive() { - FiniteDuration timeoutDuration = - new FiniteDuration(500, TimeUnit.MILLISECONDS); - - FollowerLogInformation followerLogInformation = - new FollowerLogInformationImpl( - "follower1", 10, 9, timeoutDuration); + MockRaftActorContext context = new MockRaftActorContext(); + context.setCommitIndex(10); + DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl(); + configParams.setHeartBeatInterval(new FiniteDuration(500, TimeUnit.MILLISECONDS)); + configParams.setElectionTimeoutFactor(1); + context.setConfigParams(configParams); + FollowerLogInformation followerLogInformation = + new FollowerLogInformationImpl(new PeerInfo("follower1", null, VotingState.VOTING), 9, context); assertFalse("Follower should be termed inactive before stopwatch starts", - followerLogInformation.isFollowerActive()); + followerLogInformation.isFollowerActive()); followerLogInformation.markFollowerActive(); if (sleepWithElaspsedTimeReturned(200) > 200) { - return; + return; } assertTrue("Follower should be active", followerLogInformation.isFollowerActive()); @@ -44,21 +46,88 @@ public class FollowerLogInformationImplTest { return; } assertFalse("Follower should be inactive after time lapsed", - followerLogInformation.isFollowerActive()); + followerLogInformation.isFollowerActive()); followerLogInformation.markFollowerActive(); assertTrue("Follower should be active from inactive", - followerLogInformation.isFollowerActive()); + followerLogInformation.isFollowerActive()); } // we cannot rely comfortably that the sleep will indeed sleep for the desired time // hence getting the actual elapsed time and do a match. // if the sleep has spilled over, then return the test gracefully - private long sleepWithElaspsedTimeReturned(long millis) { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.start(); + private static long sleepWithElaspsedTimeReturned(long millis) { + Stopwatch stopwatch = Stopwatch.createStarted(); Uninterruptibles.sleepUninterruptibly(millis, TimeUnit.MILLISECONDS); stopwatch.stop(); return stopwatch.elapsed(TimeUnit.MILLISECONDS); } + + @Test + public void testOkToReplicate() { + MockRaftActorContext context = new MockRaftActorContext(); + context.setCommitIndex(0); + FollowerLogInformation followerLogInformation = + new FollowerLogInformationImpl(new PeerInfo("follower1", null, VotingState.VOTING), 10, context); + + assertTrue(followerLogInformation.okToReplicate()); + assertFalse(followerLogInformation.okToReplicate()); + + // wait for 150 milliseconds and it should work again + Uninterruptibles.sleepUninterruptibly(150, TimeUnit.MILLISECONDS); + assertTrue(followerLogInformation.okToReplicate()); + + //increment next index and try immediately and it should work again + followerLogInformation.incrNextIndex(); + assertTrue(followerLogInformation.okToReplicate()); + } + + @Test + public void testVotingNotInitializedState() { + final PeerInfo peerInfo = new PeerInfo("follower1", null, VotingState.VOTING_NOT_INITIALIZED); + MockRaftActorContext context = new MockRaftActorContext(); + context.setCommitIndex(0); + FollowerLogInformation followerLogInformation = new FollowerLogInformationImpl(peerInfo, -1, context); + + assertFalse(followerLogInformation.okToReplicate()); + + followerLogInformation.markFollowerActive(); + assertFalse(followerLogInformation.isFollowerActive()); + + peerInfo.setVotingState(VotingState.VOTING); + assertTrue(followerLogInformation.okToReplicate()); + + followerLogInformation.markFollowerActive(); + assertTrue(followerLogInformation.isFollowerActive()); + } + + @Test + public void testNonVotingState() { + final PeerInfo peerInfo = new PeerInfo("follower1", null, VotingState.NON_VOTING); + MockRaftActorContext context = new MockRaftActorContext(); + context.setCommitIndex(0); + FollowerLogInformation followerLogInformation = new FollowerLogInformationImpl(peerInfo, -1, context); + + assertTrue(followerLogInformation.okToReplicate()); + + 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()); + } }