KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.eviction;
8
9 import junit.framework.TestCase;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.factories.XmlConfigurationParser;
13 import org.jboss.cache.lock.IsolationLevel;
14 import org.jboss.cache.misc.TestingUtil;
15
16 /**
17  * Unit tests for FIFOPolicy.
18  *
19  * @author Daniel Huang (dhuang@jboss.org)
20  * @version $Revision: 1.12 $
21  */

22 public class FIFOPolicyTest extends TestCase
23 {
24    CacheImpl cache;
25    int wakeupIntervalMillis = 0;
26    final String JavaDoc ROOT_STR = "/test";
27    Throwable JavaDoc t1_ex, t2_ex;
28    final long DURATION = 10000;
29    boolean isTrue;
30
31    public FIFOPolicyTest(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       log("wakeupInterval is " + wakeupIntervalMillis);
42       if (wakeupIntervalMillis < 0)
43          fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis);
44
45       t1_ex = t2_ex = null;
46       isTrue = true;
47    }
48
49    void initCaches() throws Exception JavaDoc
50    {
51       cache = new CacheImpl();
52       cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-fifo-eviction-service.xml")); // read in generic local xml
53
cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
54       cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
55       cache.start();
56    }
57
58    public void tearDown() throws Exception JavaDoc
59    {
60       super.tearDown();
61       cache.stop();
62    }
63
64    public void testEviction()
65    {
66       String JavaDoc rootStr = "/org/jboss/test/data/";
67       for (int i = 0; i < 10; i++)
68       {
69          String JavaDoc str = rootStr + i;
70          Fqn fqn = Fqn.fromString(str);
71          try
72          {
73             cache.put(fqn, str, str);
74          }
75          catch (Exception JavaDoc e)
76          {
77             fail("Failed to insert data" + e);
78             e.printStackTrace();
79          }
80       }
81
82       System.out.println(cache.toString(true));
83       TestingUtil.sleepThread(wakeupIntervalMillis + 500);
84
85       System.out.println(cache.toString(true));
86       try
87       {
88          String JavaDoc val = (String JavaDoc) cache.get(rootStr + "3", rootStr + "3");
89          assertNull("DataNode should be empty ", val);
90          assertNull(cache.get(rootStr + "1", rootStr + "1"));
91          assertNull(cache.get(rootStr + "2", rootStr + "2"));
92          assertNull(cache.get(rootStr + "0", rootStr + "0"));
93          assertNull(cache.get(rootStr + "4", rootStr + "4"));
94
95          assertNotNull(cache.get(rootStr + "5", rootStr + "5"));
96       }
97       catch (Exception JavaDoc e)
98       {
99          e.printStackTrace();
100          fail("Failed to get" + e);
101       }
102    }
103
104    public void testEviction2() throws Exception JavaDoc
105    {
106       String JavaDoc rootStr = "/org/jboss/data";
107       int maxNodes = 5000;
108       for (int i = 0; i < maxNodes * 2; i++)
109       {
110          String JavaDoc str = rootStr + i;
111          Fqn fqn = Fqn.fromString(str);
112          cache.put(fqn, str, str);
113       }
114
115       TestingUtil.sleepThread(wakeupIntervalMillis + 500);
116       assertEquals("Number of nodes", maxNodes + 2, cache.getNumberOfNodes());
117       for (int i = 0; i < maxNodes; i++)
118       {
119          String JavaDoc n = rootStr + i;
120          assertNull("DataNode should be empty " + cache.get(n) + " " + n,
121                  cache.get(n, n));
122       }
123
124       for (int i = maxNodes; i < maxNodes * 2; i++)
125       {
126          assertNotNull(cache.get(rootStr + Integer.toString(i), rootStr + Integer.toString(i)));
127       }
128
129       // add another node to cache. this should push node 5000 out of cache.
130
cache.put(rootStr + "a", rootStr + "a", rootStr + "a");
131       for (int i = maxNodes + 1; i < maxNodes; i++)
132       {
133          assertNotNull(cache.get(rootStr + Integer.toString(i), rootStr + Integer.toString(i)));
134       }
135
136       assertNotNull(cache.get(rootStr + "a", rootStr + "a"));
137    }
138
139    public void testNodeVisited()
140    {
141       String JavaDoc rootStr = "/org/jboss/test/data/";
142       for (int i = 0; i < 10; i++)
143       {
144          String JavaDoc str = rootStr + i;
145          Fqn fqn = Fqn.fromString(str);
146          try
147          {
148             cache.put(fqn, str, str);
149          }
150          catch (Exception JavaDoc e)
151          {
152             fail("Failed to insert data" + e);
153             e.printStackTrace();
154          }
155       }
156       System.out.println("cache is:\n" + cache.toString(true));
157
158       int period = wakeupIntervalMillis + 500;
159
160       log("sleeping for " + period + "ms");
161       TestingUtil.sleepThread(period); // it really depends the eviction thread time.
162
try
163       {
164          for (int i = 0; i < 5; i++)
165          {
166             String JavaDoc str = rootStr + Integer.toString(i);
167             Fqn fqn = Fqn.fromString(str);
168             assertNull(cache.get(fqn, str));
169          }
170          for (int i = 5; i < 10; i++)
171          {
172             String JavaDoc str = rootStr + Integer.toString(i);
173             Fqn fqn = Fqn.fromString(str);
174             assertNotNull(cache.get(fqn, str));
175          }
176
177          TestingUtil.sleepThread(period);
178          // since it is FIFO if we leave it alone and revisit, cache should remain the same.
179
for (int i = 5; i < 10; i++)
180          {
181             String JavaDoc str = rootStr + Integer.toString(i);
182             Fqn fqn = Fqn.fromString(str);
183             cache.get(fqn, str); // just to keep it fresh
184
System.out.println("-- sleeping for " + period + "ms");
185          }
186       }
187       catch (Exception JavaDoc e)
188       {
189          e.printStackTrace();
190          fail("Failed to evict" + e);
191       }
192    }
193
194    public void testNodeRemoved()
195    {
196       String JavaDoc rootStr = "/org/jboss/test";
197       for (int i = 0; i < 10; i++)
198       {
199          String JavaDoc str = rootStr + i + "/" + i;
200          Fqn fqn = Fqn.fromString(str);
201          String JavaDoc str2 = rootStr + i;
202          Fqn fqn2 = Fqn.fromString(str2);
203          try
204          {
205             cache.put(fqn, str, str);
206             cache.put(fqn2, str2, str2);
207          }
208          catch (Exception JavaDoc e)
209          {
210             fail("Failed to insert data" + e);
211             e.printStackTrace();
212          }
213       }
214
215       int period = (wakeupIntervalMillis + 500);
216       log("period is " + period);
217       TestingUtil.sleepThread(period);
218       String JavaDoc str1 = rootStr + "7";
219       Fqn fqn1 = Fqn.fromString(str1);
220       String JavaDoc str2 = rootStr + "7/7";
221       Fqn fqn2 = Fqn.fromString(str2);
222       try
223       {
224          assertNotNull(cache.get(fqn2, str2));
225          assertNotNull(cache.get(fqn1, str1));
226          cache.remove(fqn2);
227          TestingUtil.sleepThread(period);
228          assertNull(cache.get(fqn2, str2));
229          assertNotNull(cache.get(fqn1, str1));
230          cache.remove(fqn1);
231          TestingUtil.sleepThread(period);
232          assertNull(cache.get(fqn1, str1));
233          assertNull(cache.get(fqn2, str2));
234
235          String JavaDoc str3 = rootStr + "5/5";
236          String JavaDoc str4 = rootStr + "5";
237          Fqn fqn3 = Fqn.fromString(str3);
238          Fqn fqn4 = Fqn.fromString(str4);
239          assertNotNull(cache.get(fqn3, str3));
240          assertNotNull(cache.get(fqn4, str4));
241
242          TestingUtil.sleepThread(period);
243
244          // remove the node above fqn4 /org/jboss/test/5 will cascade the delete into /org/jboss/test/5/5
245
cache.remove(fqn4);
246
247          TestingUtil.sleepThread(period);
248          assertNull(cache.get(fqn3, str3));
249          assertNull(cache.get(fqn4, str4));
250       }
251       catch (Exception JavaDoc e)
252       {
253          e.printStackTrace();
254          fail("Failed to evict" + e);
255       }
256    }
257
258
259    class MyPutter extends Thread JavaDoc
260    {
261
262       public MyPutter(String JavaDoc name)
263       {
264          super(name);
265       }
266
267       public void run()
268       {
269          int i = 0;
270          final String JavaDoc myName = ROOT_STR + "/test1/node" + getName();
271          while (isTrue)
272          {
273             try
274             {
275                cache.put(myName + i++, "value", i);
276                sleep(1);
277             }
278             catch (Throwable JavaDoc e)
279             {
280                e.printStackTrace();
281                if (t1_ex == null)
282                   t1_ex = e;
283             }
284          }
285       }
286    }
287
288
289    public void testConcurrentPutAndEvict() throws Exception JavaDoc
290    {
291       cache.stop();
292       cache.destroy();
293       cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
294       cache.start();
295       cache.put(ROOT_STR + "/concurrentPutAndEvict", "value", 1);
296
297       for (int i = 0; i < 10; i++)
298       {
299          new MyPutter("Putter" + i).start();
300       }
301
302       int counter = 0;
303       while (true)
304       {
305          counter++;
306          if (t1_ex != null)
307          {
308             fail("Exception generated in put() " + t1_ex);
309          }
310          log("nodes/locks: " + cache.getNumberOfNodes() + "/" + cache.getNumberOfLocksHeld());
311          TestingUtil.sleepThread(1000);
312          if (counter > 10)
313          { // run for 10 seconds
314
isTrue = false;
315             break;
316          }
317       }
318    }
319
320    void log(String JavaDoc msg)
321    {
322       System.out.println("-- " + msg);
323    }
324
325 }
326
Popular Tags