KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.tests.eviction;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.eviction.EvictionException;
8 import org.jboss.cache.eviction.LRUAlgorithm;
9 import org.jboss.cache.eviction.Region;
10 import org.jboss.cache.eviction.RegionManager;
11
12 /**
13  * @author Ben Wang, Feb 11, 2004
14  */

15 public class LRUAlgorithmTest extends TestCase
16 {
17    RegionManager regionManager_;
18    LRUAlgorithm algo_;
19
20    public LRUAlgorithmTest(String JavaDoc s)
21    {
22       super(s);
23    }
24
25    public void setUp() throws Exception JavaDoc
26    {
27       super.setUp();
28       algo_ = new LRUAlgorithm();
29       DummyEvictionPolicy policy = new DummyEvictionPolicy();
30       regionManager_ = new RegionManager(policy);
31       regionManager_.createRegion("/a/b", algo_);
32       /*
33       try {
34          Thread.sleep(10000);
35       } catch (InterruptedException e) {
36          e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
37       }
38       */

39    }
40
41    public void tearDown() throws Exception JavaDoc
42    {
43       super.tearDown();
44    }
45
46    /**
47     * maxNodes = 1. Eception is evictFromCacheNode. Should be commented for now.
48     */

49    public void XtestEvictException() {
50       Fqn fqn1 = Fqn.fromString("/a/b/c");
51       Fqn fqn2 = Fqn.fromString("/a/b/d");
52       Fqn fqn3 = Fqn.fromString("/a/b/e");
53       Region region = regionManager_.getRegion("/a/b");
54       region.setMaxNodes(1);
55       region.setAddedNode(fqn1);
56       region.setAddedNode(fqn2);
57
58       try {
59          algo_.process(region);
60       } catch (EvictionException e) {
61          fail("testMaxNode: process failed " +e);
62          e.printStackTrace();
63       }
64       assertEquals("Queue size should be ", 1, algo_.evictionQueueSize());
65
66       region.setAddedNode(fqn2);
67       region.setAddedNode(fqn3);
68
69       try {
70          algo_.process(region);
71       } catch (EvictionException e) {
72          fail("testMaxNode: process failed " +e);
73          e.printStackTrace();
74       }
75       assertEquals("Queue size should be ", 1, algo_.evictionQueueSize());
76    }
77
78
79    /**
80     * maxNodes = 0 case
81     */

82    public void testMaxNode1() {
83       Fqn fqn1 = Fqn.fromString("/a/b/c");
84       Fqn fqn2 = Fqn.fromString("/a/b/d");
85       Fqn fqn3 = Fqn.fromString("/a/b/e");
86       Region region = regionManager_.getRegion("/a/b");
87       region.setMaxNodes(0);
88       region.setAddedNode(fqn1);
89       region.setAddedNode(fqn2);
90
91       try {
92          algo_.process(region);
93       } catch (EvictionException e) {
94          fail("testMaxNode: process failed " +e);
95          e.printStackTrace();
96       }
97       assertEquals("Queue size should be ", 2, algo_.evictionQueueSize());
98
99    }
100
101    /**
102     * maxNodes = 1
103     */

104    public void testMaxNode2() {
105       Fqn fqn1 = Fqn.fromString("/a/b/c");
106       Fqn fqn2 = Fqn.fromString("/a/b/d");
107       Fqn fqn3 = Fqn.fromString("/a/b/e");
108       Region region = regionManager_.getRegion("/a/b");
109       region.setMaxNodes(1);
110       region.setAddedNode(fqn1);
111       region.setAddedNode(fqn2);
112
113       try {
114          algo_.process(region);
115       } catch (EvictionException e) {
116          fail("testMaxNode: process failed " +e);
117          e.printStackTrace();
118       }
119       assertEquals("Queue size should be ", 1, algo_.evictionQueueSize());
120
121       region.setAddedNode(fqn2);
122       region.setAddedNode(fqn3);
123
124       try {
125          algo_.process(region);
126       } catch (EvictionException e) {
127          fail("testMaxNode: process failed " +e);
128          e.printStackTrace();
129       }
130       assertEquals("Queue size should be ", 1, algo_.evictionQueueSize());
131    }
132
133    /**
134    * TimeToIdleSeconds = 0
135    */

136    public void testIdleTimeSeconds1() {
137       Fqn fqn1 = Fqn.fromString("/a/b/c");
138       Fqn fqn2 = Fqn.fromString("/a/b/d");
139       Fqn fqn3 = Fqn.fromString("/a/b/e");
140       Region region = regionManager_.getRegion("/a/b");
141       region.setMaxNodes(0);
142       region.setTimeToLiveSeconds(0);
143       region.setAddedNode(fqn1);
144       region.setAddedNode(fqn2);
145       _sleep(2000);
146       try {
147          algo_.process(region);
148       } catch (EvictionException e) {
149          fail("testMaxNode: process failed " +e);
150          e.printStackTrace();
151       }
152       assertEquals("Queue size should be ", 2, algo_.evictionQueueSize());
153
154    }
155
156    /**
157    * TimeToIdleSeconds = 1
158    */

159    public void testIdleTimeSeconds2() {
160       Fqn fqn1 = Fqn.fromString("/a/b/c");
161       Fqn fqn2 = Fqn.fromString("/a/b/d");
162       Fqn fqn3 = Fqn.fromString("/a/b/e");
163       Region region = regionManager_.getRegion("/a/b");
164       region.setMaxNodes(0);
165       region.setTimeToLiveSeconds(1);
166       region.setAddedNode(fqn1);
167       region.setAddedNode(fqn2);
168       region.setAddedNode(fqn3);
169       try {
170          algo_.process(region);
171       } catch (EvictionException e) {
172          fail("testMaxNode: process failed " +e);
173          e.printStackTrace();
174       }
175       assertEquals("Queue size #1: ", 3, algo_.evictionQueueSize());
176       _sleep(2000);
177       try {
178          algo_.process(region);
179       } catch (EvictionException e) {
180          fail("testMaxNode: process failed " +e);
181          e.printStackTrace();
182       }
183       assertEquals("Queue size #2: ", 0, algo_.evictionQueueSize());
184    }
185
186    /**
187    * TimeToIdleSeconds = 1 with node visited in between.
188    */

189    public void testIdleTimeSeconds3() {
190       Fqn fqn1 = Fqn.fromString("/a/b/c");
191       Fqn fqn2 = Fqn.fromString("/a/b/d");
192       Fqn fqn3 = Fqn.fromString("/a/b/e");
193       Region region = regionManager_.getRegion("/a/b");
194       region.setMaxNodes(0);
195       region.setTimeToLiveSeconds(1);
196       region.setAddedNode(fqn1);
197       region.setAddedNode(fqn2);
198       region.setAddedNode(fqn3);
199       try {
200          algo_.process(region);
201       } catch (EvictionException e) {
202          fail("testMaxNode: process failed " +e);
203          e.printStackTrace();
204       }
205       assertEquals("Queue size #1: ", 3, algo_.evictionQueueSize());
206       _sleep(2000);
207       region.setVisitedNode(fqn2);
208       try {
209          algo_.process(region);
210       } catch (EvictionException e) {
211          fail("testMaxNode: process failed " +e);
212          e.printStackTrace();
213       }
214       assertEquals("Queue size #2: ", 1, algo_.evictionQueueSize());
215    }
216
217
218    /**
219     * Generic combo case.
220     */

221    public void testCombo1() {
222       Fqn fqn1 = Fqn.fromString("/a/b/c");
223       Fqn fqn2 = Fqn.fromString("/a/b/d");
224       Fqn fqn3 = Fqn.fromString("/a/b/e");
225       Region region = regionManager_.getRegion("/a/b");
226       region.setMaxNodes(2);
227       region.setTimeToLiveSeconds(1);
228       region.setAddedNode(fqn1);
229       region.setAddedNode(fqn2);
230       try {
231          algo_.process(region);
232       } catch (EvictionException e) {
233          fail("testMaxNode: process failed " +e);
234          e.printStackTrace();
235       }
236       assertEquals("Queue size #1: ", 2, algo_.evictionQueueSize());
237       region.setAddedNode(fqn3);
238       _sleep(2000);
239       try {
240          algo_.process(region);
241       } catch (EvictionException e) {
242          fail("testMaxNode: process failed " +e);
243          e.printStackTrace();
244       }
245       assertEquals("Queue size #2: ", 1, algo_.evictionQueueSize());
246    }
247
248    /**
249     * Generic combo case with newly added node should be around.
250     */

251    public void testCombo2() {
252       Fqn fqn1 = Fqn.fromString("/a/b/c");
253       Fqn fqn2 = Fqn.fromString("/a/b/d");
254       Fqn fqn3 = Fqn.fromString("/a/b/e");
255       Region region = regionManager_.getRegion("/a/b");
256       region.setMaxNodes(2);
257       region.setTimeToLiveSeconds(1);
258       region.setAddedNode(fqn1);
259       region.setAddedNode(fqn2);
260       region.setRemovedNode(fqn2);
261       try {
262          algo_.process(region);
263       } catch (EvictionException e) {
264          fail("testMaxNode: process failed " +e);
265          e.printStackTrace();
266       }
267       assertEquals("Queue size #1: ", 1, algo_.evictionQueueSize());
268       region.setAddedNode(fqn3);
269       _sleep(2000);
270       try {
271          algo_.process(region);
272       } catch (EvictionException e) {
273          fail("testMaxNode: process failed " +e);
274          e.printStackTrace();
275       }
276       assertEquals("Queue size #2: ", 1, algo_.evictionQueueSize());
277    }
278
279    void _sleep(long msecs) {
280       try {
281          Thread.sleep(msecs);
282       } catch (InterruptedException JavaDoc e) {
283          e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
284
}
285    }
286
287    void log(String JavaDoc msg)
288    {
289       System.out.println("-- " + msg);
290    }
291
292    public static Test suite()
293    {
294       return new TestSuite(LRUAlgorithmTest.class);
295    }
296
297    public static void main(String JavaDoc[] args)
298    {
299       junit.textui.TestRunner.run(suite());
300    }
301
302 }
303
Popular Tags