Search in sources :

Example 16 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class CatchupGoalTrackerTest method shouldFailToAchieveGoalDueToRoundExhaustion.

@Test
public void shouldFailToAchieveGoalDueToRoundExhaustion() 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; i++) {
        appendIndex += 10;
        log.setAppendIndex(appendIndex);
        clock.forward(ROUND_TIMEOUT + 1, TimeUnit.MILLISECONDS);
        catchupGoalTracker.updateProgress(new FollowerState().onSuccessResponse(appendIndex));
    }
    // then
    assertFalse(catchupGoalTracker.isGoalAchieved());
    assertTrue(catchupGoalTracker.isFinished());
}
Also used : FakeClock(org.neo4j.time.FakeClock) FollowerState(org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState) Test(org.junit.Test)

Example 17 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class RaftMachineTest method shouldVoteFalseForCandidateInOldTerm.

@Test
public void shouldVoteFalseForCandidateInOldTerm() 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
    raft.handle(voteRequest().from(member1).term(-1).candidate(member1).lastLogIndex(0).lastLogTerm(-1).build());
    // Then
    assertThat(messages.sentTo(member1).size(), equalTo(1));
    assertThat(messages.sentTo(member1), hasItem(voteResponse().from(myself).term(0).deny().build()));
}
Also used : MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) FakeClock(org.neo4j.time.FakeClock) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Example 18 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class RaftMachineTest method shouldNotBecomeLeaderWhenVotingOnItself.

@Test
public void shouldNotBecomeLeaderWhenVotingOnItself() 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(myself).term(1).grant().build());
    // Then
    assertThat(raft.isLeader(), is(false));
}
Also used : MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) FakeClock(org.neo4j.time.FakeClock) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Example 19 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class CappedLoggerTest method unsettingTimeLimitMustLetMessagesThrough.

@Test
public void unsettingTimeLimitMustLetMessagesThrough() throws Exception {
    FakeClock clock = getDefaultFakeClock();
    logger.setTimeLimit(1, TimeUnit.MILLISECONDS, clock);
    logMethod.log(logger, "### AAA ###");
    logMethod.log(logger, "### BBB ###");
    clock.forward(1, TimeUnit.MILLISECONDS);
    logMethod.log(logger, "### CCC ###");
    logMethod.log(logger, "### DDD ###");
    // Note that we are not advancing the clock!
    logger.unsetTimeLimit();
    logMethod.log(logger, "### EEE ###");
    logProvider.assertContainsMessageMatching(containsString("### AAA ###"));
    logProvider.assertNone(currentLog(inLog(CappedLogger.class), containsString("### BBB ###")));
    logProvider.assertContainsMessageMatching(containsString("### CCC ###"));
    logProvider.assertNone(currentLog(inLog(CappedLogger.class), containsString("### DDD ###")));
    logProvider.assertContainsMessageMatching(containsString("### EEE ###"));
}
Also used : FakeClock(org.neo4j.time.FakeClock) Test(org.junit.Test)

Example 20 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class CappedLoggerTest method mustNotLogMessagesWithinConfiguredTimeLimit.

@Test
public void mustNotLogMessagesWithinConfiguredTimeLimit() throws Exception {
    FakeClock clock = getDefaultFakeClock();
    logger.setTimeLimit(1, TimeUnit.MILLISECONDS, clock);
    logMethod.log(logger, "### AAA ###");
    logMethod.log(logger, "### BBB ###");
    clock.forward(1, TimeUnit.MILLISECONDS);
    logMethod.log(logger, "### CCC ###");
    logProvider.assertContainsMessageMatching(containsString("### AAA ###"));
    logProvider.assertNone(currentLog(inLog(CappedLogger.class), containsString("### BBB ###")));
    logProvider.assertContainsMessageMatching(containsString("### CCC ###"));
}
Also used : FakeClock(org.neo4j.time.FakeClock) Test(org.junit.Test)

Aggregations

FakeClock (org.neo4j.time.FakeClock)87 Test (org.junit.Test)85 ControlledRenewableTimeoutService (org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService)17 MembershipEntry (org.neo4j.causalclustering.core.consensus.membership.MembershipEntry)16 RaftCoreState (org.neo4j.causalclustering.core.state.snapshot.RaftCoreState)16 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)14 ExecutingQuery (org.neo4j.kernel.api.query.ExecutingQuery)9 QueryLogger (org.neo4j.kernel.impl.query.QueryLoggerKernelExtension.QueryLogger)9 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)7 Channel (org.jboss.netty.channel.Channel)5 User (org.neo4j.kernel.impl.security.User)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Response (javax.ws.rs.core.Response)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 InputStream (java.io.InputStream)3 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)3 RaftMachineBuilder (org.neo4j.causalclustering.core.consensus.RaftMachineBuilder)3 TestMessageBuilders.voteRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest)3 File (java.io.File)2 Clock (java.time.Clock)2