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());
}
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();
}
}
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));
}
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);
}
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);
}
Aggregations