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=75496f97526d70eeef6fbdb1fd49b8127550b79a;hp=84d1545a65e3302462b69f9ee96f8dd982955654;hb=ecccb6d5b43dd73aef0d2d19349d19ee9b4728f7;hpb=3e5bfba47ae5fe04360343073273a141730daefd 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 84d1545a65..75496f9752 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,37 @@ */ package org.opendaylight.controller.cluster.raft; +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 org.opendaylight.controller.cluster.raft.FollowerLogInformation.FollowerState; 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("follower1", 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,20 +45,72 @@ 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) { + 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(); + FollowerLogInformation followerLogInformation = + new FollowerLogInformationImpl( + "follower1", 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() { + MockRaftActorContext context = new MockRaftActorContext(); + FollowerLogInformation followerLogInformation = new FollowerLogInformationImpl("follower1", -1, context); + + followerLogInformation.setFollowerState(FollowerState.VOTING_NOT_INITIALIZED); + assertFalse(followerLogInformation.okToReplicate()); + assertFalse(followerLogInformation.canParticipateInConsensus()); + + followerLogInformation.markFollowerActive(); + assertFalse(followerLogInformation.isFollowerActive()); + + followerLogInformation.setFollowerState(FollowerState.VOTING); + assertTrue(followerLogInformation.okToReplicate()); + assertTrue(followerLogInformation.canParticipateInConsensus()); + + followerLogInformation.markFollowerActive(); + assertTrue(followerLogInformation.isFollowerActive()); + } + + @Test + public void testNonVotingState() { + MockRaftActorContext context = new MockRaftActorContext(); + FollowerLogInformation followerLogInformation = new FollowerLogInformationImpl("follower1", -1, context); + + followerLogInformation.setFollowerState(FollowerState.NON_VOTING); + assertTrue(followerLogInformation.okToReplicate()); + assertFalse(followerLogInformation.canParticipateInConsensus()); + + followerLogInformation.markFollowerActive(); + assertTrue(followerLogInformation.isFollowerActive()); + } }