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.Cache; |
15 |
| import org.jboss.cache.CacheSPI; |
16 |
| import org.jboss.cache.DefaultCacheFactory; |
17 |
| import org.jboss.cache.Fqn; |
18 |
| import org.jboss.cache.config.Configuration; |
19 |
| import org.jboss.cache.misc.TestingUtil; |
20 |
| |
21 |
| import javax.transaction.TransactionManager; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| public class CacheLoaderWithReplicationTest extends AbstractCacheLoaderTestBase |
29 |
| { |
30 |
| private Cache cache1, cache2; |
31 |
| private Fqn fqn = Fqn.fromString("/a"); |
32 |
| private String key = "key"; |
33 |
| |
34 |
| private static final Log log = LogFactory.getLog(CacheLoaderWithReplicationTest.class); |
35 |
| private CacheLoader loader1, loader2; |
36 |
| private TransactionManager mgr1, mgr2; |
37 |
| |
38 |
| |
39 |
4
| public void setUp() throws Exception
|
40 |
| { |
41 |
4
| cache1 = DefaultCacheFactory.getInstance().createCache(false);
|
42 |
4
| cache1.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.DummyInMemoryCacheLoader", null, false, true, false));
|
43 |
4
| cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
44 |
| |
45 |
4
| cache2 = DefaultCacheFactory.getInstance().createCache(false);
|
46 |
4
| cache2.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.DummyInMemoryCacheLoader", null, false, true, false));
|
47 |
4
| cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
48 |
| |
49 |
4
| loader1 = loader2 = null;
|
50 |
4
| mgr1 = mgr2 = null;
|
51 |
| } |
52 |
| |
53 |
4
| public void tearDown() throws Exception
|
54 |
| { |
55 |
4
| if (cache1 != null)
|
56 |
| { |
57 |
4
| try
|
58 |
| { |
59 |
4
| try
|
60 |
| { |
61 |
4
| if (mgr1 != null) mgr1.rollback();
|
62 |
| } |
63 |
| catch (Exception e) |
64 |
| { |
65 |
| |
66 |
| } |
67 |
| |
68 |
4
| loader1.remove(fqn);
|
69 |
4
| cache1.removeNode(fqn);
|
70 |
4
| cache1.stop();
|
71 |
4
| cache1.destroy();
|
72 |
| } |
73 |
| finally |
74 |
| { |
75 |
4
| cache1 = null;
|
76 |
4
| mgr1 = null;
|
77 |
4
| loader1 = null;
|
78 |
| } |
79 |
| } |
80 |
| |
81 |
4
| if (cache2 != null)
|
82 |
| { |
83 |
4
| try
|
84 |
| { |
85 |
4
| try
|
86 |
| { |
87 |
4
| if (mgr2 != null) mgr2.rollback();
|
88 |
| } |
89 |
| catch (Exception e) |
90 |
| { |
91 |
| |
92 |
| } |
93 |
4
| loader2.remove(fqn);
|
94 |
4
| cache2.removeNode(fqn);
|
95 |
4
| cache2.stop();
|
96 |
4
| cache2.destroy();
|
97 |
| } |
98 |
| finally |
99 |
| { |
100 |
4
| cache2 = null;
|
101 |
4
| mgr2 = null;
|
102 |
4
| loader2 = null;
|
103 |
| } |
104 |
| |
105 |
| } |
106 |
| } |
107 |
| |
108 |
4
| private void createCaches(boolean sync, boolean optimistic) throws Exception
|
109 |
| { |
110 |
4
| cache1.getConfiguration().setCacheMode(sync ? Configuration.CacheMode.REPL_SYNC : Configuration.CacheMode.REPL_ASYNC);
|
111 |
4
| cache2.getConfiguration().setCacheMode(sync ? Configuration.CacheMode.REPL_SYNC : Configuration.CacheMode.REPL_ASYNC);
|
112 |
4
| if (sync)
|
113 |
| { |
114 |
2
| cache1.getConfiguration().setSyncCommitPhase(true);
|
115 |
2
| cache2.getConfiguration().setSyncCommitPhase(true);
|
116 |
2
| cache1.getConfiguration().setSyncRollbackPhase(true);
|
117 |
2
| cache2.getConfiguration().setSyncRollbackPhase(true);
|
118 |
| } |
119 |
| |
120 |
4
| if (optimistic)
|
121 |
| { |
122 |
2
| cache1.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
123 |
2
| cache2.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
124 |
| } |
125 |
| |
126 |
4
| cache1.start();
|
127 |
4
| cache2.start();
|
128 |
| |
129 |
4
| TestingUtil.blockUntilViewsReceived(10000, cache1, cache2);
|
130 |
| |
131 |
4
| loader1 = ((CacheSPI) cache1).getCacheLoaderManager().getCacheLoader();
|
132 |
4
| loader2 = ((CacheSPI) cache2).getCacheLoaderManager().getCacheLoader();
|
133 |
| |
134 |
| |
135 |
4
| Assert.assertNull(loader1.get(fqn));
|
136 |
4
| Assert.assertNull(loader2.get(fqn));
|
137 |
4
| Assert.assertNull(cache1.getRoot().getChild(fqn));
|
138 |
4
| Assert.assertNull(cache2.getRoot().getChild(fqn));
|
139 |
| |
140 |
4
| mgr1 = cache1.getConfiguration().getRuntimeConfig().getTransactionManager();
|
141 |
4
| mgr2 = cache2.getConfiguration().getRuntimeConfig().getTransactionManager();
|
142 |
| } |
143 |
| |
144 |
1
| public void testPessSyncRepl() throws Exception
|
145 |
| { |
146 |
1
| createCaches(true, false);
|
147 |
| |
148 |
1
| mgr1.begin();
|
149 |
1
| cache1.put(fqn, key, "value");
|
150 |
| |
151 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
152 |
1
| Assert.assertNull(cache2.get(fqn, key));
|
153 |
1
| Assert.assertNull(loader1.get(fqn));
|
154 |
1
| Assert.assertNull(loader2.get(fqn));
|
155 |
1
| mgr1.commit();
|
156 |
| |
157 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
158 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
159 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
160 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
161 |
| |
162 |
1
| mgr1.begin();
|
163 |
1
| cache1.put(fqn, key, "value2");
|
164 |
| |
165 |
1
| Assert.assertEquals("value2", cache1.get(fqn, key));
|
166 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
167 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
168 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
169 |
| |
170 |
1
| mgr1.rollback();
|
171 |
| |
172 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
173 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
174 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
175 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
176 |
| |
177 |
| } |
178 |
| |
179 |
1
| public void testPessAsyncRepl() throws Exception
|
180 |
| { |
181 |
1
| createCaches(false, false);
|
182 |
| |
183 |
1
| mgr1.begin();
|
184 |
1
| cache1.put(fqn, key, "value");
|
185 |
| |
186 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
187 |
1
| Assert.assertNull(cache2.get(fqn, key));
|
188 |
1
| Assert.assertNull(loader1.get(fqn));
|
189 |
1
| Assert.assertNull(loader2.get(fqn));
|
190 |
1
| mgr1.commit();
|
191 |
| |
192 |
1
| TestingUtil.sleepThread(500);
|
193 |
| |
194 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
195 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
196 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
197 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
198 |
| |
199 |
1
| mgr1.begin();
|
200 |
1
| cache1.put(fqn, key, "value2");
|
201 |
| |
202 |
1
| Assert.assertEquals("value2", cache1.get(fqn, key));
|
203 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
204 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
205 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
206 |
| |
207 |
1
| mgr1.rollback();
|
208 |
| |
209 |
1
| TestingUtil.sleepThread(500);
|
210 |
| |
211 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
212 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
213 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
214 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
215 |
| } |
216 |
| |
217 |
1
| public void testOptSyncRepl() throws Exception
|
218 |
| { |
219 |
1
| createCaches(true, true);
|
220 |
1
| mgr1.begin();
|
221 |
1
| cache1.put(fqn, key, "value");
|
222 |
| |
223 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
224 |
1
| Assert.assertNull(cache2.get(fqn, key));
|
225 |
1
| Assert.assertNull(loader1.get(fqn));
|
226 |
1
| Assert.assertNull(loader2.get(fqn));
|
227 |
1
| mgr1.commit();
|
228 |
| |
229 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
230 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
231 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
232 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
233 |
| |
234 |
1
| mgr1.begin();
|
235 |
1
| cache1.put(fqn, key, "value2");
|
236 |
| |
237 |
1
| Assert.assertEquals("value2", cache1.get(fqn, key));
|
238 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
239 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
240 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
241 |
| |
242 |
1
| mgr1.rollback();
|
243 |
| |
244 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
245 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
246 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
247 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
248 |
| } |
249 |
| |
250 |
1
| public void testOptAsyncRepl() throws Exception
|
251 |
| { |
252 |
1
| createCaches(false, true);
|
253 |
| |
254 |
1
| mgr1.begin();
|
255 |
1
| cache1.put(fqn, key, "value");
|
256 |
| |
257 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
258 |
1
| Assert.assertNull(cache2.get(fqn, key));
|
259 |
1
| Assert.assertNull(loader1.get(fqn));
|
260 |
1
| Assert.assertNull(loader2.get(fqn));
|
261 |
1
| mgr1.commit();
|
262 |
| |
263 |
1
| TestingUtil.sleepThread(500);
|
264 |
| |
265 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
266 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
267 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
268 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
269 |
| |
270 |
1
| mgr1.begin();
|
271 |
1
| cache1.put(fqn, key, "value2");
|
272 |
| |
273 |
1
| Assert.assertEquals("value2", cache1.get(fqn, key));
|
274 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
275 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
276 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
277 |
| |
278 |
1
| mgr1.rollback();
|
279 |
1
| TestingUtil.sleepThread(500);
|
280 |
| |
281 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
282 |
1
| Assert.assertEquals("value", cache2.get(fqn, key));
|
283 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
284 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
285 |
| } |
286 |
| |
287 |
1
| public static Test suite()
|
288 |
| { |
289 |
1
| return new TestSuite(CacheLoaderWithReplicationTest.class);
|
290 |
| } |
291 |
| |
292 |
0
| public static void main(String[] args)
|
293 |
| { |
294 |
0
| junit.textui.TestRunner.run(suite());
|
295 |
| } |
296 |
| |
297 |
| } |