Search in sources :

Example 71 with FakeClock

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

the class CatchupGoalTrackerTest method shouldFailToAchieveGoalDueToCatchupTimeoutExpiring.

@Test
public void shouldFailToAchieveGoalDueToCatchupTimeoutExpiring() throws Exception {
    FakeClock clock = Clocks.fakeClock();
    StubLog log = new StubLog();
    log.setAppendIndex(10);
    CatchupGoalTracker catchupGoalTracker = new CatchupGoalTracker(log, clock, ROUND_TIMEOUT, CATCHUP_TIMEOUT);
    // when
    clock.forward(CATCHUP_TIMEOUT + 10, TimeUnit.MILLISECONDS);
    catchupGoalTracker.updateProgress(new FollowerState().onSuccessResponse(4));
    // 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 72 with FakeClock

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

the class CatchupGoalTrackerTest method shouldFailToAchieveGoalDueToCatchupTimeoutExpiringEvenThoughWeDoEventuallyAchieveTarget.

@Test
public void shouldFailToAchieveGoalDueToCatchupTimeoutExpiringEvenThoughWeDoEventuallyAchieveTarget() throws Exception {
    FakeClock clock = Clocks.fakeClock();
    StubLog log = new StubLog();
    log.setAppendIndex(10);
    CatchupGoalTracker catchupGoalTracker = new CatchupGoalTracker(log, clock, ROUND_TIMEOUT, CATCHUP_TIMEOUT);
    // when
    clock.forward(CATCHUP_TIMEOUT + 10, TimeUnit.MILLISECONDS);
    catchupGoalTracker.updateProgress(new FollowerState().onSuccessResponse(10));
    // 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 73 with FakeClock

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

the class MonitoredBoltWorkerFactoryTest method shouldSignalReceivedStartAndComplete.

@Test
public void shouldSignalReceivedStartAndComplete() throws Throwable {
    // given
    FakeClock clock = Clocks.fakeClock();
    WorkerFactory delegate = mock(WorkerFactory.class);
    BoltStateMachine machine = mock(BoltStateMachine.class);
    when(delegate.newWorker(anyObject(), anyObject())).thenReturn(new BoltWorker() {

        @Override
        public void enqueue(Job job) {
            clock.forward(1337, TimeUnit.MILLISECONDS);
            try {
                job.perform(machine);
            } catch (BoltConnectionFatality connectionFatality) {
                throw new RuntimeException(connectionFatality);
            }
        }

        @Override
        public void interrupt() {
            throw new RuntimeException();
        }

        @Override
        public void halt() {
            throw new RuntimeException();
        }
    });
    Monitors monitors = new Monitors();
    CountingSessionMonitor monitor = new CountingSessionMonitor();
    monitors.addMonitorListener(monitor);
    MonitoredWorkerFactory workerFactory = new MonitoredWorkerFactory(monitors, delegate, clock);
    BoltWorker worker = workerFactory.newWorker(CONNECTION_DESCRIPTOR);
    // when
    worker.enqueue((stateMachine) -> {
        stateMachine.run("hello", null, nullResponseHandler());
        clock.forward(1338, TimeUnit.MILLISECONDS);
    });
    // then
    assertEquals(1, monitor.messagesReceived);
    assertEquals(1337, monitor.queueTime);
    assertEquals(1338, monitor.processingTime);
}
Also used : FakeClock(org.neo4j.time.FakeClock) MonitoredBoltWorker(org.neo4j.bolt.v1.runtime.MonitoredWorkerFactory.MonitoredBoltWorker) Monitors(org.neo4j.kernel.monitoring.Monitors) Test(org.junit.Test)

Example 74 with FakeClock

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

the class DelayedRenewableTimeoutServiceTest method shouldNotTimeOutWhenStopped.

@Test
public void shouldNotTimeOutWhenStopped() throws Throwable {
    // given
    final AtomicLong timeoutCount = new AtomicLong();
    FakeClock clock = Clocks.fakeClock();
    DelayedRenewableTimeoutService timeoutService = new DelayedRenewableTimeoutService(clock, NullLogProvider.getInstance());
    RenewableTimeoutService.RenewableTimeout timeout = timeoutService.create(Timeouts.FOOBAR, TIMEOUT_MS, 0, t -> timeoutCount.incrementAndGet());
    life.add(timeoutService);
    clock.forward(TIMEOUT_MS, MILLISECONDS);
    Predicates.await(timeoutCount::get, count -> count == 1, LONG_TIME_MS, MILLISECONDS, 1, MILLISECONDS);
    // when
    timeoutService.stop();
    timeoutService.shutdown();
    timeout.renew();
    Thread.sleep(TIMEOUT_MS / 2);
    clock.forward(TIMEOUT_MS, MILLISECONDS);
    Thread.sleep(TIMEOUT_MS / 2);
    // then
    assertThat(timeoutCount.get(), equalTo(1L));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) FakeClock(org.neo4j.time.FakeClock) Test(org.junit.Test)

Example 75 with FakeClock

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

the class ElectionTest method candidateShouldLoseElectionAndRemainCandidate.

@Test
public void candidateShouldLoseElectionAndRemainCandidate() throws Exception {
    // Note the etcd implementation seems to diverge from the paper here, since the paper suggests that it should
    // remain as a candidate
    // 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).deny().build());
    raft.handle(voteResponse().from(member2).term(1).deny().build());
    // then
    assertEquals(1, raft.term());
    assertEquals(CANDIDATE, raft.currentRole());
    verify(outbound, never()).send(eq(member1), isA(RaftMessages.AppendEntries.Request.class));
    verify(outbound, never()).send(eq(member2), isA(RaftMessages.AppendEntries.Request.class));
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) RaftMachineBuilder(org.neo4j.causalclustering.core.consensus.RaftMachineBuilder) MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) FakeClock(org.neo4j.time.FakeClock) TestMessageBuilders.voteRequest(org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Aggregations

FakeClock (org.neo4j.time.FakeClock)111 Test (org.junit.Test)82 Test (org.junit.jupiter.api.Test)21 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)20 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 ExecutingQuery (org.neo4j.kernel.api.query.ExecutingQuery)9 QueryLogger (org.neo4j.kernel.impl.query.QueryLoggerKernelExtension.QueryLogger)9 User (org.neo4j.kernel.impl.security.User)9 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)7 Channel (org.jboss.netty.channel.Channel)5 Path (java.nio.file.Path)4 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 ExecutionException (java.util.concurrent.ExecutionException)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)3