1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.loader; |
8 |
| |
9 |
| import junit.framework.Assert; |
10 |
| import junit.framework.Test; |
11 |
| import junit.framework.TestSuite; |
12 |
| import org.apache.commons.logging.Log; |
13 |
| import org.apache.commons.logging.LogFactory; |
14 |
| import org.jboss.cache.CacheImpl; |
15 |
| import org.jboss.cache.DefaultCacheFactory; |
16 |
| import org.jboss.cache.Fqn; |
17 |
| import org.jboss.cache.config.Configuration; |
18 |
| import org.jboss.cache.misc.TestingUtil; |
19 |
| |
20 |
| import javax.transaction.TransactionManager; |
21 |
| import java.io.File; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| public class CacheLoaderWithReplicationTest extends AbstractCacheLoaderTestBase |
29 |
| { |
30 |
| private CacheImpl cache1, cache2; |
31 |
| private String tmpLocation1 = System.getProperty("java.io.tmpdir", ".") + File.separator + "JBossCache-CacheLoaderWithReplicationTest1"; |
32 |
| private String tmpLocation2 = System.getProperty("java.io.tmpdir", ".") + File.separator + "JBossCache-CacheLoaderWithReplicationTest2"; |
33 |
| private File dir1 = new File(tmpLocation1); |
34 |
| private File dir2 = new File(tmpLocation2); |
35 |
| private Fqn fqn = Fqn.fromString("/a"); |
36 |
| private String key = "key"; |
37 |
| |
38 |
| private static final Log log = LogFactory.getLog(CacheLoaderWithReplicationTest.class); |
39 |
| |
40 |
| |
41 |
4
| public CacheLoaderWithReplicationTest()
|
42 |
| { |
43 |
4
| recursivedelete(dir1);
|
44 |
4
| recursivedelete(dir2);
|
45 |
| |
46 |
4
| if (!dir1.exists()) dir1.mkdirs();
|
47 |
4
| if (!dir2.exists()) dir2.mkdirs();
|
48 |
| |
49 |
4
| log.debug(" System props dump: " + System.getProperties());
|
50 |
4
| log.debug("Using location for CL 1 : " + tmpLocation1);
|
51 |
4
| log.debug("Using location for CL 2 : " + tmpLocation2);
|
52 |
| } |
53 |
| |
54 |
4
| public void setUp() throws Exception
|
55 |
| { |
56 |
4
| cache1 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
57 |
4
| cache1.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.bdbje.BdbjeCacheLoader", "location=" + tmpLocation1, false, true, false));
|
58 |
4
| cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
59 |
| |
60 |
4
| cache2 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
61 |
4
| cache2.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.bdbje.BdbjeCacheLoader", "location=" + tmpLocation2, false, true, false));
|
62 |
4
| cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
63 |
| } |
64 |
| |
65 |
8
| public void tearDown() throws Exception
|
66 |
| { |
67 |
8
| if (cache1 != null)
|
68 |
| { |
69 |
4
| try
|
70 |
| { |
71 |
4
| cache1.remove(fqn);
|
72 |
4
| cache1.getCacheLoaderManager().getCacheLoader().remove(fqn);
|
73 |
4
| cache1.stop();
|
74 |
| } |
75 |
| finally |
76 |
| { |
77 |
4
| cache1 = null;
|
78 |
| } |
79 |
| } |
80 |
| |
81 |
8
| if (cache2 != null)
|
82 |
| { |
83 |
4
| try
|
84 |
| { |
85 |
4
| cache2.remove(fqn);
|
86 |
4
| cache2.getCacheLoaderManager().getCacheLoader().remove(fqn);
|
87 |
4
| cache2.stop();
|
88 |
| } |
89 |
| finally |
90 |
| { |
91 |
4
| cache2 = null;
|
92 |
| } |
93 |
| |
94 |
| } |
95 |
8
| recursivedelete(dir1);
|
96 |
8
| recursivedelete(dir2);
|
97 |
| } |
98 |
| |
99 |
40
| private void recursivedelete(File f)
|
100 |
| { |
101 |
40
| if (f.isDirectory())
|
102 |
| { |
103 |
14
| File[] files = f.listFiles();
|
104 |
14
| for (int i = 0; i < files.length; i++)
|
105 |
| { |
106 |
16
| recursivedelete(files[i]);
|
107 |
| } |
108 |
| } |
109 |
| |
110 |
40
| f.delete();
|
111 |
| } |
112 |
| |
113 |
1
| public void testPessSyncRepl() throws Exception
|
114 |
| { |
115 |
1
| cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
116 |
1
| cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
117 |
| |
118 |
1
| cache1.start();
|
119 |
1
| cache2.start();
|
120 |
| |
121 |
1
| Assert.assertNull(cache1.get(fqn, key));
|
122 |
1
| Assert.assertNull(cache2.get(fqn, key));
|
123 |
| |
124 |
| |
125 |
1
| CacheLoader loader1 = cache1.getCacheLoaderManager().getCacheLoader();
|
126 |
1
| CacheLoader loader2 = cache2.getCacheLoaderManager().getCacheLoader();
|
127 |
| |
128 |
1
| TransactionManager mgr = cache1.getTransactionManager();
|
129 |
1
| mgr.begin();
|
130 |
1
| cache1.put(fqn, key, "value");
|
131 |
| |
132 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
133 |
1
| Assert.assertNull(cache2.get(fqn, key));
|
134 |
1
| Assert.assertNull(loader1.get(fqn));
|
135 |
1
| Assert.assertNull(loader2.get(fqn));
|
136 |
1
| mgr.commit();
|
137 |
| |
138 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
139 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
140 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
141 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
142 |
| |
143 |
1
| mgr.begin();
|
144 |
1
| cache1.put(fqn, key, "value2");
|
145 |
| |
146 |
1
| Assert.assertEquals("value2", cache1.get(fqn, key));
|
147 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
148 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
149 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
150 |
| |
151 |
1
| mgr.rollback();
|
152 |
| |
153 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
154 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
155 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
156 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
157 |
| |
158 |
| |
159 |
1
| tearDown();
|
160 |
| } |
161 |
| |
162 |
1
| public void testPessAsyncRepl() throws Exception
|
163 |
| { |
164 |
1
| cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_ASYNC);
|
165 |
1
| cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_ASYNC);
|
166 |
| |
167 |
1
| cache1.start();
|
168 |
1
| cache2.start();
|
169 |
| |
170 |
1
| CacheLoader loader1 = cache1.getCacheLoaderManager().getCacheLoader();
|
171 |
1
| CacheLoader loader2 = cache2.getCacheLoaderManager().getCacheLoader();
|
172 |
| |
173 |
| |
174 |
1
| Assert.assertNull(loader1.get(fqn));
|
175 |
1
| Assert.assertNull(loader2.get(fqn));
|
176 |
1
| Assert.assertNull(cache1.get(fqn));
|
177 |
1
| Assert.assertNull(cache2.get(fqn));
|
178 |
| |
179 |
1
| TransactionManager mgr = cache1.getTransactionManager();
|
180 |
1
| mgr.begin();
|
181 |
1
| cache1.put(fqn, key, "value");
|
182 |
| |
183 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
184 |
1
| Assert.assertNull(cache2.get(fqn, key));
|
185 |
1
| Assert.assertNull(loader1.get(fqn));
|
186 |
1
| Assert.assertNull(loader2.get(fqn));
|
187 |
1
| mgr.commit();
|
188 |
| |
189 |
1
| TestingUtil.sleepThread(500);
|
190 |
| |
191 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
192 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
193 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
194 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
195 |
| |
196 |
1
| mgr.begin();
|
197 |
1
| cache1.put(fqn, key, "value2");
|
198 |
| |
199 |
1
| Assert.assertEquals("value2", cache1.get(fqn, key));
|
200 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
201 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
202 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
203 |
| |
204 |
1
| mgr.rollback();
|
205 |
| |
206 |
1
| TestingUtil.sleepThread(500);
|
207 |
| |
208 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
209 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
210 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
211 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
212 |
| |
213 |
| |
214 |
1
| tearDown();
|
215 |
| } |
216 |
| |
217 |
1
| public void testOptSyncRepl() throws Exception
|
218 |
| { |
219 |
1
| try
|
220 |
| { |
221 |
1
| cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
222 |
1
| cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
223 |
| |
224 |
1
| cache1.getConfiguration().setNodeLockingScheme("OPTIMISTIC");
|
225 |
1
| cache2.getConfiguration().setNodeLockingScheme("OPTIMISTIC");
|
226 |
| |
227 |
1
| cache1.start();
|
228 |
1
| cache2.start();
|
229 |
| |
230 |
1
| CacheLoader loader1 = cache1.getCacheLoaderManager().getCacheLoader();
|
231 |
1
| CacheLoader loader2 = cache2.getCacheLoaderManager().getCacheLoader();
|
232 |
| |
233 |
1
| TransactionManager mgr = cache1.getTransactionManager();
|
234 |
1
| mgr.begin();
|
235 |
1
| cache1.put(fqn, key, "value");
|
236 |
| |
237 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
238 |
1
| Assert.assertNull(cache2.get(fqn, key));
|
239 |
1
| Assert.assertNull(loader1.get(fqn));
|
240 |
1
| Assert.assertNull(loader2.get(fqn));
|
241 |
1
| mgr.commit();
|
242 |
| |
243 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
244 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
245 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
246 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
247 |
| |
248 |
1
| mgr.begin();
|
249 |
1
| cache1.put(fqn, key, "value2");
|
250 |
| |
251 |
1
| Assert.assertEquals("value2", cache1.get(fqn, key));
|
252 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
253 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
254 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
255 |
| |
256 |
1
| mgr.rollback();
|
257 |
| |
258 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
259 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
260 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
261 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
262 |
| } |
263 |
| catch (Exception e) |
264 |
| { |
265 |
0
| Assert.assertTrue("Caught exception " + e.getMessage(), false);
|
266 |
0
| e.printStackTrace();
|
267 |
| } |
268 |
| |
269 |
| |
270 |
1
| tearDown();
|
271 |
| } |
272 |
| |
273 |
1
| public void testOptAsyncRepl() throws Exception
|
274 |
| { |
275 |
1
| cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_ASYNC);
|
276 |
1
| cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_ASYNC);
|
277 |
| |
278 |
1
| cache1.getConfiguration().setNodeLockingScheme("OPTIMISTIC");
|
279 |
1
| cache2.getConfiguration().setNodeLockingScheme("OPTIMISTIC");
|
280 |
| |
281 |
1
| cache1.start();
|
282 |
1
| cache2.start();
|
283 |
| |
284 |
1
| CacheLoader loader1 = cache1.getCacheLoaderManager().getCacheLoader();
|
285 |
1
| CacheLoader loader2 = cache2.getCacheLoaderManager().getCacheLoader();
|
286 |
| |
287 |
1
| TransactionManager mgr = cache1.getTransactionManager();
|
288 |
1
| mgr.begin();
|
289 |
1
| cache1.put(fqn, key, "value");
|
290 |
| |
291 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
292 |
1
| Assert.assertNull(cache2.get(fqn, key));
|
293 |
1
| Assert.assertNull(loader1.get(fqn));
|
294 |
1
| Assert.assertNull(loader2.get(fqn));
|
295 |
1
| mgr.commit();
|
296 |
| |
297 |
1
| TestingUtil.sleepThread(500);
|
298 |
| |
299 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
300 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
301 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
302 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
303 |
| |
304 |
1
| mgr.begin();
|
305 |
1
| cache1.put(fqn, key, "value2");
|
306 |
| |
307 |
1
| Assert.assertEquals("value2", cache1.get(fqn, key));
|
308 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
309 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
310 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
311 |
| |
312 |
1
| mgr.rollback();
|
313 |
1
| TestingUtil.sleepThread(500);
|
314 |
| |
315 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
316 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
317 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
318 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
319 |
| |
320 |
| |
321 |
1
| tearDown();
|
322 |
| } |
323 |
| |
324 |
1
| public static Test suite()
|
325 |
| { |
326 |
1
| return new TestSuite(CacheLoaderWithReplicationTest.class);
|
327 |
| } |
328 |
| |
329 |
0
| public static void main(String[] args)
|
330 |
| { |
331 |
0
| junit.textui.TestRunner.run(suite());
|
332 |
| } |
333 |
| |
334 |
| } |
335 |
| |
336 |
| |