X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fbehaviors%2FCandidateTest.java;h=924ba57986739a1a56865b797a03007df8a7f872;hb=834f164ee449e9c5b2fd131e0196e58642e29a89;hp=8dd81dab8cf2c79d225bcff4dd0d77b649292150;hpb=9491b06df9419e58db3089a4c5cd9f5407cb9aac;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/CandidateTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/CandidateTest.java index 8dd81dab8c..924ba57986 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/CandidateTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/CandidateTest.java @@ -43,7 +43,7 @@ import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class CandidateTest extends AbstractRaftActorBehaviorTest { +public class CandidateTest extends AbstractRaftActorBehaviorTest { static final Logger LOG = LoggerFactory.getLogger(CandidateTest.class); private final TestActorRef candidateActor = actorFactory.createTestActor( @@ -93,7 +93,7 @@ public class CandidateTest extends AbstractRaftActorBehaviorTest { candidate = new Candidate(raftActorContext); RaftActorBehavior newBehavior = - candidate.handleMessage(candidateActor, new ElectionTimeout()); + candidate.handleMessage(candidateActor, ElectionTimeout.INSTANCE); assertEquals("Behavior", RaftState.Leader, newBehavior.state()); } @@ -104,7 +104,7 @@ public class CandidateTest extends AbstractRaftActorBehaviorTest { raftActorContext.setPeerAddresses(setupPeers(1)); candidate = new Candidate(raftActorContext); - candidate = candidate.handleMessage(candidateActor, new ElectionTimeout()); + candidate = candidate.handleMessage(candidateActor, ElectionTimeout.INSTANCE); assertEquals("Behavior", RaftState.Candidate, candidate.state()); } @@ -112,6 +112,7 @@ public class CandidateTest extends AbstractRaftActorBehaviorTest { @Test public void testBecomeLeaderOnReceivingMajorityVotesInThreeNodeCluster(){ MockRaftActorContext raftActorContext = createActorContext(); + raftActorContext.setLastApplied(raftActorContext.getReplicatedLog().lastIndex()); raftActorContext.setPeerAddresses(setupPeers(2)); candidate = new Candidate(raftActorContext); @@ -120,12 +121,26 @@ public class CandidateTest extends AbstractRaftActorBehaviorTest { assertEquals("Behavior", RaftState.Leader, candidate.state()); } + @Test + public void testBecomePreLeaderOnReceivingMajorityVotesInThreeNodeCluster(){ + MockRaftActorContext raftActorContext = createActorContext(); + raftActorContext.setPeerAddresses(setupPeers(2)); + candidate = new Candidate(raftActorContext); + + candidate = candidate.handleMessage(peerActors[0], new RequestVoteReply(1, true)); + + // LastApplied is -1 and behind the last index. + assertEquals("Behavior", RaftState.PreLeader, candidate.state()); + } + @Test public void testBecomeLeaderOnReceivingMajorityVotesInFiveNodeCluster(){ MockRaftActorContext raftActorContext = createActorContext(); raftActorContext.getTermInformation().update(2L, "other"); raftActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder(). createEntries(0, 5, 1).build()); + raftActorContext.setCommitIndex(raftActorContext.getReplicatedLog().lastIndex()); + raftActorContext.setLastApplied(raftActorContext.getReplicatedLog().lastIndex()); raftActorContext.setPeerAddresses(setupPeers(4)); candidate = new Candidate(raftActorContext); @@ -313,7 +328,7 @@ public class CandidateTest extends AbstractRaftActorBehaviorTest { context.getTermInformation().update(2, "test"); // Send an unknown message so that the state of the RaftActor remains unchanged - RaftActorBehavior expected = behavior.handleMessage(candidateActor, "unknown"); + behavior.handleMessage(candidateActor, "unknown"); RaftActorBehavior raftBehavior = behavior.handleMessage(candidateActor, appendEntries); @@ -325,7 +340,7 @@ public class CandidateTest extends AbstractRaftActorBehaviorTest { } @Override - protected RaftActorBehavior createBehavior(RaftActorContext actorContext) { + protected Candidate createBehavior(final RaftActorContext actorContext) { return new Candidate(actorContext); } @@ -333,7 +348,8 @@ public class CandidateTest extends AbstractRaftActorBehaviorTest { return new MockRaftActorContext("candidate", getSystem(), candidateActor); } - private Map setupPeers(int count) { + @SuppressWarnings("unchecked") + private Map setupPeers(final int count) { Map peerMap = new HashMap<>(); peerActors = new TestActorRef[count]; for(int i = 0; i < count; i++) { @@ -346,9 +362,13 @@ public class CandidateTest extends AbstractRaftActorBehaviorTest { } @Override - protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(RaftActorContext actorContext, - ActorRef actorRef, RaftRPC rpc) throws Exception { + protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(final MockRaftActorContext actorContext, + final ActorRef actorRef, final RaftRPC rpc) throws Exception { super.assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, actorRef, rpc); - assertEquals("New votedFor", null, actorContext.getTermInformation().getVotedFor()); + if(rpc instanceof RequestVote) { + assertEquals("New votedFor", ((RequestVote)rpc).getCandidateId(), actorContext.getTermInformation().getVotedFor()); + } else { + assertEquals("New votedFor", null, actorContext.getTermInformation().getVotedFor()); + } } }