Search in sources :

Example 41 with FakeClock

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

the class IdleChannelReaperTest method shouldNotTryToCloseAChannelThatWasRecentlyActive.

@Test
public void shouldNotTryToCloseAChannelThatWasRecentlyActive() {
    // given
    FakeClock clock = Clocks.fakeClock();
    ChannelCloser channelCloser = mock(ChannelCloser.class);
    IdleChannelReaper idleChannelReaper = new IdleChannelReaper(channelCloser, NO_LOGGING, clock, THRESHOLD);
    Channel channel = mock(Channel.class);
    RequestContext request = dummyRequestContext();
    idleChannelReaper.add(channel, request);
    // when
    clock.forward(THRESHOLD + 100, TimeUnit.MILLISECONDS);
    idleChannelReaper.update(channel);
    idleChannelReaper.run();
    // then
    verifyNoMoreInteractions(channelCloser);
}
Also used : FakeClock(org.neo4j.time.FakeClock) Channel(org.jboss.netty.channel.Channel) Test(org.junit.Test)

Example 42 with FakeClock

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

the class TransactionStateMachineSPITest method throwsWhenTxAwaitDurationExpires.

@Test
public void throwsWhenTxAwaitDurationExpires() throws Exception {
    long lastClosedTransactionId = 100;
    TransactionIdStore txIdStore = fixedTxIdStore(lastClosedTransactionId);
    Duration txAwaitDuration = Duration.ofSeconds(42);
    FakeClock clock = new FakeClock();
    AvailabilityGuard availabilityGuard = spy(new AvailabilityGuard(clock, NullLog.getInstance()));
    when(availabilityGuard.isAvailable()).then(invocation -> {
        boolean available = (boolean) invocation.callRealMethod();
        clock.forward(txAwaitDuration.getSeconds() + 1, SECONDS);
        return available;
    });
    TransactionStateMachineSPI txSpi = createTxSpi(txIdStore, txAwaitDuration, availabilityGuard, clock);
    Future<Void> result = otherThread.execute(state -> {
        txSpi.awaitUpToDate(lastClosedTransactionId + 42);
        return null;
    });
    try {
        result.get(20, SECONDS);
    } catch (Exception e) {
        assertThat(e, instanceOf(ExecutionException.class));
        assertThat(e.getCause(), instanceOf(TransactionFailureException.class));
    }
}
Also used : TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) FakeClock(org.neo4j.time.FakeClock) Duration(java.time.Duration) AvailabilityGuard(org.neo4j.kernel.AvailabilityGuard) ExecutionException(java.util.concurrent.ExecutionException) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) Test(org.junit.Test)

Example 43 with FakeClock

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

the class RaftMachineTest method shouldAppendNewLeaderBarrierAfterBecomingLeader.

@Test
public void shouldAppendNewLeaderBarrierAfterBecomingLeader() throws Exception {
    // Given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    OutboundMessageCollector messages = new OutboundMessageCollector();
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).outbound(messages).raftLog(raftLog).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    // When
    timeouts.invokeTimeout(ELECTION);
    raft.handle(voteResponse().from(member1).term(1).grant().build());
    // Then
    assertEquals(new NewLeaderBarrier(), readLogEntry(raftLog, raftLog.appendIndex()).content());
}
Also used : MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) 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 44 with FakeClock

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

the class RaftMachineTest method shouldThrowExceptionIfReceivesClientRequestWithNoLeaderElected.

@Test
public void shouldThrowExceptionIfReceivesClientRequestWithNoLeaderElected() 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();
    try {
        // When
        // There is no leader
        raft.getLeader();
        fail("Should have thrown exception");
    }// Then
     catch (NoLeaderFoundException e) {
    // expected
    }
}
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 45 with FakeClock

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

the class RaftMachineTest method shouldNotBecomeLeaderOnMultipleVotesFromSameMember.

@Test
public void shouldNotBecomeLeaderOnMultipleVotesFromSameMember() 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);
    // When
    raft.handle(voteResponse().from(member1).term(1).grant().build());
    raft.handle(voteResponse().from(member1).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)

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