use of org.neo4j.causalclustering.core.consensus.RaftMachine in project neo4j by neo4j.
the class ElectionTest method candidateShouldWinElectionAndBecomeLeader.
@Test
public void candidateShouldWinElectionAndBecomeLeader() throws Exception {
// given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).outbound(outbound).timeoutService(timeouts).clock(fakeClock).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
raft.startTimers();
timeouts.invokeTimeout(RaftMachine.Timeouts.ELECTION);
// when
raft.handle(voteResponse().from(member1).term(1).grant().build());
raft.handle(voteResponse().from(member2).term(1).grant().build());
// then
assertEquals(1, raft.term());
assertEquals(LEADER, raft.currentRole());
verify(outbound).send(eq(member1), isA(RaftMessages.AppendEntries.Request.class));
verify(outbound).send(eq(member2), isA(RaftMessages.AppendEntries.Request.class));
}
use of org.neo4j.causalclustering.core.consensus.RaftMachine in project neo4j by neo4j.
the class ElectionTest method candidateShouldVoteForTheSameCandidateInTheSameTerm.
@Test
public void candidateShouldVoteForTheSameCandidateInTheSameTerm() throws Exception {
// given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).outbound(outbound).timeoutService(timeouts).clock(fakeClock).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
// when
raft.handle(voteRequest().from(member1).candidate(member1).term(1).build());
raft.handle(voteRequest().from(member1).candidate(member1).term(1).build());
// then
verify(outbound, times(2)).send(member1, voteResponse().term(1).grant().build());
}
Aggregations