Search in sources :

Example 56 with FakeClock

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

the class TransportWriteThrottleTest method shouldThrowThrottleExceptionWhenMaxDurationIsReached.

@Test
void shouldThrowThrottleExceptionWhenMaxDurationIsReached() throws Exception {
    // given
    FakeClock clock = Clocks.fakeClock(1, TimeUnit.SECONDS);
    TransportThrottle throttle = newThrottleAndInstall(channel, new DefaultThrottleLock(), clock, Duration.ofSeconds(5));
    when(channel.isWritable()).thenReturn(false);
    // when
    Future<Void> future = otherThread.execute(() -> {
        throttle.acquire(channel);
        return null;
    });
    otherThread.get().waitUntilWaiting();
    clock.forward(6, TimeUnit.SECONDS);
    // expect
    assertThatExceptionOfType(ExecutionException.class).isThrownBy(() -> future.get(1, TimeUnit.MINUTES)).withCauseInstanceOf(TransportThrottleException.class).withMessageContaining("will be closed because the client did not consume outgoing buffers for");
}
Also used : FakeClock(org.neo4j.time.FakeClock) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 57 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 58 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 59 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)

Example 60 with FakeClock

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

the class RaftMachineTest method shouldNotBecomeLeaderByVotesFromFutureTerm.

@Test
public void shouldNotBecomeLeaderByVotesFromFutureTerm() 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(2).grant().build());
    raft.handle(voteResponse().from(member2).term(2).grant().build());
    assertThat(raft.isLeader(), is(false));
    assertEquals(raft.term(), 2L);
}
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)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