KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.eviction;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.jboss.cache.Fqn;
10 import org.jboss.cache.RegionManager;
11 import org.jboss.cache.Region;
12 import org.jboss.cache.misc.TestingUtil;
13
14 /**
15  * Unit tests for LRUAlgorithm.
16  *
17  * @author Ben Wang, Feb 11, 2004
18  * @author Daniel Huang (dhuang@jboss.org)
19  */

20 public class LRUAlgorithmTest extends TestCase
21 {
22    RegionManager regionManager_;
23    LRUAlgorithm algo_;
24    LRUConfiguration config_;
25    Log log = LogFactory.getLog(LRUAlgorithm.class);
26
27    public LRUAlgorithmTest(String JavaDoc s)
28    {
29       super(s);
30    }
31
32    public void setUp() throws Exception JavaDoc
33    {
34       super.setUp();
35       log.debug("");
36       log.debug("Test case " + getName());
37       log.debug("");
38
39       algo_ = new LRUAlgorithm();
40       config_ = new LRUConfiguration();
41       config_.setEvictionPolicyClass(DummyEvictionPolicy.class.getName());
42       // We have to configure timeToLiveSeconds!!
43
config_.setTimeToLiveSeconds(0);
44       
45       regionManager_ = new RegionManager();
46       regionManager_.getRegion("/a/b", true).setEvictionPolicy(config_);
47       /*
48       try {
49          Thread.sleep(10000);
50       } catch (InterruptedException e) {
51          e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
52       }
53       */

54    }
55
56    public void tearDown() throws Exception JavaDoc
57    {
58       super.tearDown();
59    }
60
61    /**
62     * maxNodes = 1. Eception is evictFromCacheNode. Should be commented for now.
63     */

64    public void XtestEvictException()
65    {
66       Fqn fqn1 = Fqn.fromString("/a/b/c");
67       Fqn fqn2 = Fqn.fromString("/a/b/d");
68       Fqn fqn3 = Fqn.fromString("/a/b/e");
69       Region region = regionManager_.getRegion("/a/b", true);
70       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
71       config.setMaxNodes(1);
72       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
73       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
74
75       try
76       {
77          algo_.process(region);
78       }
79       catch (EvictionException e)
80       {
81          fail("testMaxNode: process failed " + e);
82          e.printStackTrace();
83       }
84       assertEquals("Queue size should be ", 1, algo_.getEvictionQueue().getNumberOfNodes());
85
86       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
87       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.ADD_NODE_EVENT));
88
89       try
90       {
91          algo_.process(region);
92       }
93       catch (EvictionException e)
94       {
95          fail("testMaxNode: process failed " + e);
96          e.printStackTrace();
97       }
98       assertEquals("Queue size should be ", 1, algo_.getEvictionQueue().getNumberOfNodes());
99    }
100
101
102    /**
103     * maxNodes = 0 case
104     */

105    public void testMaxNode1()
106    {
107       Fqn fqn1 = Fqn.fromString("/a/b/c");
108       Fqn fqn2 = Fqn.fromString("/a/b/d");
109       Region region = regionManager_.getRegion("/a/b", true);
110       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
111       config.setMaxNodes(0);
112       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
113       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
114
115       try
116       {
117          algo_.process(region);
118       }
119       catch (EvictionException e)
120       {
121          fail("testMaxNode: process failed " + e);
122          e.printStackTrace();
123       }
124       assertEquals("Queue size should be ", 2, algo_.getEvictionQueue().getNumberOfNodes());
125
126    }
127
128    /**
129     * maxNodes = 1
130     */

