use of org.opendaylight.controller.cluster.raft.MockRaftActorContext in project controller by opendaylight.
the class CandidateTest method testCandidateSchedulesElectionTimeoutImmediatelyWhenItHasNoPeers.
@Test
public void testCandidateSchedulesElectionTimeoutImmediatelyWhenItHasNoPeers() {
MockRaftActorContext context = createActorContext();
Stopwatch stopwatch = Stopwatch.createStarted();
candidate = createBehavior(context);
MessageCollectorActor.expectFirstMatching(candidateActor, ElectionTimeout.class);
long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
assertTrue(elapsed < context.getConfigParams().getElectionTimeOutInterval().toMillis());
}
use of org.opendaylight.controller.cluster.raft.MockRaftActorContext in project controller by opendaylight.
the class CandidateTest method testBecomeLeaderOnReceivingMajorityVotesInThreeNodeCluster.
@Test
public void testBecomeLeaderOnReceivingMajorityVotesInThreeNodeCluster() {
MockRaftActorContext raftActorContext = createActorContext();
raftActorContext.setLastApplied(raftActorContext.getReplicatedLog().lastIndex());
raftActorContext.setPeerAddresses(setupPeers(2));
candidate = new Candidate(raftActorContext);
candidate = candidate.handleMessage(peerActors[0], new RequestVoteReply(1, true));
assertEquals("Behavior", RaftState.Leader, candidate.state());
}
use of org.opendaylight.controller.cluster.raft.MockRaftActorContext in project controller by opendaylight.
the class FollowerTest method testHandleAppendEntriesWithExistingLogEntry.
@Test
public void testHandleAppendEntriesWithExistingLogEntry() {
logStart("testHandleAppendEntriesWithExistingLogEntry");
MockRaftActorContext context = createActorContext();
context.getTermInformation().update(1, "test");
// Prepare the receivers log
MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog();
log.append(newReplicatedLogEntry(1, 0, "zero"));
log.append(newReplicatedLogEntry(1, 1, "one"));
context.setReplicatedLog(log);
// Send the last entry again.
List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(1, 1, "one"));
follower = createBehavior(context);
follower.handleMessage(leaderActor, new AppendEntries(1, "leader", 0, 1, entries, 1, -1, (short) 0));
assertEquals("Next index", 2, log.last().getIndex() + 1);
assertEquals("Entry 1", entries.get(0), log.get(1));
expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 1);
// Send the last entry again and also a new one.
entries = Arrays.asList(newReplicatedLogEntry(1, 1, "one"), newReplicatedLogEntry(1, 2, "two"));
MessageCollectorActor.clearMessages(leaderActor);
follower.handleMessage(leaderActor, new AppendEntries(1, "leader", 0, 1, entries, 2, -1, (short) 0));
assertEquals("Next index", 3, log.last().getIndex() + 1);
assertEquals("Entry 1", entries.get(0), log.get(1));
assertEquals("Entry 2", entries.get(1), log.get(2));
expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 2);
}
use of org.opendaylight.controller.cluster.raft.MockRaftActorContext in project controller by opendaylight.
the class FollowerTest method testHandleFirstAppendEntriesWithPrevIndexMinusOne.
@Test
public void testHandleFirstAppendEntriesWithPrevIndexMinusOne() {
logStart("testHandleFirstAppendEntries");
MockRaftActorContext context = createActorContext();
List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(2, 101, "foo"));
// The new commitIndex is 101
AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 101, 100, (short) 0);
follower = createBehavior(context);
follower.handleMessage(leaderActor, appendEntries);
FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
assertFalse(syncStatus.isInitialSyncDone());
assertFalse("append entries reply should be false", reply.isSuccess());
}
use of org.opendaylight.controller.cluster.raft.MockRaftActorContext in project controller by opendaylight.
the class FollowerTest method testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNull.
@Test
public void testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNull() {
logStart("testHandleRequestVoteWhenSenderTermEqualToCurrentTermAndVotedForIsNull");
MockRaftActorContext context = createActorContext();
long term = 1000;
context.getTermInformation().update(term, null);
follower = createBehavior(context);
follower.handleMessage(leaderActor, new RequestVote(term, "test", 10000, 999));
RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, RequestVoteReply.class);
assertEquals("isVoteGranted", true, reply.isVoteGranted());
assertEquals("getTerm", term, reply.getTerm());
verify(follower).scheduleElection(any(FiniteDuration.class));
}
Aggregations