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