You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tv...@apache.org on 2014/03/25 22:03:57 UTC
svn commit: r1581516 - in /commons/proper/jcs/trunk/src: changes/
java/org/apache/commons/jcs/engine/control/
java/org/apache/commons/jcs/engine/memory/
java/org/apache/commons/jcs/engine/memory/lru/ test-conf/
test/org/apache/commons/jcs/engine/contro...
Author: tv
Date: Tue Mar 25 21:03:57 2014
New Revision: 1581516
URL: http://svn.apache.org/r1581516
Log:
Fix updating of last access time, add tests for event handling
Modified:
commons/proper/jcs/trunk/src/changes/changes.xml
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCache.java
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/memory/lru/LHMLRUMemoryCache.java
commons/proper/jcs/trunk/src/test-conf/TestSimpleEventHandling.ccf
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/event/SimpleEventHandlingUnitTest.java
Modified: commons/proper/jcs/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/changes/changes.xml?rev=1581516&r1=1581515&r2=1581516&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/changes/changes.xml (original)
+++ commons/proper/jcs/trunk/src/changes/changes.xml Tue Mar 25 21:03:57 2014
@@ -20,6 +20,9 @@
</properties>
<body>
<release version="2.0" date="unreleased" description="JDK 1.6 based major release">
+ <action dev="tv" type="fix">
+ Fix updating of last access time, add tests for event handling
+ </action>
<action dev="tv" type="update">
Update documentation to reflect generics and API changes
</action>
Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCache.java?rev=1581516&r1=1581515&r2=1581516&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCache.java (original)
+++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCache.java Tue Mar 25 21:03:57 2014
@@ -231,6 +231,8 @@ public class CompositeCache<K extends Se
memCache.update( cacheElement );
updateAuxiliaries( cacheElement, localOnly );
+
+ cacheElement.getElementAttributes().setLastAccessTimeNow();
}
}
@@ -606,6 +608,11 @@ public class CompositeCache<K extends Se
}
}
+ if (element != null)
+ {
+ element.getElementAttributes().setLastAccessTimeNow();
+ }
+
return element;
}
Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java?rev=1581516&r1=1581515&r2=1581516&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java (original)
+++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java Tue Mar 25 21:03:57 2014
@@ -108,7 +108,6 @@ public abstract class AbstractDoubleLink
throws IOException
{
putCnt++;
- ce.getElementAttributes().setLastAccessTimeNow();
synchronized ( this )
{
@@ -207,7 +206,6 @@ public abstract class AbstractDoubleLink
{
ce = me.ce;
hitCnt++;
- ce.getElementAttributes().setLastAccessTimeNow();
if ( log.isDebugEnabled() )
{
log.debug( cacheName + ": LRUMemoryCache hit for " + ce.getKey() );
Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/memory/lru/LHMLRUMemoryCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/memory/lru/LHMLRUMemoryCache.java?rev=1581516&r1=1581515&r2=1581516&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/memory/lru/LHMLRUMemoryCache.java (original)
+++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/memory/lru/LHMLRUMemoryCache.java Tue Mar 25 21:03:57 2014
@@ -96,7 +96,6 @@ public class LHMLRUMemoryCache<K extends
throws IOException
{
putCnt++;
- ce.getElementAttributes().setLastAccessTimeNow();
map.put( ce.getKey(), new MemoryElementDescriptor<K, V>(ce) );
}
@@ -138,7 +137,6 @@ public class LHMLRUMemoryCache<K extends
if ( me != null )
{
hitCnt++;
- me.ce.getElementAttributes().setLastAccessTimeNow();
if ( log.isDebugEnabled() )
{
log.debug( cacheName + ": LRUMemoryCache hit for " + key );
Modified: commons/proper/jcs/trunk/src/test-conf/TestSimpleEventHandling.ccf
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test-conf/TestSimpleEventHandling.ccf?rev=1581516&r1=1581515&r2=1581516&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/test-conf/TestSimpleEventHandling.ccf (original)
+++ commons/proper/jcs/trunk/src/test-conf/TestSimpleEventHandling.ccf Tue Mar 25 21:03:57 2014
@@ -17,7 +17,7 @@
# Cache configuration for the 'TestDiskCache' test. The memory cache has a
# a maximum of 100 objects, so objects should get pushed into the disk cache
-jcs.default=indexedDiskCache
+jcs.default=
jcs.default.cacheattributes=org.apache.commons.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=100
jcs.default.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache
@@ -42,6 +42,20 @@ jcs.region.DiskButNotAllowed.cacheattrib
jcs.region.DiskButNotAllowed.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.DiskButNotAllowed.elementattributes.IsSpool=false
+jcs.region.Maxlife=
+jcs.region.Maxlife.cacheattributes=org.apache.commons.jcs.engine.CompositeCacheAttributes
+jcs.region.Maxlife.cacheattributes.MaxObjects=200
+jcs.region.Maxlife.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache
+jcs.region.Maxlife.elementattributes.IsEternal=false
+jcs.region.Maxlife.elementattributes.MaxLifeSeconds=2
+
+jcs.region.Idletime=
+jcs.region.Idletime.cacheattributes=org.apache.commons.jcs.engine.CompositeCacheAttributes
+jcs.region.Idletime.cacheattributes.MaxObjects=200
+jcs.region.Idletime.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache
+jcs.region.Idletime.elementattributes.IsEternal=false
+jcs.region.Idletime.elementattributes.MaxLifeSeconds=300
+jcs.region.Idletime.elementattributes.IdleTime=1
# #### AUXILIARY CACHES
# Indexed Disk Cache
Modified: commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/event/SimpleEventHandlingUnitTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/event/SimpleEventHandlingUnitTest.java?rev=1581516&r1=1581515&r2=1581516&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/event/SimpleEventHandlingUnitTest.java (original)
+++ commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/event/SimpleEventHandlingUnitTest.java Tue Mar 25 21:03:57 2014
@@ -24,7 +24,6 @@ import junit.framework.TestCase;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.jcs.engine.behavior.IElementAttributes;
-import org.apache.commons.jcs.engine.control.event.behavior.ElementEventType;
import org.apache.commons.jcs.engine.control.event.behavior.IElementEvent;
import org.apache.commons.jcs.engine.control.event.behavior.IElementEventHandler;
@@ -174,6 +173,89 @@ public class SimpleEventHandlingUnitTest
}
/**
+ * Test the ELEMENT_EVENT_EXCEEDED_MAXLIFE_ONREQUEST event.
+ * @throws Exception
+ */
+ public void testExceededMaxlifeOnrequestEvent()
+ throws Exception
+ {
+ MyEventHandler meh = new MyEventHandler();
+
+ CacheAccess<String, String> jcs = JCS.getInstance( "Maxlife" );
+ // this should add the event handler to all items as they are created.
+ IElementAttributes attributes = jcs.getDefaultElementAttributes();
+ attributes.addElementEventHandler( meh );
+ jcs.setDefaultElementAttributes( attributes );
+
+ // put them in
+ for ( int i = 0; i < 200; i++ )
+ {
+ jcs.put( i + ":key", "data" + i);
+ }
+
+ // wait a bit for the items to expire
+ Thread.sleep( 3000 );
+
+ for ( int i = 0; i < 200; i++ )
+ {
+ String value = jcs.get( i + ":key");
+ assertNull("Item should be null for key " + i + ":key, but is " + value, value);
+ }
+
+ // wait a bit for it to finish
+ Thread.sleep( 100 );
+
+ // test to see if the count is right
+ assertTrue( "The number of ELEMENT_EVENT_EXCEEDED_MAXLIFE_ONREQUEST events [" + meh.getExceededMaxlifeCount()
+ + "] does not equal the number expected.", meh.getExceededMaxlifeCount() >= 200 );
+ }
+
+ /**
+ * Test the ELEMENT_EVENT_EXCEEDED_IDLETIME_ONREQUEST event.
+ * @throws Exception
+ */
+ public void testExceededIdletimeOnrequestEvent()
+ throws Exception
+ {
+ MyEventHandler meh = new MyEventHandler();
+
+ CacheAccess<String, String> jcs = JCS.getInstance( "Idletime" );
+ // this should add the event handler to all items as they are created.
+ IElementAttributes attributes = jcs.getDefaultElementAttributes();
+ attributes.addElementEventHandler( meh );
+ jcs.setDefaultElementAttributes( attributes );
+
+ // put them in
+ for ( int i = 0; i < 200; i++ )
+ {
+ jcs.put( i + ":key", "data" + i);
+ }
+
+ // update access time
+ for ( int i = 0; i < 200; i++ )
+ {
+ String value = jcs.get( i + ":key");
+ assertNotNull("Item should not be null for key " + i + ":key", value);
+ }
+
+ // wait a bit for the items to expire
+ Thread.sleep( 1500 );
+
+ for ( int i = 0; i < 200; i++ )
+ {
+ String value = jcs.get( i + ":key");
+ assertNull("Item should be null for key " + i + ":key, but is " + value, value);
+ }
+
+ // wait a bit for it to finish
+ Thread.sleep( 100 );
+
+ // test to see if the count is right
+ assertTrue( "The number of ELEMENT_EVENT_EXCEEDED_IDLETIME_ONREQUEST events [" + meh.getExceededIdletimeCount()
+ + "] does not equal the number expected.", meh.getExceededIdletimeCount() >= 200 );
+ }
+
+ /**
* Simple event counter used to verify test results.
*/
public static class MyEventHandler
@@ -188,6 +270,12 @@ public class SimpleEventHandlingUnitTest
/** times spool without disk */
private int spoolNoDiskCount = 0;
+ /** times exceeded maxlife */
+ private int exceededMaxlifeCount = 0;
+
+ /** times exceeded idle time */
+ private int exceededIdletimeCount = 0;
+
/**
* @param event
*/
@@ -196,28 +284,30 @@ public class SimpleEventHandlingUnitTest
//System.out.println( "Handling Event of Type " +
// event.getElementEvent() );
- if ( event.getElementEvent() == ElementEventType.SPOOLED_DISK_AVAILABLE )
+ switch (event.getElementEvent())
{
+ case SPOOLED_DISK_AVAILABLE:
//System.out.println( "Handling Event of Type
// ELEMENT_EVENT_SPOOLED_DISK_AVAILABLE, " + getSpoolCount() );
- setSpoolCount( getSpoolCount() + 1 );
- }
- else if ( event.getElementEvent() == ElementEventType.SPOOLED_NOT_ALLOWED )
- {
- setSpoolNotAllowedCount( getSpoolNotAllowedCount() + 1 );
- }
- else if ( event.getElementEvent() == ElementEventType.SPOOLED_DISK_NOT_AVAILABLE )
- {
- setSpoolNoDiskCount( getSpoolNoDiskCount() + 1 );
- }
- }
+ spoolCount++;
+ break;
- /**
- * @param spoolCount The spoolCount to set.
- */
- protected void setSpoolCount( int spoolCount )
- {
- this.spoolCount = spoolCount;
+ case SPOOLED_NOT_ALLOWED:
+ spoolNotAllowedCount++;
+ break;
+
+ case SPOOLED_DISK_NOT_AVAILABLE:
+ spoolNoDiskCount++;
+ break;
+
+ case EXCEEDED_MAXLIFE_ONREQUEST:
+ exceededMaxlifeCount++;
+ break;
+
+ case EXCEEDED_IDLETIME_ONREQUEST:
+ exceededIdletimeCount++;
+ break;
+ }
}
/**
@@ -229,37 +319,36 @@ public class SimpleEventHandlingUnitTest
}
/**
- * @param spoolNotAllowedCount The spoolNotAllowedCount to set.
+ * @return Returns the spoolNotAllowedCount.
*/
- protected void setSpoolNotAllowedCount( int spoolNotAllowedCount )
+ protected int getSpoolNotAllowedCount()
{
- this.spoolNotAllowedCount = spoolNotAllowedCount;
+ return spoolNotAllowedCount;
}
/**
- * @return Returns the spoolNotAllowedCount.
+ * @return Returns the spoolNoDiskCount.
*/
- protected int getSpoolNotAllowedCount()
+ protected int getSpoolNoDiskCount()
{
- return spoolNotAllowedCount;
+ return spoolNoDiskCount;
}
/**
- * @param spoolNoDiskCount The spoolNoDiskCount to set.
+ * @return the exceededMaxlifeCount
*/
- protected void setSpoolNoDiskCount( int spoolNoDiskCount )
+ protected int getExceededMaxlifeCount()
{
- this.spoolNoDiskCount = spoolNoDiskCount;
+ return exceededMaxlifeCount;
}
/**
- * @return Returns the spoolNoDiskCount.
+ * @return the exceededIdletimeCount
*/
- protected int getSpoolNoDiskCount()
+ protected int getExceededIdletimeCount()
{
- return spoolNoDiskCount;
+ return exceededIdletimeCount;
}
-
}
}