KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > passivation > ConcurrentPassivationTest


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.passivation;
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.config.CacheLoaderConfig;
16 import org.jboss.cache.config.Configuration;
17 import org.jboss.cache.factories.XmlConfigurationParser;
18 import org.jboss.cache.misc.TestingUtil;
19
20 /**
21  * Tests cache behavior in the presence of concurrent passivation.
22  *
23  * @author Brian Stansberry
24  * @version $Revision: 1.9 $
25  */

26 public class ConcurrentPassivationTest extends TestCase
27 {
28    private CacheImpl cache_;
29    private int wakeupIntervalMillis_ = 0;
30
31    public ConcurrentPassivationTest(String JavaDoc s)
32    {
33       super(s);
34    }
35
36    public void setUp() throws Exception JavaDoc
37    {
38       super.setUp();
39       initCaches();
40       wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
41       if (wakeupIntervalMillis_ < 0)
42          fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
43
44    }
45
46    void initCaches() throws Exception JavaDoc
47    {
48       TestingUtil.recursiveFileRemove("/tmp/JBossCacheFileCacheLoader");
49       cache_ = new CacheImpl();
50       cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-passivation-service.xml")); // read in generic local xml
51
cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
52
53       // hack in the path to the file store in the cache loaders
54
injectCacheLoaderLocation(cache_.getConfiguration(), "/tmp/JBossCacheFileCacheLoader");
55       cache_.start();
56    }
57
58    private void injectCacheLoaderLocation(Configuration configuration, String JavaDoc location)
59    {
60       for (CacheLoaderConfig.IndividualCacheLoaderConfig iclc : configuration.getCacheLoaderConfig().getIndividualCacheLoaderConfigs())
61       {
62          iclc.getProperties().put("location", location);
63       }
64    }
65
66
67    public void tearDown() throws Exception JavaDoc
68    {
69       super.tearDown();
70       TestingUtil.recursiveFileRemove("/tmp/JBossCacheFileCacheLoader");
71       cache_.stop();
72       cache_ = null;
73    }
74
75    public void testConcurrentPassivation() throws Exception JavaDoc
76    {
77       Fqn base = Fqn.fromString("/org/jboss/test/data/concurrent/passivation");
78
79       // Create a bunch of nodes; more than the /org/jboss/test/data
80
// region's maxNodes so we know eviction will kick in
81
for (int i = 0; i < 35000; i++)
82       {
83          cache_.put(new Fqn(base, i / 100), new Integer JavaDoc(i), "value");
84       }
85
86       // Loop for long enough to have 5 runs of the eviction thread
87
long loopDone = System.currentTimeMillis() + (5 * wakeupIntervalMillis_);
88       while (System.currentTimeMillis() < loopDone)
89       {
90          // If any get returns null, that's a failure
91
for (int i = 0; i < 35000; i++)
92          {
93             Fqn fqn = new Fqn(base, i / 100);
94             Integer JavaDoc key = i;
95             assertNotNull("found value under Fqn " + fqn + " and key " + key,
96                     cache_.get(fqn, key));
97          }
98       }
99    }
100
101    public static Test suite()
102    {
103       return new TestSuite(org.jboss.cache.passivation.ConcurrentPassivationTest.class);
104    }
105
106    public static void main(String JavaDoc[] args)
107    {
108       junit.textui.TestRunner.run(org.jboss.cache.passivation.ConcurrentPassivationTest.suite());
109    }
110 }
111
Popular Tags