Author: asmuts
Date: Fri Feb 10 09:05:21 2006
New Revision: 376763

URL: http://svn.apache.org/viewcvs?rev=376763&view=rev
Log:
Adding tests for cache access and the memory shrinker.

Added:
    jakarta/jcs/trunk/src/test/org/apache/jcs/access/CacheAccessUnitTest.java
    
jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/MemoryCacheMockImpl.java
    
jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/shrinking/ShrinkerThreadUnitTest.java
Modified:
    jakarta/jcs/trunk/src/java/org/apache/jcs/access/CacheAccess.java
    
jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/shrinking/ShrinkerThread.java

Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/access/CacheAccess.java
URL: 
http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/java/org/apache/jcs/access/CacheAccess.java?rev=376763&r1=376762&r2=376763&view=diff
==============================================================================
--- jakarta/jcs/trunk/src/java/org/apache/jcs/access/CacheAccess.java (original)
+++ jakarta/jcs/trunk/src/java/org/apache/jcs/access/CacheAccess.java Fri Feb 
10 09:05:21 2006
@@ -242,7 +242,7 @@
     {
         if ( this.cacheControl.get( (Serializable) key ) != null )
         {
-            throw new ObjectExistsException( "Object exists for key " + key );
+            throw new ObjectExistsException( "putSafe failed.  Object exists 
in the cache for key [" + key +"].  Remove first or use a non-safe put to 
override the value." );
         }
         put( key, value );
     }

Modified: 
jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/shrinking/ShrinkerThread.java
URL: 
http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/shrinking/ShrinkerThread.java?rev=376763&r1=376762&r2=376763&view=diff
==============================================================================
--- 
jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/shrinking/ShrinkerThread.java
 (original)
+++ 
jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/shrinking/ShrinkerThread.java
 Fri Feb 10 09:05:21 2006
@@ -68,7 +68,7 @@
 
         long maxMemoryIdleTimeSeconds = 
cache.getCacheAttributes().getMaxMemoryIdleTimeSeconds();
 
-        if ( maxMemoryIdleTimeSeconds == -1 )
+        if ( maxMemoryIdleTimeSeconds < 0 )
         {
             this.maxMemoryIdleTime = -1;
         }
@@ -116,7 +116,10 @@
     {
         if ( log.isDebugEnabled() )
         {
-            log.debug( "Shrinking memory cache for: " + 
this.cache.getCompositeCache().getCacheName() );
+            if ( this.cache.getCompositeCache() != null )
+            {
+                log.debug( "Shrinking memory cache for: " + 
this.cache.getCompositeCache().getCacheName() );
+            }
         }
 
         try
@@ -155,10 +158,10 @@
                 //
                 // if ( log.isDebugEnabled() )
                 // {
-                //     log.debug( "IsEternal: " + attributes.getIsEternal() );
-                //     log.debug( "MaxLifeSeconds: "
-                //                + attributes.getMaxLifeSeconds() );
-                //     log.debug( "CreateTime:" + attributes.getCreateTime() );
+                // log.debug( "IsEternal: " + attributes.getIsEternal() );
+                // log.debug( "MaxLifeSeconds: "
+                // + attributes.getMaxLifeSeconds() );
+                // log.debug( "CreateTime:" + attributes.getCreateTime() );
                 // }
 
                 // If the element is not eternal, check if it should be
@@ -179,7 +182,7 @@
 
                 if ( !remove && ( maxMemoryIdleTime != -1 ) )
                 {
-                    if ( !spoolLimit || ( spoolCount <= this.maxSpoolPerRun ) )
+                    if ( !spoolLimit || ( spoolCount < this.maxSpoolPerRun ) )
                     {
 
                         final long lastAccessTime = 
attributes.getLastAccessTime();
@@ -191,8 +194,11 @@
                                 log.debug( "Exceeded memory idle time: " + 
cacheElement.getKey() );
                             }
 
-                            // FIXME: Shouldn't we ensure that the element is
-                            //        spooled before removing it from memory?
+                            // Shouldn't we ensure that the element is
+                            // spooled before removing it from memory?
+                            // No the disk caches have a purgatory. If it fails
+                            // to spool that does not affect the
+                            // responsibilities of the memory cache.
 
                             spoolCount++;
 
@@ -231,7 +237,7 @@
             // concurrent modifications should no longer be a problem
             // It is up to the IMemoryCache to return an array of keys
 
-            //stop for now
+            // stop for now
             return;
         }
 

Added: jakarta/jcs/trunk/src/test/org/apache/jcs/access/CacheAccessUnitTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test/org/apache/jcs/access/CacheAccessUnitTest.java?rev=376763&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/access/CacheAccessUnitTest.java 
(added)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/access/CacheAccessUnitTest.java 
Fri Feb 10 09:05:21 2006
@@ -0,0 +1,70 @@
+package org.apache.jcs.access;
+
+import org.apache.jcs.access.exception.CacheException;
+import org.apache.jcs.access.exception.ObjectExistsException;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the methods of the cache access class from which the class JCS 
extends.
+ *
+ * @author Aaron Smuts
+ *
+ */
+public class CacheAccessUnitTest
+    extends TestCase
+{
+
+    /**
+     * Verify that we get an object exists exception if the item is in
+     * the cache.
+     *
+     */
+    public void testPutSafe()
+    {
+        
+        CacheAccess access = null;
+        try
+        {
+            access = CacheAccess.getAccess( "test" );
+            
+            assertNotNull( "We should have an access class", access );
+        }
+        catch ( CacheException e )
+        {
+            fail( "Shouldn't have received an error." + e.getMessage() );
+        }
+        
+        String key = "mykey";
+        String value = "myvalue";
+        
+        try
+        {
+            access.put( key, value );
+        }
+        catch ( CacheException e )
+        {
+            fail( "Should have been able to put " + e.getMessage() );
+        }
+        String returnedValue1 = (String)access.get( key );
+        assertEquals( "Wrong value returned.", value, returnedValue1 );
+        
+        try
+        {
+            access.putSafe( key, "someothervalue" );
+            fail( "We should have received an eception since this key is 
alredy in the cache." );
+        }
+        catch ( CacheException e )
+        {
+            //e.printStackTrace();
+            // expected
+            assertTrue( "Wrong type of exception.", e instanceof 
ObjectExistsException );
+            assertTrue( "Should have the key in the error message.", 
e.getMessage().indexOf( "[" + key + "]" ) != -1 );
+        }
+
+        String returnedValue2 = (String)access.get( key );
+        assertEquals( "Wrong value returned.  Shoudl still be the original.", 
value, returnedValue2 );
+    }
+    
+    
+}

Added: 
jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/MemoryCacheMockImpl.java
URL: 
http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/MemoryCacheMockImpl.java?rev=376763&view=auto
==============================================================================
--- 
jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/MemoryCacheMockImpl.java
 (added)
+++ 
jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/MemoryCacheMockImpl.java
 Fri Feb 10 09:05:21 2006
@@ -0,0 +1,139 @@
+package org.apache.jcs.engine.memory;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.jcs.engine.behavior.ICacheElement;
+import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
+import org.apache.jcs.engine.control.CompositeCache;
+import org.apache.jcs.engine.stats.behavior.IStats;
+
+/**
+ * Mock implementation of a memory cache for testing things like the memory
+ * shrinker.
+ * 
+ * @author Aaron Smuts
+ * 
+ */
+public class MemoryCacheMockImpl
+    implements MemoryCache
+{
+
+    private ICompositeCacheAttributes cacheAttr;
+
+    private HashMap map = new HashMap();
+
+    /**
+     * The number of times waterfall was called.
+     */
+    public int waterfallCallCount = 0;
+
+    public void initialize( CompositeCache cache )
+    {
+        // TODO Auto-generated method stub
+    }
+
+    public void dispose()
+        throws IOException
+    {
+        // TODO Auto-generated method stub
+
+    }
+
+    public int getSize()
+    {
+        return map.size();
+    }
+
+    public IStats getStatistics()
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public Iterator getIterator()
+    {
+        // return
+        return null;
+    }
+
+    public Object[] getKeyArray()
+    {
+        return map.keySet().toArray();
+    }
+
+    public boolean remove( Serializable key )
+        throws IOException
+    {
+        return map.remove( key ) != null;
+    }
+
+    public void removeAll()
+        throws IOException
+    {
+        map.clear();
+    }
+
+    public ICacheElement get( Serializable key )
+        throws IOException
+    {
+        return (ICacheElement) map.get( key );
+    }
+
+    public ICacheElement getQuiet( Serializable key )
+        throws IOException
+    {
+        return (ICacheElement) map.get( key );
+    }
+
+    public void waterfal( ICacheElement ce )
+        throws IOException
+    {
+        waterfallCallCount++;
+    }
+
+    public void update( ICacheElement ce )
+        throws IOException
+    {
+        if ( ce != null )
+        {
+            map.put( ce.getKey(), ce );
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.jcs.engine.memory.MemoryCache#getCacheAttributes()
+     */
+    public ICompositeCacheAttributes getCacheAttributes()
+    {
+        return cacheAttr;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see 
org.apache.jcs.engine.memory.MemoryCache#setCacheAttributes(org.apache.jcs.engine.behavior.ICompositeCacheAttributes)
+     */
+    public void setCacheAttributes( ICompositeCacheAttributes cattr )
+    {
+        this.cacheAttr = cattr;
+    }
+
+    public CompositeCache getCompositeCache()
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public Set getGroupKeys( String group )
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+}

Added: 
jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/shrinking/ShrinkerThreadUnitTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/shrinking/ShrinkerThreadUnitTest.java?rev=376763&view=auto
==============================================================================
--- 
jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/shrinking/ShrinkerThreadUnitTest.java
 (added)
+++ 
jakarta/jcs/trunk/src/test/org/apache/jcs/engine/memory/shrinking/ShrinkerThreadUnitTest.java
 Fri Feb 10 09:05:21 2006
@@ -0,0 +1,114 @@
+package org.apache.jcs.engine.memory.shrinking;
+
+import junit.framework.TestCase;
+
+import org.apache.jcs.engine.CacheElement;
+import org.apache.jcs.engine.CompositeCacheAttributes;
+import org.apache.jcs.engine.ElementAttributes;
+import org.apache.jcs.engine.behavior.ICacheElement;
+import org.apache.jcs.engine.memory.MemoryCacheMockImpl;
+
+/**
+ * This tests the functionality of the shrinker thread.
+ * 
+ * @author Aaron Smuts
+ * 
+ */
+public class ShrinkerThreadUnitTest
+    extends TestCase
+{
+
+    /**
+     * Setup cache attributes in mock. Create the shrinker with the mock. Add
+     * some elements into the mock memory cache see that they get spooled.
+     * 
+     * @throws Exception
+     * 
+     */
+    public void testSimpleShrink()
+        throws Exception
+    {
+        MemoryCacheMockImpl memory = new MemoryCacheMockImpl();
+
+        CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes();
+        cacheAttr.setMaxMemoryIdleTimeSeconds( 1 );
+        cacheAttr.setMaxSpoolPerRun( 10 );
+
+        memory.setCacheAttributes( cacheAttr );
+
+        String key = "key";
+        String value = "value";
+
+        ICacheElement element = new CacheElement( "testRegion", key, value );
+
+        ElementAttributes elementAttr = new ElementAttributes();
+        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 );
+
+        ICacheElement returnedElement2 = memory.get( key );
+        assertTrue( "Waterfall should have been called.", 
memory.waterfallCallCount > 0 );
+        assertNull( "We not should have received an element.  It should have 
been spooled.", returnedElement2 );
+    }
+
+    /**
+     * Add 10 to the memory cache. Set the spool per run limit to 3.
+     * 
+     * @throws Exception
+     */
+    public void testSimpleShrinkMutiple()
+        throws Exception
+    {
+        MemoryCacheMockImpl memory = new MemoryCacheMockImpl();
+
+        CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes();
+        cacheAttr.setMaxMemoryIdleTimeSeconds( 1 );
+        cacheAttr.setMaxSpoolPerRun( 3 );
+
+        memory.setCacheAttributes( cacheAttr );
+
+        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.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 );
+        
+        assertEquals( "Wrong number of elements remain.", 7, memory.getSize() 
);
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to