use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class RaftMachineTest method shouldBecomeLeaderInMajorityOf3.
@Test
public void shouldBecomeLeaderInMajorityOf3() 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);
assertThat(raft.isLeader(), is(false));
// When
raft.handle(voteResponse().from(member1).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 shouldPersistAtSpecifiedLogIndex.
@Test
public void shouldPersistAtSpecifiedLogIndex() throws Exception {
// given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).raftLog(raftLog).build();
raftLog.append(new RaftLogEntry(0, new MemberIdSet(asSet(myself, member1, member2))));
// when
raft.handle(appendEntriesRequest().from(member1).prevLogIndex(0).prevLogTerm(0).leaderTerm(0).logEntry(new RaftLogEntry(0, data1)).build());
// then
assertEquals(1, raftLog.appendIndex());
assertEquals(data1, readLogEntry(raftLog, 1).content());
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class RaftMachineTest method newMembersShouldBeIncludedInHeartbeatMessages.
@Test
public void newMembersShouldBeIncludedInHeartbeatMessages() throws Exception {
// Given
DirectNetworking network = new DirectNetworking();
final MemberId newMember = member(99);
DirectNetworking.Inbound newMemberInbound = network.new Inbound(newMember);
final OutboundMessageCollector messages = new OutboundMessageCollector();
newMemberInbound.registerHandler(new Inbound.MessageHandler<RaftMessages.RaftMessage>() {
@Override
public void handle(RaftMessages.RaftMessage message) {
messages.send(newMember, message);
}
});
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
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();
// We make ourselves the leader
timeouts.invokeTimeout(ELECTION);
raft.handle(voteResponse().from(member1).term(1).grant().build());
// When
raft.setTargetMembershipSet(asSet(myself, member1, member2, newMember));
network.processMessages();
timeouts.invokeTimeout(RaftMachine.Timeouts.HEARTBEAT);
network.processMessages();
// Then
assertEquals(RaftMessages.AppendEntries.Request.class, messages.sentTo(newMember).get(0).getClass());
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class CatchupGoalTrackerTest method shouldNotFinishIfRoundsNotExhausted.
@Test
public void shouldNotFinishIfRoundsNotExhausted() throws Exception {
FakeClock clock = Clocks.fakeClock();
StubLog log = new StubLog();
long appendIndex = 10;
log.setAppendIndex(appendIndex);
CatchupGoalTracker catchupGoalTracker = new CatchupGoalTracker(log, clock, ROUND_TIMEOUT, CATCHUP_TIMEOUT);
for (int i = 0; i < CatchupGoalTracker.MAX_ROUNDS - 5; i++) {
appendIndex += 10;
log.setAppendIndex(appendIndex);
clock.forward(ROUND_TIMEOUT + 1, TimeUnit.MILLISECONDS);
catchupGoalTracker.updateProgress(new FollowerState().onSuccessResponse(appendIndex));
}
// then
assertFalse(catchupGoalTracker.isGoalAchieved());
assertFalse(catchupGoalTracker.isFinished());
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class CatchupGoalTrackerTest method shouldAchieveGoalIfWithinRoundTimeout.
@Test
public void shouldAchieveGoalIfWithinRoundTimeout() throws Exception {
FakeClock clock = Clocks.fakeClock();
StubLog log = new StubLog();
log.setAppendIndex(10);
CatchupGoalTracker catchupGoalTracker = new CatchupGoalTracker(log, clock, ROUND_TIMEOUT, CATCHUP_TIMEOUT);
clock.forward(ROUND_TIMEOUT - 5, TimeUnit.MILLISECONDS);
catchupGoalTracker.updateProgress(new FollowerState().onSuccessResponse(10));
assertTrue(catchupGoalTracker.isGoalAchieved());
assertTrue(catchupGoalTracker.isFinished());
}
Aggregations