KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.cache.config.EvictionConfig;
10 import org.jboss.cache.config.EvictionRegionConfig;
11
12 import junit.framework.TestCase;
13
14 /**
15  * Unit tests for FIFOQueue.
16  *
17  * @author Daniel Huang (dhuang@jboss.org)
18  * @version $Revision: 1.4 $
19  */

20 public class FIFOQueueTest extends TestCase
21 {
22    private static final int CAPACITY = EvictionConfig.EVENT_QUEUE_SIZE_DEFAULT;
23    
24    private FIFOQueue queue;
25
26    public void setUp() throws Exception JavaDoc
27    {
28       super.setUp();
29       queue = new FIFOQueue();
30    }
31
32    public void tearDown() throws Exception JavaDoc
33    {
34       super.tearDown();
35    }
36
37    public void testQueue() throws Exception JavaDoc
38    {
39       for (int i = 0; i < 50000; i++)
40       {
41          NodeEntry ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
42          queue.addNodeEntry(ne);
43       }
44
45       assertEquals(queue.getFirstNodeEntry().getFqn().toString(), "/a/b/c/0");
46
47       assertEquals(50000, queue.getNumberOfNodes());
48       assertTrue(queue.containsNodeEntry(new NodeEntry("/a/b/c/250")));
49
50       NodeEntry ne27500 = queue.getNodeEntry("/a/b/c/27500");
51       assertEquals("/a/b/c/27500", ne27500.getFqn().toString());
52
53       // now make sure the ordering is correct.
54
int k = 0;
55       NodeEntry ne;
56       while ((ne = queue.getFirstNodeEntry()) != null)
57       {
58          assertEquals("/a/b/c/" + Integer.toString(k), ne.getFqn().toString());
59          queue.removeNodeEntry(ne);
60          k++;
61          if (k == 25000)
62          {
63             break;
64          }
65       }
66
67       assertEquals(queue.getFirstNodeEntry().getFqn().toString(), "/a/b/c/25000");
68
69       assertEquals(25000, queue.getNumberOfNodes());
70       k = 25000;
71       while ((ne = queue.getFirstNodeEntry()) != null)
72       {
73          assertEquals(ne.getFqn().toString(), "/a/b/c/" + Integer.toString(k));
74          queue.removeNodeEntry(ne);
75          k++;
76       }
77
78       assertEquals(0, queue.getNumberOfNodes());
79
80       assertFalse(queue.containsNodeEntry(new NodeEntry("/a/b/c/27500")));
81
82       assertNull(queue.getNodeEntry("/a/b/c/27500"));
83    }
84
85    public void testGetFirstNodeEntry() throws Exception JavaDoc
86    {
87       for (int i = 0; i < 50000; i++)
88       {
89          NodeEntry ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
90          queue.addNodeEntry(ne);
91       }
92
93       NodeEntry ne;
94       int count = 0;
95       while ((ne = queue.getFirstNodeEntry()) != null)
96       {
97          assertEquals("/a/b/c/" + Integer.toString(count), ne.getFqn().toString());
98          queue.removeNodeEntry(ne);
99          count++;
100       }
101    }
102
103    public void testLargeAddAndRemoval() throws Exception JavaDoc
104    {
105       long begin = System.currentTimeMillis();
106       for (int i = 0; i < CAPACITY; i++)
107       {
108          queue.addNodeEntry(new NodeEntry("/test/" + Integer.toString(i)));
109       }
110       System.out.println("Took " + (System.currentTimeMillis() - begin) + "ms to add " + CAPACITY + " entries to queue");
111
112       assertEquals(CAPACITY, queue.getNumberOfNodes());
113
114       begin = System.currentTimeMillis();
115       for (int i = CAPACITY - 1; i >= 0; i--)
116       {
117          // pop it backwards for worse case scenario if O(n) = n
118
queue.removeNodeEntry(new NodeEntry("/test/" + Integer.toString(i)));
119       }
120
121       System.out.println("Took " + (System.currentTimeMillis() - begin) + "ms to remove " + CAPACITY + " entries to queue");
122
123       assertEquals(0, queue.getNumberOfNodes());
124
125       begin = System.currentTimeMillis();
126       for (int i = 0; i < CAPACITY; i++)
127       {
128          queue.addNodeEntry(new NodeEntry("/test/" + Integer.toString(i)));
129       }
130       System.out.println("Took " + (System.currentTimeMillis() - begin) + "ms to readd " + CAPACITY + " entries to queue");
131
132       assertEquals(CAPACITY, queue.getNumberOfNodes());
133       NodeEntry ne;
134       begin = System.currentTimeMillis();
135
136       while ((ne = queue.getFirstNodeEntry()) != null)
137       {
138          queue.removeNodeEntry(ne);
139       }
140
141       System.out.println("Took " + (System.currentTimeMillis() - begin) + "ms to iterate via getFirstNodeEntry() and remove (pop from top of queue) " +
142                          CAPACITY + " entries from queue");
143
144       assertEquals(0, queue.getNumberOfNodes());
145    }
146
147    public void testNumElements() throws Exception JavaDoc
148    {
149       FIFOQueue queue = new FIFOQueue();
150
151       NodeEntry ne = new NodeEntry("/a/b/c");
152       ne.setNumberOfElements(50);
153       queue.addNodeEntry(ne);
154
155       assertEquals(50, queue.getNumberOfElements());
156       assertEquals(1, queue.getNumberOfNodes());
157
158       queue.removeNodeEntry(ne);
159       assertEquals(0, queue.getNumberOfElements());
160
161       for (int i = 0; i < 10; i++)
162       {
163          ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
164          ne.setNumberOfElements(i);
165          queue.addNodeEntry(ne);
166       }
167
168       assertEquals(45, queue.getNumberOfElements());
169       assertEquals(10, queue.getNumberOfNodes());
170
171       ne = queue.getNodeEntry("/a/b/c/0");
172       assertNotNull(ne);
173       assertEquals(0, ne.getNumberOfElements());
174       ne.setNumberOfElements(500);
175
176       assertEquals(545, queue.getNumberOfElements());
177       ne = queue.getNodeEntry("/a/b/c/0");
178       assertEquals(500, ne.getNumberOfElements());
179
180       queue.removeNodeEntry(ne);
181
182       assertEquals(45, queue.getNumberOfElements());
183       assertEquals(9, queue.getNumberOfNodes());
184       for (int i = 1; i < 10; i++)
185       {
186          ne = queue.getNodeEntry("/a/b/c/" + Integer.toString(i));
187          assertEquals(i, ne.getNumberOfElements());
188          queue.removeNodeEntry(ne);
189       }
190
191       assertEquals(0, queue.getNumberOfNodes());
192       assertEquals(0, queue.getNumberOfElements());
193
194       assertNull(queue.getNodeEntry("/a/b/c/0"));
195       assertNull(queue.getFirstNodeEntry());
196    }
197 }
198
Popular Tags