KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.eviction;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.AbstractCacheListener;
5 import org.jboss.cache.CacheImpl;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.factories.XmlConfigurationParser;
8 import org.jboss.cache.misc.TestingUtil;
9
10 /**
11  * @author Ben Wang, Feb 11, 2004
12  */

13 public class ReplicatedLRUPolicyTest extends TestCase
14 {
15    CacheImpl cache_, cache1_, cache2_;
16    int wakeupIntervalMillis_ = 0;
17    EvictionListener listener_;
18
19    public ReplicatedLRUPolicyTest(String JavaDoc s)
20    {
21       super(s);
22       listener_ = new EvictionListener();
23    }
24
25    public void setUp() throws Exception JavaDoc
26    {
27       super.setUp();
28       cache_ = new CacheImpl();
29       initCaches(cache_);
30       cache_.getConfiguration().setUseRegionBasedMarshalling(true);
31       cache_.start();
32       cache_.getNotifier().addCacheListener(listener_);
33       listener_.resetCounter();
34
35       cache2_ = new CacheImpl();
36       cache2_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml")); // read in generic local xml
37
cache2_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
38       cache2_.getConfiguration().setUseRegionBasedMarshalling(true);
39       cache2_.start();
40
41       wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
42       log("wakeupInterval is " + wakeupIntervalMillis_);
43       if (wakeupIntervalMillis_ <= 0)
44       {
45          fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
46       }
47    }
48
49    void initCaches(CacheImpl cache) throws Exception JavaDoc
50    {
51       cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-eviction-service.xml")); // read in generic local xml
52
cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
53    }
54
55    public void tearDown() throws Exception JavaDoc
56    {
57       super.tearDown();
58       cache_.stop();
59       cache2_.stop();
60    }
61
62    /**
63     * Test local eviction policy that failed for eviction event.
64     */

65    public void testBasic() throws Exception JavaDoc
66    {
67       String JavaDoc rootStr = "/org/jboss/test/data/";
68       String JavaDoc str = rootStr + "0";
69       cache_.put(str, str, str);
70
71       TestingUtil.sleepThread(30000);
72       String JavaDoc val = (String JavaDoc) cache_.get(str, str);
73       assertNull("DataNode should be evicted already ", val);
74       assertEquals("Eviction counter ", 1, listener_.getCounter());
75       val = (String JavaDoc) cache2_.get(str, str);
76       assertNotNull("DataNode should not be evicted here ", val);
77       assertEquals("Eviction counter ", 1, listener_.getCounter());
78    }
79
80    public void testEviction() throws Exception JavaDoc
81    {
82       String JavaDoc rootStr = "/org/jboss/test/data/";
83       for (int i = 0; i < 10; i++)
84       {
85          String JavaDoc str = rootStr + i;
86          Fqn fqn = Fqn.fromString(str);
87          cache_.put(fqn, str, str);
88       }
89
90       TestingUtil.sleepThread(2 * wakeupIntervalMillis_);
91       String JavaDoc val = (String JavaDoc) cache_.get(rootStr + "3", rootStr + "3");
92       assertNull("DataNode should be evicted already ", val);
93       val = (String JavaDoc) cache2_.get(rootStr + "3", rootStr + "3");
94       assertNotNull("DataNode should not be evicted here ", val);
95    }
96
97    public void testEvictionReplication() throws Exception JavaDoc
98    {
99       String JavaDoc rootStr = "/org/jboss/test/data/";
100       for (int i = 0; i < 10; i++)
101       {
102          String JavaDoc str = rootStr + i;
103          Fqn fqn = Fqn.fromString(str);
104          cache_.put(fqn, str, str);
105       }
106
107       TestingUtil.sleepThread(wakeupIntervalMillis_ - 1000);
108       String JavaDoc str = rootStr + "7";
109       Fqn fqn = Fqn.fromString(str);
110       cache_.get(fqn, str);
111       TestingUtil.sleepThread(wakeupIntervalMillis_);
112
113       String JavaDoc val = (String JavaDoc) cache_.get(rootStr + "3", rootStr + "3");
114       assertNull("DataNode should be empty ", val);
115       val = (String JavaDoc) cache2_.get(rootStr + "7", rootStr + "7");
116       assertNotNull("DataNode should not be null", val);
117    }
118
119    void log(String JavaDoc msg)
120    {
121       System.out.println("-- " + msg);
122    }
123
124    class EvictionListener extends AbstractCacheListener
125    {
126       int counter = 0;
127
128       public int getCounter()
129       {
130          return counter;
131       }
132
133       public void resetCounter()
134       {
135          counter = 0;
136       }
137
138       public void nodeEvicted(Fqn fqn, boolean pre, boolean isLocal)
139       {
140          if (pre) counter++;
141       }
142    }
143 }
144
Popular Tags