Search in sources :

Example 36 with FakeClock

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

the class ResourcePoolTest method shouldKeepSmallPeakAndNeverDisposeIfAcquireAndReleaseContinuously.

@Test
public void shouldKeepSmallPeakAndNeverDisposeIfAcquireAndReleaseContinuously() throws Exception {
    // given
    final int poolMinSize = 1;
    StatefulMonitor stateMonitor = new StatefulMonitor();
    FakeClock clock = getFakeClocks();
    final ResourcePool<Something> pool = getResourcePool(stateMonitor, clock, poolMinSize);
    // when
    for (int i = 0; i < 200; i++) {
        List<ResourceHolder> newOnes = acquireFromPool(pool, 1);
        CountDownLatch release = new CountDownLatch(newOnes.size());
        for (ResourceHolder newOne : newOnes) {
            newOne.release(release);
        }
        release.await();
    }
    // then
    // no alarm has rung, -1 is the default
    assertEquals(-1, stateMonitor.currentPeakSize.get());
    assertEquals(1, stateMonitor.created.get());
    // we should always be below min size, so 0 dispose calls
    assertEquals(0, stateMonitor.disposed.get());
}
Also used : FakeClock(org.neo4j.time.FakeClock) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 37 with FakeClock

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

the class ResourcePoolTest method shouldUpdateTargetSizeWhenSpikesOccur.

@Test
public void shouldUpdateTargetSizeWhenSpikesOccur() throws Exception {
    // given
    final int poolMinSize = 5;
    final int poolMaxSize = 10;
    StatefulMonitor stateMonitor = new StatefulMonitor();
    FakeClock clock = getFakeClocks();
    final ResourcePool<Something> pool = getResourcePool(stateMonitor, clock, poolMinSize);
    // when
    List<ResourceHolder> holders = acquireFromPool(pool, poolMaxSize);
    exceedTimeout(clock);
    // Needed to trigger the alarm
    holders.addAll(acquireFromPool(pool, 1));
    // then
    assertEquals(poolMaxSize + 1, stateMonitor.currentPeakSize.get());
    // We have not released anything, so targetSize will not be reduced
    // + 1 from the acquire
    assertEquals(poolMaxSize + 1, stateMonitor.targetSize.get());
    for (ResourceHolder holder : holders) {
        holder.end();
    }
}
Also used : FakeClock(org.neo4j.time.FakeClock) Test(org.junit.Test)

Example 38 with FakeClock

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

the class ResourcePoolTest method shouldMaintainPoolHigherThenMinSizeWhenPeekUsagePasses.

@Test
public void shouldMaintainPoolHigherThenMinSizeWhenPeekUsagePasses() throws Exception {
    // given
    final int poolMinSize = 50;
    final int poolMaxSize = 200;
    final int afterPeekPoolSize = 90;
    StatefulMonitor stateMonitor = new StatefulMonitor();
    FakeClock clock = getFakeClocks();
    final SomethingResourcePool pool = getResourcePool(stateMonitor, clock, poolMinSize);
    acquireResourcesAndExceedTimeout(pool, clock, poolMaxSize);
    // when
    // After the peak, stay at afterPeekPoolSize concurrent usage, using up all already present resources in the process
    // but also keeping the high watermark above the minimum size
    exceedTimeout(clock);
    // not always result in reaping of resources, as there is reuse
    for (int i = 0; i < 10; i++) {
        // The latch is necessary to reduce races between batches
        CountDownLatch release = new CountDownLatch(afterPeekPoolSize);
        for (ResourceHolder holder : acquireFromPool(pool, afterPeekPoolSize)) {
            holder.release(release);
        }
        release.await();
        exceedTimeout(clock);
    }
    // then
    // currentPeakSize should be at afterPeekPoolSize
    assertEquals(afterPeekPoolSize, stateMonitor.currentPeakSize.get());
    // target size too
    assertEquals(afterPeekPoolSize, stateMonitor.targetSize.get());
    // only the excess from the maximum size down to after peek usage size must have been disposed
    // +1 that was used to trigger exceed timeout check
    assertEquals(afterPeekPoolSize, pool.unusedSize());
    assertThat(stateMonitor.disposed.get(), greaterThanOrEqualTo(poolMaxSize - afterPeekPoolSize + 1));
}
Also used : FakeClock(org.neo4j.time.FakeClock) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 39 with FakeClock

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

the class IdleChannelReaperTest method shouldNotCloseAChannelThatHasBeenIdleForMoreThanHalfThresholdButIsStillOpenConnectedAndBound.

@Test
public void shouldNotCloseAChannelThatHasBeenIdleForMoreThanHalfThresholdButIsStillOpenConnectedAndBound() {
    // given
    FakeClock clock = Clocks.fakeClock();
    ChannelCloser channelCloser = mock(ChannelCloser.class);
    IdleChannelReaper idleChannelReaper = new IdleChannelReaper(channelCloser, NO_LOGGING, clock, THRESHOLD);
    Channel channel = mock(Channel.class);
    idleChannelReaper.add(channel, dummyRequestContext());
    when(channel.isOpen()).thenReturn(true);
    when(channel.isConnected()).thenReturn(true);
    when(channel.isBound()).thenReturn(true);
    // when
    clock.forward(THRESHOLD / 2 + 10, TimeUnit.MILLISECONDS);
    idleChannelReaper.run();
    // then
    verifyNoMoreInteractions(channelCloser);
}
Also used : FakeClock(org.neo4j.time.FakeClock) Channel(org.jboss.netty.channel.Channel) Test(org.junit.Test)

Example 40 with FakeClock

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

the class IdleChannelReaperTest method shouldNotCloseAnyRecentlyActiveChannels.

@Test
public void shouldNotCloseAnyRecentlyActiveChannels() {
    // given
    FakeClock clock = Clocks.fakeClock();
    ChannelCloser channelCloser = mock(ChannelCloser.class);
    IdleChannelReaper idleChannelReaper = new IdleChannelReaper(channelCloser, NO_LOGGING, clock, THRESHOLD);
    Channel channel = mock(Channel.class);
    idleChannelReaper.add(channel, dummyRequestContext());
    // when
    idleChannelReaper.run();
    // then
    verifyNoMoreInteractions(channelCloser);
}
Also used : FakeClock(org.neo4j.time.FakeClock) Channel(org.jboss.netty.channel.Channel) 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