use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.
the class AppendEntriesRequestTest method shouldCommitEntry.
@Test
public void shouldCommitEntry() throws Exception {
// given
InMemoryRaftLog raftLog = new InMemoryRaftLog();
RaftState state = raftState().entryLog(raftLog).myself(myself).build();
long leaderTerm = state.term() + leaderTermDifference;
raftLog.append(new RaftLogEntry(leaderTerm, content()));
// when
Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(raftLog.appendIndex()).prevLogTerm(leaderTerm).leaderCommit(0).build(), state, log());
// then
assertTrue(((Response) messageFor(outcome, leader)).success());
assertThat(outcome.getCommitIndex(), Matchers.equalTo(0L));
}
use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.
the class VoteRequestTest method shouldVoteForCandidateInLaterTerm.
@Test
public void shouldVoteForCandidateInLaterTerm() throws Exception {
// given
RaftState state = newState();
// when
final long candidateTerm = state.term() + 1;
Outcome outcome = role.handler.handle(voteRequest().from(member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, log());
// then
assertTrue(((RaftMessages.Vote.Response) messageFor(outcome, member1)).voteGranted());
}
use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.
the class VoteRequestTest method shouldDenyForCandidateInPreviousTerm.
@Test
public void shouldDenyForCandidateInPreviousTerm() throws Exception {
// given
RaftState state = newState();
// when
final long candidateTerm = state.term() - 1;
Outcome outcome = role.handler.handle(voteRequest().from(member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, log());
// then
assertFalse(((RaftMessages.Vote.Response) messageFor(outcome, member1)).voteGranted());
assertEquals(role, outcome.getRole());
}
use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.
the class VoteRequestTest method shouldVoteForOnlyOneCandidatePerTerm.
@Test
public void shouldVoteForOnlyOneCandidatePerTerm() throws Exception {
// given
RaftState state = newState();
// when
final long candidateTerm = state.term() + 1;
Outcome outcome1 = role.handler.handle(voteRequest().from(member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, log());
state.update(outcome1);
Outcome outcome2 = role.handler.handle(voteRequest().from(member2).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, log());
// then
assertFalse(((RaftMessages.Vote.Response) messageFor(outcome2, member2)).voteGranted());
}
use of org.neo4j.causalclustering.core.consensus.state.RaftState in project neo4j by neo4j.
the class HeartbeatTest method shouldNotResultInCommitIfReferringToFutureEntries.
@Test
public void shouldNotResultInCommitIfReferringToFutureEntries() throws Exception {
InMemoryRaftLog raftLog = new InMemoryRaftLog();
RaftState state = raftState().myself(myself).entryLog(raftLog).build();
long leaderTerm = state.term() + leaderTermDifference;
raftLog.append(new RaftLogEntry(leaderTerm, content()));
RaftMessages.Heartbeat heartbeat = heartbeat().from(leader).commitIndex(// The leader is talking about committing stuff we don't know about
raftLog.appendIndex() + 1).commitIndexTerm(// And is in the same term
leaderTerm).leaderTerm(leaderTerm).build();
Outcome outcome = role.handler.handle(heartbeat, state, log());
assertThat(outcome.getLogCommands(), empty());
}
Aggregations