131    public void testMaxNode2()
132    {
133       Fqn fqn1 = Fqn.fromString("/a/b/c");
134       Fqn fqn2 = Fqn.fromString("/a/b/d");
135       Fqn fqn3 = Fqn.fromString("/a/b/e");
136       Region region = regionManager_.getRegion("/a/b", true);
137       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
138       config.setMaxNodes(1);
139       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
140       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
141
142       try
143       {
144          algo_.process(region);
145       }
146       catch (EvictionException e)
147       {
148          fail("testMaxNode: process failed " + e);
149          e.printStackTrace();
150       }
151       assertEquals("Queue size should be ", 1, algo_.getEvictionQueue().getNumberOfNodes());
152
153       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
154       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.ADD_NODE_EVENT));
155
156       try
157       {
158          algo_.process(region);
159       }
160       catch (EvictionException e)
161       {
162          fail("testMaxNode: process failed " + e);
163          e.printStackTrace();
164       }
165       assertEquals("Queue size should be ", 1, algo_.getEvictionQueue().getNumberOfNodes());
166    }
167
168    /**
169     * TimeToIdleSeconds = 0
170     */

171    public void testIdleTimeSeconds1()
172    {
173       Fqn fqn1 = Fqn.fromString("/a/b/c");
174       Fqn fqn2 = Fqn.fromString("/a/b/d");
175       Region region = regionManager_.getRegion("/a/b", true);
176       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
177       config.setMaxNodes(0);
178       config.setTimeToLiveSeconds(0);
179       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
180       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
181       TestingUtil.sleepThread(2000);
182       try
183       {
184          algo_.process(region);
185       }
186       catch (EvictionException e)
187       {
188          fail("testMaxNode: process failed " + e);
189          e.printStackTrace();
190       }
191       assertEquals("Queue size should be ", 2, algo_.getEvictionQueue().getNumberOfNodes());
192
193    }
194
195    /**
196     * TimeToIdleSeconds = 1
197     */

198    public void testIdleTimeSeconds2()
199    {
200       Fqn fqn1 = Fqn.fromString("/a/b/c");
201       Fqn fqn2 = Fqn.fromString("/a/b/d");
202       Fqn fqn3 = Fqn.fromString("/a/b/e");
203       Region region = regionManager_.getRegion("/a/b", true);
204       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
205       config.setMaxNodes(0);
206       config.setTimeToLiveSeconds(1);
207       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
208       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
209       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.ADD_NODE_EVENT));
210
211       try
212       {
213          algo_.process(region);
214       }
215       catch (EvictionException e)
216       {
217          fail("testMaxNode: process failed " + e);
218          e.printStackTrace();
219       }
220       assertEquals("Queue size #1: ", 3, algo_.getEvictionQueue().getNumberOfNodes());
221       TestingUtil.sleepThread(2000);
222       try
223       {
224          algo_.process(region);
225       }
226       catch (EvictionException e)
227       {
228          fail("testMaxNode: process failed " + e);
229          e.printStackTrace();
230       }
231       assertEquals("Queue size #2: ", 0, algo_.getEvictionQueue().getNumberOfNodes());
232    }
233
234    /**
235     * TimeToIdleSeconds = 1 with node visited in between.
236     */

237    public void testIdleTimeSeconds3()
238    {
239       Fqn fqn1 = Fqn.fromString("/a/b/c");
240       Fqn fqn2 = Fqn.fromString("/a/b/d");
241       Fqn fqn3 = Fqn.fromString("/a/b/e");
242       Region region = regionManager_.getRegion("/a/b", true);
243       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
244       config.setMaxNodes(0);
245       config.setTimeToLiveSeconds(1);
246       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
247       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
248       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.ADD_NODE_EVENT));
249
250       try
251       {
252          algo_.process(region);
253       }
254       catch (EvictionException e)
255       {
256          fail("testMaxNode: process failed " + e);
257          e.printStackTrace();
258       }
259       assertEquals("Queue size #1: ", 3, algo_.getEvictionQueue().getNumberOfNodes());
260       TestingUtil.sleepThread(2000);
261       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.VISIT_NODE_EVENT));
262       try
263       {
264          algo_.process(region);
265       }
266       catch (EvictionException e)
267       {
268          fail("testMaxNode: process failed " + e);
269          e.printStackTrace();
270       }
271       assertEquals("Queue size #2: ", 1, algo_.getEvictionQueue().getNumberOfNodes());
272    }
273
274    /**
275     * MaxAgeSeconds = 1 with 3 nodes.
276     *
277     * @throws Exception
278     */

