* @throws Exception
*/
public void testSimpleShrinkMutipleWithEventHandler()
throws Exception
{
MemoryCacheMockImpl memory = new MemoryCacheMockImpl();
CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes();
cacheAttr.setMaxMemoryIdleTimeSeconds( 1 );
cacheAttr.setMaxSpoolPerRun( 3 );
memory.setCacheAttributes( cacheAttr );
ElementEventHandlerMockImpl handler = new ElementEventHandlerMockImpl();
for ( int i = 0; i < 10; i++ )
{
String key = "key" + i;
String value = "value";
ICacheElement element = new CacheElement( "testRegion", key, value );
ElementAttributes elementAttr = new ElementAttributes();
elementAttr.addElementEventHandler( handler );
elementAttr.setIsEternal( false );
element.setElementAttributes( elementAttr );
element.getElementAttributes().setMaxLifeSeconds( 1 );
memory.update( element );
ICacheElement returnedElement1 = memory.get( key );
assertNotNull( "We should have received an element", returnedElement1 );
// set this to 2 seconds ago.
elementAttr.lastAccessTime = System.currentTimeMillis() - 2000;
}
ShrinkerThread shrinker = new ShrinkerThread( memory );
Thread runner = new Thread( shrinker );
runner.run();
Thread.sleep( 500 );
assertEquals( "Waterfall called the wrong number of times.", 3, memory.waterfallCallCount );
// the shrinker delegates the the composite cache on the memory cache to put the
// event on the queue. This make it hard to test. TODO we need to change this to make it easier to verify.
//assertEquals( "Event handler ExceededIdleTimeBackground called the wrong number of times.", 3, handler.getExceededIdleTimeBackgroundCount() );
assertEquals( "Wrong number of elements remain.", 7, memory.getSize() );
}