Search in sources :

Example 76 with FakeClock

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

the class ElectionTest method candidateShouldWinElectionAndBecomeLeader.

@Test
public void candidateShouldWinElectionAndBecomeLeader() throws Exception {
    // 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).grant().build());
    raft.handle(voteResponse().from(member2).term(1).grant().build());
    // then
    assertEquals(1, raft.term());
    assertEquals(LEADER, raft.currentRole());
    verify(outbound).send(eq(member1), isA(RaftMessages.AppendEntries.Request.class));
    verify(outbound).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)

Example 77 with FakeClock

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

the class ElectionTest method candidateShouldVoteForTheSameCandidateInTheSameTerm.

@Test
public void candidateShouldVoteForTheSameCandidateInTheSameTerm() throws Exception {
    // 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))));
    // when
    raft.handle(voteRequest().from(member1).candidate(member1).term(1).build());
    raft.handle(voteRequest().from(member1).candidate(member1).term(1).build());
    // then
    verify(outbound, times(2)).send(member1, voteResponse().term(1).grant().build());
}
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) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Example 78 with FakeClock

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

the class ResourcePoolTest method shouldBuildUpGracefullyWhilePassingMinPoolSizeBeforeTimerRings.

@Test
public void shouldBuildUpGracefullyWhilePassingMinPoolSizeBeforeTimerRings() throws InterruptedException {
    // GIVEN
    StatefulMonitor stateMonitor = new StatefulMonitor();
    FakeClock clock = getFakeClocks();
    final ResourcePool<Something> pool = getResourcePool(stateMonitor, clock, 5);
    // WHEN
    acquireFromPool(pool, 15);
    // THEN
    assertEquals(-1, stateMonitor.currentPeakSize.get());
    assertEquals(15, stateMonitor.created.get());
    assertEquals(-1, stateMonitor.targetSize.get());
    assertEquals(0, stateMonitor.disposed.get());
}
Also used : FakeClock(org.neo4j.time.FakeClock) Test(org.junit.Test)

Example 79 with FakeClock

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

the class ResourcePoolTest method shouldSlowlyReduceTheNumberOfResourcesInThePoolWhenResourcesAreReleased.

@Test
public void shouldSlowlyReduceTheNumberOfResourcesInThePoolWhenResourcesAreReleased() throws Exception {
    // given
    final int poolMinSize = 50;
    final int poolMaxSize = 200;
    StatefulMonitor stateMonitor = new StatefulMonitor();
    FakeClock clock = getFakeClocks();
    final SomethingResourcePool pool = getResourcePool(stateMonitor, clock, poolMinSize);
    acquireResourcesAndExceedTimeout(pool, clock, poolMaxSize);
    // when
    // After the peak, stay below MIN_SIZE concurrent usage, using up all already present resources.
    exceedTimeout(clock);
    for (int i = 0; i < poolMaxSize; i++) {
        acquireFromPool(pool, 1).get(0).release();
    }
    // then
    // currentPeakSize must have reset from the latest check to minimum size.
    // because of timeout
    assertEquals(1, stateMonitor.currentPeakSize.get());
    // targetSize must be set to MIN_SIZE since currentPeakSize was that 2 checks ago and didn't increase
    assertEquals(poolMinSize, stateMonitor.targetSize.get());
    // Only pooled resources must be used, disposing what is in excess
    // +1 that was used to trigger exceed timeout check
    assertEquals(poolMinSize, pool.unusedSize());
    assertEquals(poolMaxSize - poolMinSize + 1, stateMonitor.disposed.get());
}
Also used : FakeClock(org.neo4j.time.FakeClock) Test(org.junit.Test)

Example 80 with FakeClock

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

the class ResourcePoolTest method shouldTimeoutGracefully.

@Test
public void shouldTimeoutGracefully() throws InterruptedException {
    FakeClock clock = getFakeClocks();
    ResourcePool.CheckStrategy timeStrategy = new ResourcePool.CheckStrategy.TimeoutCheckStrategy(TIMEOUT_MILLIS, clock);
    while (clock.millis() <= TIMEOUT_MILLIS) {
        assertFalse(timeStrategy.shouldCheck());
        clock.forward(10, TimeUnit.MILLISECONDS);
    }
    assertTrue(timeStrategy.shouldCheck());
    clock.forward(1, TimeUnit.MILLISECONDS);
    assertFalse(timeStrategy.shouldCheck());
}
Also used : FakeClock(org.neo4j.time.FakeClock) 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