279    public void testMaxAgeSeconds1() throws Exception JavaDoc
280    {
281       Fqn fqn1 = Fqn.fromString("/a/b/c");
282       Fqn fqn2 = Fqn.fromString("/a/b/d");
283       Fqn fqn3 = Fqn.fromString("/a/b/e");
284       Region region = regionManager_.getRegion("/a/b", true);
285       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
286       config.setMaxNodes(0);
287       config.setTimeToLiveSeconds(0);
288       config.setMaxAgeSeconds(1);
289       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
290       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
291       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.ADD_NODE_EVENT));
292
293       algo_.process(region);
294       assertEquals("Queue size #1: ", 3, algo_.getEvictionQueue().getNumberOfNodes());
295       TestingUtil.sleepThread(2000);
296       algo_.process(region);
297       assertEquals("Queue size #2: ", 0, algo_.getEvictionQueue().getNumberOfNodes());
298    }
299
300    /**
301     * MaxAgeSeconds = 2 with 3 nodes.
302     *
303     * @throws Exception
304     */

305    public void testMaxAgeSeconds2() throws Exception JavaDoc
306    {
307       Fqn fqn1 = Fqn.fromString("/a/b/c");
308       Fqn fqn2 = Fqn.fromString("/a/b/d");
309       Fqn fqn3 = Fqn.fromString("/a/b/e");
310       Region region = regionManager_.getRegion("/a/b", true);
311       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
312
313       config.setMaxNodes(0);
314       config.setTimeToLiveSeconds(0);
315       config.setMaxAgeSeconds(2);
316       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
317       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
318       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.ADD_NODE_EVENT));
319
320       algo_.process(region);
321       assertEquals("Queue size #1: ", 3, algo_.getEvictionQueue().getNumberOfNodes());
322       TestingUtil.sleepThread(1000);
323       algo_.process(region);
324       assertEquals("Queue size #2: ", 3, algo_.getEvictionQueue().getNumberOfNodes());
325       TestingUtil.sleepThread(1500);
326       algo_.process(region);
327       assertEquals("Queue size #3: ", 0, algo_.getEvictionQueue().getNumberOfNodes());
328    }
329
330    /**
331     * Generic combo case.
332     */

333    public void testCombo1()
334    {
335       Fqn fqn1 = Fqn.fromString("/a/b/c");
336       Fqn fqn2 = Fqn.fromString("/a/b/d");
337       Fqn fqn3 = Fqn.fromString("/a/b/e");
338       Fqn fqn4 = Fqn.fromString("/a/b/f");
339       Region region = regionManager_.getRegion("/a/b", true);
340       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
341
342       config.setMaxNodes(2);
343       config.setTimeToLiveSeconds(1);
344       config.setMaxAgeSeconds(3);
345       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
346       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
347       region.putNodeEvent(new EvictedEventNode(fqn4, NodeEventType.ADD_NODE_EVENT));
348
349       try
350       {
351          algo_.process(region);
352       }
353       catch (EvictionException e)
354       {
355          fail("testMaxNode: process failed " + e);
356          e.printStackTrace();
357       }
358       assertEquals("Queue size #1: ", 2, algo_.getEvictionQueue().getNumberOfNodes());
359       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.ADD_NODE_EVENT));
360
361       TestingUtil.sleepThread(2000);
362       try
363       {
364          algo_.process(region);
365       }
366       catch (EvictionException e)
367       {
368          fail("testMaxNode: process failed " + e);
369          e.printStackTrace();
370       }
371       assertEquals("Queue size #2: ", 1, algo_.getEvictionQueue().getNumberOfNodes());
372
373       TestingUtil.sleepThread(3500);
374       // visit the node now to prevent the idle time from doing the pruning - node still gets pruned but by
375
// max age.
376
region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.VISIT_NODE_EVENT));
377       try
378       {
379          algo_.process(region);
380       }
381       catch (EvictionException e)
382       {
383          fail("testMaxAge: process failed " + e);
384          e.printStackTrace();
385       }
386       assertEquals("Queue size #3: ", 0, algo_.getEvictionQueue().getNumberOfNodes());
387    }
388
389    /**
390     * Generic combo case with newly added node should be around.
391     */

