KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > ConcurrentEvictionTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.eviction;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.jboss.cache.CacheImpl;
14 import org.jboss.cache.Fqn;
15 import org.jboss.cache.factories.XmlConfigurationParser;
16 import org.jboss.cache.misc.TestingUtil;
17
18 /**
19  * Tests cache behavior in the presence of concurrent passivation.
20  *
21  * @author Brian Stansberry
22  * @version $Revision: 1.7 $
23  */

24 public class ConcurrentEvictionTest extends TestCase
25 {
26    private CacheImpl cache_;
27    private int wakeupIntervalMillis_ = 0;
28
29    public ConcurrentEvictionTest(String JavaDoc s)
30    {
31       super(s);
32    }
33
34    protected void setUp() throws Exception JavaDoc
35    {
36       super.setUp();
37       initCaches();
38       wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
39       if (wakeupIntervalMillis_ < 0)
40          fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
41
42    }
43
44    void initCaches() throws Exception JavaDoc
45    {
46       TestingUtil.recursiveFileRemove("/tmp/JBossCacheFileCacheLoader");
47       cache_ = new CacheImpl();
48       cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-eviction-cacheloader-service.xml")); // read in generic local xml
49
cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
50       cache_.start();
51    }
52
53    public void tearDown() throws Exception JavaDoc
54    {
55       super.tearDown();
56       TestingUtil.recursiveFileRemove("/tmp/JBossCacheFileCacheLoader");
57       cache_.stop();
58       cache_ = null;
59    }
60
61    public void testConcurrentEviction() throws Exception JavaDoc
62    {
63       Fqn base = Fqn.fromString("/org/jboss/test/data/concurrent/eviction");
64
65       // Create a bunch of nodes; more than the /org/jboss/test/data
66
// region's maxNodes so we know eviction will kick in
67
for (int i = 0; i < 1000; i++)
68       {
69          cache_.put(new Fqn(base, i / 100), new Integer JavaDoc(i), "value");
70       }
71
72       // Loop for long enough to have 5 runs of the eviction thread
73
long loopDone = System.currentTimeMillis() + (5 * wakeupIntervalMillis_);
74       while (System.currentTimeMillis() < loopDone)
75       {
76          // If any get returns null, that's a failure
77
for (int i = 0; i < 1000; i++)
78          {
79             Fqn fqn = new Fqn(base, i / 100);
80             Integer JavaDoc key = i;
81             assertNotNull("found value under Fqn " + fqn + " and key " + key,
82                     cache_.get(fqn, key));
83          }
84       }
85    }
86
87    public static Test suite()
88    {
89       return new TestSuite(ConcurrentEvictionTest.class);
90    }
91 }
92
Popular Tags