use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class RaftMachineTest method shouldNotBecomeLeaderByVotesFromOldTerm.
@Test
public void shouldNotBecomeLeaderByVotesFromOldTerm() throws Exception {
// Given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
raft.startTimers();
timeouts.invokeTimeout(ELECTION);
// When
raft.handle(voteResponse().from(member1).term(0).grant().build());
raft.handle(voteResponse().from(member2).term(0).grant().build());
// Then
assertThat(raft.isLeader(), is(false));
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class RaftMachineTest method shouldNotBecomeLeaderWhenMembersVoteNo.
@Test
public void shouldNotBecomeLeaderWhenMembersVoteNo() throws Exception {
// Given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
raft.startTimers();
timeouts.invokeTimeout(ELECTION);
// When
raft.handle(voteResponse().from(member1).term(1).deny().build());
raft.handle(voteResponse().from(member2).term(1).deny().build());
// Then
assertThat(raft.isLeader(), is(false));
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class RaftMachineTest method shouldBecomeLeaderInMajorityOf5.
@Test
public void shouldBecomeLeaderInMajorityOf5() throws Exception {
// Given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2, member3, member4))));
raft.startTimers();
timeouts.invokeTimeout(ELECTION);
raft.handle(voteResponse().from(member1).term(1).grant().build());
assertThat(raft.isLeader(), is(false));
// When
raft.handle(voteResponse().from(member2).term(1).grant().build());
// Then
assertThat(raft.isLeader(), is(true));
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class RaftMachineTest method leaderShouldSendHeartBeatsOnHeartbeatTimeout.
@Test
public void leaderShouldSendHeartBeatsOnHeartbeatTimeout() throws Exception {
// Given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
OutboundMessageCollector messages = new OutboundMessageCollector();
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).outbound(messages).clock(fakeClock).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
raft.startTimers();
timeouts.invokeTimeout(ELECTION);
raft.handle(voteResponse().from(member1).term(1).grant().build());
// When
timeouts.invokeTimeout(RaftMachine.Timeouts.HEARTBEAT);
// Then
assertTrue(last(messages.sentTo(member1)) instanceof RaftMessages.Heartbeat);
assertTrue(last(messages.sentTo(member2)) instanceof RaftMessages.Heartbeat);
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class RaftMachineTest method shouldRequestVotesOnElectionTimeout.
@Test
public void shouldRequestVotesOnElectionTimeout() throws Exception {
// Given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
OutboundMessageCollector messages = new OutboundMessageCollector();
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).outbound(messages).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
raft.startTimers();
// When
timeouts.invokeTimeout(ELECTION);
// Then
assertThat(messages.sentTo(myself).size(), equalTo(0));
assertThat(messages.sentTo(member1).size(), equalTo(1));
assertThat(messages.sentTo(member1).get(0), instanceOf(RaftMessages.Vote.Request.class));
assertThat(messages.sentTo(member2).size(), equalTo(1));
assertThat(messages.sentTo(member2).get(0), instanceOf(RaftMessages.Vote.Request.class));
}
Aggregations