public void testTrimming() throws Exception
{
String name = "testTrimming";
getLogger().info( "Test: " + name );
BufferedLogger logger = new BufferedLogger();
PoolableTestObject.setStaticLoggger( logger );
PoolableTestObject.resetInstanceCounter();
PoolableTestObjectInterface[] poolables = new PoolableTestObjectInterface[ 4 ];
// Lookup and release all 4 components a couple of times.
for( int i = 0; i < 4; i++ )
{
poolables[ i ] =
(PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
}
for( int i = 0; i < 4; i++ )
{
release( (Component)poolables[ i ] );
poolables[ i ] = null;
}
for( int i = 0; i < 4; i++ )
{
poolables[ i ] =
(PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
}
for( int i = 0; i < 4; i++ )
{
release( (Component)poolables[ i ] );
poolables[ i ] = null;
}
// Now wait for 550 ms to trigger a trim on the next lookup.
try
{
Thread.sleep( 550 );
}
catch( InterruptedException e )
{
}
// Lookup and release 2 components to mark them as being recently used.
for( int i = 0; i < 2; i++ )
{
poolables[ i ] =
(PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
}
for( int i = 0; i < 2; i++ )
{
release( (Component)poolables[ i ] );
poolables[ i ] = null;
}
// Now wait for 550 ms to trigger a trim on the next lookup.
try
{
Thread.sleep( 550 );
}
catch( InterruptedException e )
{
}
// This next get should cause 2 of the components to be trimmed but the 2 we just lookedup
// should stay around.
// Lookup and release all 4 components to see which ones are left.
for( int i = 0; i < 4; i++ )
{
poolables[ i ] =
(PoolableTestObjectInterface)lookup( PoolableTestObjectInterface.ROLE + "/" + name );
}
for( int i = 0; i < 4; i++ )
{
release( (Component)poolables[ i ] );
poolables[ i ] = null;
}
// The disposal of the objects will not show up in the log until the component manager is
// actually disposed.
String resultLog = logger.toString();
String expectedLog =
"DEBUG - PoolableTestObject #1 initialized.\n" + // First 4 lookups
"DEBUG - PoolableTestObject #2 initialized.\n" +
"DEBUG - PoolableTestObject #3 initialized.\n" +
"DEBUG - PoolableTestObject #4 initialized.\n" +