KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.Configuration;
13 import org.jboss.cache.factories.XmlConfigurationParser;
14 import org.jboss.cache.lock.IsolationLevel;
15 import org.jboss.cache.misc.TestingUtil;
16
17 /**
18  * Unit tests for LFU Policy.
19  *
20  * @author Daniel Huang (dhuang@jboss.org)
21  * @version $Revision: 1.10 $
22  */

23 public class LFUPolicyTest extends TestCase
24 {
25    CacheImpl cache;
26    int wakeupIntervalMillis = 0;
27    final String JavaDoc ROOT_STR = "/test";
28    Throwable JavaDoc t1_ex, t2_ex;
29    final long DURATION = 10000;
30    boolean isTrue;
31
32    public LFUPolicyTest(String JavaDoc s)
33    {
34       super(s);
35    }
36
37    public void setUp() throws Exception JavaDoc
38    {
39       super.setUp();
40       initCaches();
41       wakeupIntervalMillis = cache.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
42       log("wakeupInterval is " + wakeupIntervalMillis);
43       if (wakeupIntervalMillis < 0)
44          fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis);
45
46       t1_ex = t2_ex = null;
47       isTrue = true;
48    }
49
50    void initCaches() throws Exception JavaDoc
51    {
52       cache = new CacheImpl();
53       XmlConfigurationParser parser = new XmlConfigurationParser();
54       Configuration c = parser.parseFile("META-INF/local-lfu-eviction-service.xml");
55       cache.setConfiguration(c);
56       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
57       c.setIsolationLevel(IsolationLevel.SERIALIZABLE);
58       cache.start();
59    }
60
61    public void tearDown() throws Exception JavaDoc
62    {
63       super.tearDown();
64       cache.stop();
65    }
66
67    public void testEviction() throws Exception JavaDoc
68    {
69       String JavaDoc rootStr = "/org/jboss/data/";
70       for (int i = 0; i < 8000; i++)
71       {
72          String JavaDoc str = rootStr + i;
73          Fqn fqn = Fqn.fromString(str);
74          try
75          {
76             cache.put(fqn, str, str);
77          }
78          catch (Exception JavaDoc e)
79          {
80             fail("Failed to insert data" + e);
81             e.printStackTrace();
82          }
83
84          // visit odd numbered nodes an extra time to make them get evicted last.
85
if (i % 2 != 0)
86          {
87             cache.get(fqn, str);
88          }
89       }
90
91       long period = wakeupIntervalMillis + 500;
92       TestingUtil.sleepThread(period);
93
94       for (int i = 0; i < 8000; i++)
95       {
96          String JavaDoc str = rootStr + i;
97          Fqn fqn = Fqn.fromString(str);
98
99          if (i % 2 == 0)
100          {
101             assertNull(cache.get(fqn, str));
102          }
103          else
104          {
105             assertNotNull(cache.get(fqn, str));
106          }
107       }
108    }
109
110    public void testNodeVisited()
111    {
112       String JavaDoc rootStr = "/org/jboss/test/data/";
113       for (int i = 0; i < 10; i++)
114       {
115          String JavaDoc str = rootStr + i;
116          Fqn fqn = Fqn.fromString(str);
117          try
118          {
119             cache.put(fqn, str, str);
120          }
121          catch (Exception JavaDoc e)
122          {
123             fail("Failed to insert data" + e);
124             e.printStackTrace();
125          }
126       }
127       System.out.println("cache is:\n" + cache.toString(true));
128
129       int period = wakeupIntervalMillis + 500;
130
131       log("sleeping for " + period + "ms");
132       TestingUtil.sleepThread(period); // it really depends the eviction thread time.
133

134       System.out.println("cache is:\n" + cache.toString(true));
135
136       try
137       {
138          for (int i = 0; i < 5; i++)
139          {
140             String JavaDoc str = rootStr + Integer.toString(i);
141             Fqn fqn = Fqn.fromString(str);
142             assertNull(cache.get(fqn, str));
143          }
144          for (int i = 5; i < 10; i++)
145          {
146             String JavaDoc str = rootStr + Integer.toString(i);
147             Fqn fqn = Fqn.fromString(str);
148             assertNotNull(cache.get(fqn, str));
149          }
150
151          TestingUtil.sleepThread(period);
152
153          System.out.println(cache.toString(true));
154
155          // since min is 5 the cache won't deplete past 5 nodes left in the cache.
156
for (int i = 5; i < 10; i++)
157          {
158             String JavaDoc str = rootStr + Integer.toString(i);
159             Fqn fqn = Fqn.fromString(str);
160             assertNotNull(cache.get(fqn, str));
161          }
162
163          // now we add some more nodes and we selectively visit some older nodes but not all. they should not get
164
// evicted when the thread next runs.
165
for (int i = 5; i < 7; i++)
166          {
167             String JavaDoc str = rootStr + Integer.toString(i);
168             Fqn fqn = Fqn.fromString(str);
169             cache.get(fqn, str);
170          }
171
172          // add 2 more to push the limit to 5 so we cause the old unvisited nodes to get evicted.
173
for (int i = 10; i < 13; i++)
174          {
175             String JavaDoc str = rootStr + Integer.toString(i);
176             Fqn fqn = Fqn.fromString(str);
177             cache.put(fqn, str, str);
178
179             // now bring up their hit count for LFU purposes (cache hit count based eviction).
180
for (int k = 0; k < 10; k++)
181             {
182                cache.get(fqn, str);
183             }
184          }
185
186          TestingUtil.sleepThread(period);
187
188          System.out.println(cache.toString(true));
189
190          // now make sure we still only have 5 nodes and they are the ones we expect based on LFU
191
for (int i = 5; i < 7; i++)
192          {
193             String JavaDoc str = rootStr + Integer.toString(i);
194             Fqn fqn = Fqn.fromString(str);
195             assertNotNull(cache.get(fqn, str));
196          }
197
198          for (int i = 7; i < 10; i++)
199          {
200             String JavaDoc str = rootStr + Integer.toString(i);
201             Fqn fqn = Fqn.fromString(str);
202             assertNull(cache.get(fqn, str));
203          }
204
205          for (int i = 10; i < 13; i++)
206          {
207             String JavaDoc str = rootStr + Integer.toString(i);
208             Fqn fqn = Fqn.fromString(str);
209             assertNotNull(cache.get(fqn, str));
210          }
211
212       }
213       catch (Exception JavaDoc e)
214       {
215          e.printStackTrace();
216          fail("Failed to evict" + e);
217       }
218    }
219
220    public void testNodeRemoved() throws Exception JavaDoc
221    {
222       String JavaDoc rootStr = "/org/jboss/data/";
223       for (int i = 0; i < 5000; i++)
224       {
225          String JavaDoc str = rootStr + i;
226          Fqn fqn = Fqn.fromString(str);
227          try
228          {
229             cache.put(fqn, str, str);
230          }
231          catch (Exception JavaDoc e)
232          {
233             fail("Failed to insert data" + e);
234             e.printStackTrace();
235          }
236       }
237
238       int period = (wakeupIntervalMillis + 500);
239       log("period is " + period);
240       TestingUtil.sleepThread(period);
241
242       for (int i = 0; i < 1000; i++)
243       {
244          String JavaDoc str = rootStr + i;
245          Fqn fqn = Fqn.fromString(str);
246          assertNull(cache.get(fqn, str));
247       }
248
249       for (int i = 1000; i < 5000; i++)
250       {
251          String JavaDoc str = rootStr + i;
252          Fqn fqn = Fqn.fromString(str);
253          assertNotNull(cache.get(fqn, str));
254       }
255
256       for (int i = 1000; i < 5000; i++)
257       {
258          if (i % 2 == 0)
259          {
260             String JavaDoc str = rootStr + i;
261             Fqn fqn = Fqn.fromString(str);
262             cache.remove(fqn);
263          }
264       }
265
266       TestingUtil.sleepThread(period);
267
268
269       for (int i = 1000; i < 5000; i++)
270       {
271          if (i % 2 == 0)
272          {
273             String JavaDoc str = rootStr + i;
274             Fqn fqn = Fqn.fromString(str);
275             assertNull(cache.get(fqn));
276          }
277       }
278    }
279
280
281    class MyPutter extends Thread JavaDoc
282    {
283
284       public MyPutter(String JavaDoc name)
285       {
286          super(name);
287       }
288
289       public void run()
290       {
291          int i = 0;
292          final String JavaDoc myName = ROOT_STR + "/test1/node" + getName();
293          while (isTrue)
294          {
295             try
296             {
297                cache.put(myName + i++, "value", i);
298                sleep(1);
299             }
300             catch (Throwable JavaDoc e)
301             {
302                e.printStackTrace();
303                if (t1_ex == null)
304                   t1_ex = e;
305             }
306          }
307       }
308    }
309
310
311    public void testConcurrentPutAndEvict() throws Exception JavaDoc
312    {
313       cache.stop();
314       cache.destroy();
315       cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
316       cache.start();
317       cache.put(ROOT_STR + "/concurrentPutAndEvict", "value", 1);
318
319       for (int i = 0; i < 10; i++)
320       {
321          new MyPutter("Putter" + i).start();
322       }
323
324       int counter = 0;
325       while (true)
326       {
327          counter++;
328          if (t1_ex != null)
329          {
330             fail("Exception generated in put() " + t1_ex);
331          }
332          log("nodes/locks: " + cache.getNumberOfNodes() + "/" + cache.getNumberOfLocksHeld());
333          TestingUtil.sleepThread(1000);
334          if (counter > 10)
335          { // run for 10 seconds
336
isTrue = false;
337             break;
338          }
339       }
340    }
341
342    void log(String JavaDoc msg)
343    {
344       System.out.println("-- " + msg);
345    }
346
347 }
348
Popular Tags