Skip to content

Commit

Permalink
Add extra assertions to ResourcePoolTest
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegrohmann committed Oct 14, 2015
1 parent 8014933 commit 91ff5ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion enterprise/com/src/main/java/org/neo4j/com/ResourcePool.java
Expand Up @@ -104,7 +104,8 @@ public boolean shouldCheck()


public static final int DEFAULT_CHECK_INTERVAL = 60 * 1000; public static final int DEFAULT_CHECK_INTERVAL = 60 * 1000;


private final LinkedList<R> unused = new LinkedList<>(); // protected for testing
protected final LinkedList<R> unused = new LinkedList<>();
private final Map<Thread,R> current = new ConcurrentHashMap<>(); private final Map<Thread,R> current = new ConcurrentHashMap<>();
private final Monitor<R> monitor; private final Monitor<R> monitor;
private final int minSize; private final int minSize;
Expand Down
16 changes: 12 additions & 4 deletions enterprise/com/src/test/java/org/neo4j/com/ResourcePoolTest.java
Expand Up @@ -192,7 +192,7 @@ public void shouldSlowlyReduceTheNumberOfResourcesInThePoolWhenResourcesAreRelea


StatefulMonitor stateMonitor = new StatefulMonitor(); StatefulMonitor stateMonitor = new StatefulMonitor();
FakeClock clock = new FakeClock(); FakeClock clock = new FakeClock();
final ResourcePool<Something> pool = getResourcePool( stateMonitor, clock, poolMinSize ); final SomethingResourcePool pool = getResourcePool( stateMonitor, clock, poolMinSize );


acquireResourcesAndExceedTimeout( pool, clock, poolMaxSize ); acquireResourcesAndExceedTimeout( pool, clock, poolMaxSize );


Expand All @@ -211,6 +211,7 @@ public void shouldSlowlyReduceTheNumberOfResourcesInThePoolWhenResourcesAreRelea
assertEquals( poolMinSize, stateMonitor.targetSize.get() ); assertEquals( poolMinSize, stateMonitor.targetSize.get() );
// Only pooled resources must be used, disposing what is in excess // Only pooled resources must be used, disposing what is in excess
// +1 that was used to trigger exceed timeout check // +1 that was used to trigger exceed timeout check
assertEquals( poolMinSize, pool.unusedSize() );
assertEquals( poolMaxSize - poolMinSize + 1, stateMonitor.disposed.get() ); assertEquals( poolMaxSize - poolMinSize + 1, stateMonitor.disposed.get() );
} }


Expand All @@ -224,7 +225,7 @@ public void shouldMaintainPoolHigherThenMinSizeWhenPeekUsagePasses() throws Exce


StatefulMonitor stateMonitor = new StatefulMonitor(); StatefulMonitor stateMonitor = new StatefulMonitor();
FakeClock clock = new FakeClock(); FakeClock clock = new FakeClock();
final ResourcePool<Something> pool = getResourcePool( stateMonitor, clock, poolMinSize ); final SomethingResourcePool pool = getResourcePool( stateMonitor, clock, poolMinSize );


acquireResourcesAndExceedTimeout( pool, clock, poolMaxSize ); acquireResourcesAndExceedTimeout( pool, clock, poolMaxSize );


Expand Down Expand Up @@ -253,6 +254,7 @@ public void shouldMaintainPoolHigherThenMinSizeWhenPeekUsagePasses() throws Exce
assertEquals( afterPeekPoolSize, stateMonitor.targetSize.get() ); assertEquals( afterPeekPoolSize, stateMonitor.targetSize.get() );
// only the excess from the maximum size down to after peek usage size must have been disposed // 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 // +1 that was used to trigger exceed timeout check
assertEquals( afterPeekPoolSize, pool.unusedSize() );
assertEquals( poolMaxSize - afterPeekPoolSize + 1, stateMonitor.disposed.get() ); assertEquals( poolMaxSize - afterPeekPoolSize + 1, stateMonitor.disposed.get() );
} }


Expand All @@ -266,7 +268,7 @@ public void shouldReclaimAndRecreateWhenUsageGoesDownBetweenSpikes() throws Exce


StatefulMonitor stateMonitor = new StatefulMonitor(); StatefulMonitor stateMonitor = new StatefulMonitor();
FakeClock clock = new FakeClock(); FakeClock clock = new FakeClock();
final ResourcePool<Something> pool = getResourcePool( stateMonitor, clock, poolMinSize ); final SomethingResourcePool pool = getResourcePool( stateMonitor, clock, poolMinSize );


acquireResourcesAndExceedTimeout( pool, clock, poolMaxSize ); acquireResourcesAndExceedTimeout( pool, clock, poolMaxSize );


Expand All @@ -292,6 +294,7 @@ public void shouldReclaimAndRecreateWhenUsageGoesDownBetweenSpikes() throws Exce
assertEquals( bellowPoolMinSize, stateMonitor.currentPeakSize.get() ); assertEquals( bellowPoolMinSize, stateMonitor.currentPeakSize.get() );
// target size should remain at pool min size // target size should remain at pool min size
assertEquals( poolMinSize, stateMonitor.targetSize.get() ); assertEquals( poolMinSize, stateMonitor.targetSize.get() );
assertEquals( poolMinSize, pool.unusedSize() );
// only the excess from the pool max size down to min size must have been disposed // only the excess from the pool max size down to min size must have been disposed
// +1 that was used to trigger initial exceed timeout check // +1 that was used to trigger initial exceed timeout check
assertEquals( poolMaxSize - poolMinSize + 1, stateMonitor.disposed.get() ); assertEquals( poolMaxSize - poolMinSize + 1, stateMonitor.disposed.get() );
Expand Down Expand Up @@ -330,7 +333,7 @@ private void acquireResourcesAndExceedTimeout( ResourcePool<Something> pool,
} }
} }


private ResourcePool<Something> getResourcePool( StatefulMonitor stateMonitor, FakeClock clock, int minSize ) private SomethingResourcePool getResourcePool( StatefulMonitor stateMonitor, FakeClock clock, int minSize )
{ {
ResourcePool.CheckStrategy.TimeoutCheckStrategy timeoutCheckStrategy = ResourcePool.CheckStrategy.TimeoutCheckStrategy timeoutCheckStrategy =
new ResourcePool.CheckStrategy.TimeoutCheckStrategy( TIMEOUT_MILLIS, clock ); new ResourcePool.CheckStrategy.TimeoutCheckStrategy( TIMEOUT_MILLIS, clock );
Expand Down Expand Up @@ -370,6 +373,11 @@ protected boolean isAlive( Something resource )
{ {
return !resource.closed; return !resource.closed;
} }

public int unusedSize()
{
return unused.size();
}
} }


private class ResourceHolder implements Runnable private class ResourceHolder implements Runnable
Expand Down

0 comments on commit 91ff5ef

Please sign in to comment.