Author: tv
Date: Tue Dec 10 13:14:49 2013
New Revision: 1549833

URL: http://svn.apache.org/r1549833
Log:
Add multi-thread test for JCS-73

Added:
    commons/proper/jcs/trunk/src/test-conf/TestJCS-73.ccf
    
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/JCSConcurrentCacheAccessUnitTest.java
   (with props)

Added: commons/proper/jcs/trunk/src/test-conf/TestJCS-73.ccf
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test-conf/TestJCS-73.ccf?rev=1549833&view=auto
==============================================================================
--- commons/proper/jcs/trunk/src/test-conf/TestJCS-73.ccf (added)
+++ commons/proper/jcs/trunk/src/test-conf/TestJCS-73.ccf Tue Dec 10 13:14:49 
2013
@@ -0,0 +1,42 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# Cache configuration for the 'JCSConcurrentCacheAccessUnitTest' test.
+
+jcs.default=CACHE
+jcs.default.cacheattributes=org.apache.commons.jcs.engine.CompositeCacheAttributes
+jcs.default.cacheattributes.MaxObjects=-1
+jcs.default.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache
+jcs.default.cacheattributes.DiskUsagePatternName=UPDATE
+jcs.default.cacheattributes.UseMemoryShrinker=true
+jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=10
+jcs.default.cacheattributes.ShrinkerIntervalSeconds=10
+jcs.default.elementattributes=org.apache.commons.jcs.engine.ElementAttributes
+jcs.default.elementattributes.IsRemote=false
+jcs.default.elementattributes.IsLateral=false
+jcs.default.elementattributes.IsSpool=true
+jcs.default.elementattributes.IsEternal=true
+
+jcs.auxiliary.CACHE=org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory
+jcs.auxiliary.CACHE.attributes=org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes
+jcs.auxiliary.CACHE.attributes.DiskPath=target/test-sandbox/concurrent_cache
+jcs.auxiliary.CACHE.attributes.MaxPurgatorySize=-1
+jcs.auxiliary.CACHE.attributes.MaxKeySize=-1
+jcs.auxiliary.CACHE.attributes.MaxRecycleBinSize=500
+jcs.auxiliary.CACHE.attributes.ShutdownSpoolTimeLimit=60
+jcs.auxiliary.CACHE.attributes.OptimizeAtRemoveCount=30000
+jcs.auxiliary.CACHE.attributes.OptimizeOnShutdown=true
+jcs.auxiliary.CACHE.attributes.EventQueueType=SINGLE

Added: 
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/JCSConcurrentCacheAccessUnitTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/JCSConcurrentCacheAccessUnitTest.java?rev=1549833&view=auto
==============================================================================
--- 
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/JCSConcurrentCacheAccessUnitTest.java
 (added)
+++ 
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/JCSConcurrentCacheAccessUnitTest.java
 Tue Dec 10 13:14:49 2013
@@ -0,0 +1,164 @@
+package org.apache.commons.jcs;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.commons.jcs.access.exception.CacheException;
+
+/**
+ * Test Case for JCS-73, modeled after the Groovy code by Alexander Kleymenov
+ *
+ * @author Thomas Vandahl
+ *
+ */
+public class JCSConcurrentCacheAccessUnitTest
+    extends TestCase
+{
+    /**
+     * the cache instance
+     */
+    protected JCS<Integer, String> cache;
+
+    /**
+     * the group name
+     */
+    protected String group = "group";
+
+    /**
+     * the error count
+     */
+    protected AtomicInteger errcount;
+
+    @Override
+       protected void setUp() throws Exception
+       {
+        super.setUp();
+        JCS.setConfigFilename( "/TestJCS-73.ccf" );
+        cache = JCS.getInstance( "cache" );
+        errcount = new AtomicInteger(0);
+       }
+
+    @Override
+    protected void tearDown()
+        throws Exception
+    {
+        super.tearDown();
+        cache.clear();
+        cache.dispose();
+    }
+
+    /**
+     * Worker thread
+     */
+    private class Worker extends Thread
+    {
+       @Override
+               public void run()
+               {
+                       String name = getName();
+
+                       for (int idx = 0; idx < 10000; idx++)
+                       {
+                               if (idx > 0)
+                               {
+                                       // get previously stored value
+                           String res = 
cache.getFromGroup(Integer.valueOf(idx-1), group);
+
+                           if (res == null)
+                           {
+                               // null value got inspite of the fact it was 
placed in cache!
+                               System.out.println("ERROR: for " + idx + " in " 
+ name);
+                               errcount.incrementAndGet();
+
+                               // try to get the value again:
+                               int n = 5;
+                               while (n-- > 0)
+                               {
+                                   res = 
cache.getFromGroup(Integer.valueOf(idx-1), group);
+                                   if (res != null)
+                                   {
+                                       // the value finally appeared in cache
+                                       System.out.println("ERROR FIXED for " + 
idx + ": " + res + " " + name);
+                                       errcount.decrementAndGet();
+                                       break;
+                                   }
+
+                                   System.out.println("ERROR STILL PERSISTS 
for " + idx + " in " + name);
+                                   try
+                                   {
+                                                               
Thread.sleep(1000);
+                                                       }
+                                   catch (InterruptedException e)
+                                                       {
+                                                               // continue
+                                                       }
+                               }
+                           }
+
+                           Assert.assertEquals("Values do not match", 
String.valueOf(idx-1), res);
+                               }
+
+                                // put value in the cache
+                       try
+                       {
+                                       cache.putInGroup(Integer.valueOf(idx), 
group, String.valueOf(idx));
+                               }
+                       catch (CacheException e)
+                       {
+                               // continue
+                               }
+
+                       if ((idx % 1000) == 0)
+                       {
+                               System.out.println(name + " " + idx);
+                       }
+                       }
+
+               }
+    }
+
+       /**
+     *
+     * @throws Exception
+     */
+    public void testConcurrentAccess()
+        throws Exception
+    {
+       Worker[] worker = new Worker[10];
+
+        for (int i = 0; i < 10; i++)
+        {
+               worker[i] = new Worker();
+               worker[i].start();
+        }
+
+        for (int i = 0; i < 10; i++)
+        {
+               worker[i].join();
+        }
+
+        assertEquals("Error count should be 0",  0, errcount.intValue());
+    }
+
+}

Propchange: 
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/JCSConcurrentCacheAccessUnitTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to