392    public void testCombo2()
393    {
394       Fqn fqn1 = Fqn.fromString("/a/b/c");
395       Fqn fqn2 = Fqn.fromString("/a/b/d");
396       Fqn fqn3 = Fqn.fromString("/a/b/e");
397       Region region = regionManager_.getRegion("/a/b", true);
398       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
399
400       config.setMaxNodes(2);
401       config.setTimeToLiveSeconds(1);
402       config.setMaxAgeSeconds(3);
403       region.putNodeEvent(new EvictedEventNode(fqn1, NodeEventType.ADD_NODE_EVENT));
404       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.ADD_NODE_EVENT));
405       region.putNodeEvent(new EvictedEventNode(fqn2, NodeEventType.REMOVE_NODE_EVENT));
406       try
407       {
408          algo_.process(region);
409       }
410       catch (EvictionException e)
411       {
412          fail("testMaxNode: process failed " + e);
413          e.printStackTrace();
414       }
415       assertEquals("Queue size #1: ", 1, algo_.getEvictionQueue().getNumberOfNodes());
416       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.ADD_NODE_EVENT));
417
418       TestingUtil.sleepThread(2000);
419       try
420       {
421          algo_.process(region);
422       }
423       catch (EvictionException e)
424       {
425          fail("testMaxNode: process failed " + e);
426          e.printStackTrace();
427       }
428       assertEquals("Queue size #2: ", 1, algo_.getEvictionQueue().getNumberOfNodes());
429
430       TestingUtil.sleepThread(3000);
431       region.putNodeEvent(new EvictedEventNode(fqn3, NodeEventType.VISIT_NODE_EVENT));
432       try
433       {
434          algo_.process(region);
435       }
436       catch (EvictionException e)
437       {
438          fail("testCombo: process failed " + e);
439          e.printStackTrace();
440       }
441
442       TestingUtil.sleepThread(1000);
443       assertEquals("Queue size #3: ", 0, algo_.getEvictionQueue().getNumberOfNodes());
444    }
445
446    public void testEvictionSortOrder() throws Exception JavaDoc
447    {
448       Region region = regionManager_.getRegion("/a/b", true);
449       LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig();
450
451       config.setMaxAgeSeconds(1000);
452       config.setMaxNodes(0);
453       config.setTimeToLiveSeconds(1000);
454
455       for (int i = 0; i < 100; i++)
456       {
457          Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
458          region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
459       }
460
461       algo_.process(region);
462
463       TestingUtil.sleepThread(5000);
464
465       for (int i = 0; i < 100; i++)
466       {
467          Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
468          if (i % 2 == 0)
469          {
470             region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT));
471          }
472       }
473
474       algo_.process(region);
475
476       LRUQueue queue = (LRUQueue) algo_.getEvictionQueue();
477
478       NodeEntry ne;
479       int count = 0;
480       while ((ne = queue.getFirstLRUNodeEntry()) != null)
481       {
482          if (count < 50)
483          {
484             assertEquals(1, ne.getNumberOfNodeVisits());
485          }
486          else
487          {
488             assertEquals(2, ne.getNumberOfNodeVisits());
489          }
490          queue.removeNodeEntry(ne);
491          count++;
492       }
493
494       for (int i = 0; i < 100; i++)
495       {
496          Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
497          region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
498       }
499
500       algo_.process(region);
501       long lastCreateTimestamp = 0;
502       while ((ne = queue.getFirstMaxAgeNodeEntry()) != null)
503       {
504          assertTrue(ne.getCreationTimeStamp() >= lastCreateTimestamp);
505          lastCreateTimestamp = ne.getCreationTimeStamp();
506          queue.removeNodeEntry(ne);
507       }
508    }
509
510    void log(String JavaDoc msg)
511    {
512       System.out.println("-- " + msg);
513    }
514
515    public static Test suite()
516    {
517       return new TestSuite(LRUAlgorithmTest.class);
518    }
519
520    public static void main(String JavaDoc[] args)
521    {
522       junit.textui.TestRunner.run(suite());
523    }
524
525 }
526
Popular Tags