1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.pojo.rollback; |
9 |
| |
10 |
| import junit.framework.Test; |
11 |
| import junit.framework.TestCase; |
12 |
| import junit.framework.TestSuite; |
13 |
| import org.apache.commons.logging.Log; |
14 |
| import org.apache.commons.logging.LogFactory; |
15 |
| import org.jboss.cache.config.Configuration.CacheMode; |
16 |
| import org.jboss.cache.factories.UnitTestCacheConfigurationFactory; |
17 |
| import org.jboss.cache.pojo.PojoCache; |
18 |
| import org.jboss.cache.pojo.PojoCacheFactory; |
19 |
| import org.jboss.cache.pojo.TestingUtil; |
20 |
| import org.jboss.cache.pojo.test.Person; |
21 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
22 |
| |
23 |
| import javax.naming.Context; |
24 |
| import javax.naming.InitialContext; |
25 |
| import javax.naming.NamingException; |
26 |
| import javax.transaction.NotSupportedException; |
27 |
| import javax.transaction.RollbackException; |
28 |
| import javax.transaction.SystemException; |
29 |
| import javax.transaction.Transaction; |
30 |
| import javax.transaction.UserTransaction; |
31 |
| import java.util.ArrayList; |
32 |
| import java.util.List; |
33 |
| import java.util.Properties; |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class ReplicatedTxTest extends TestCase |
39 |
| { |
40 |
| Log log = LogFactory.getLog(org.jboss.cache.pojo.rollback.ReplicatedTxTest.class); |
41 |
| PojoCache cache, cache1; |
42 |
| final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; |
43 |
| DummyTransactionManager tx_mgr; |
44 |
| Throwable t1_ex, t2_ex; |
45 |
| long start = 0; |
46 |
| |
47 |
| |
48 |
4
| public ReplicatedTxTest(String name)
|
49 |
| { |
50 |
4
| super(name);
|
51 |
| } |
52 |
| |
53 |
4
| protected void setUp() throws Exception
|
54 |
| { |
55 |
4
| super.setUp();
|
56 |
4
| log.info("setUp() ....");
|
57 |
4
| boolean toStart = false;
|
58 |
4
| cache = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
|
59 |
4
| cache.start();
|
60 |
4
| cache1 = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
|
61 |
4
| cache1.start();
|
62 |
| |
63 |
4
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
|
64 |
| |
65 |
4
| tx_mgr = DummyTransactionManager.getInstance();
|
66 |
4
| t1_ex = t2_ex = null;
|
67 |
| } |
68 |
| |
69 |
4
| protected void tearDown() throws Exception
|
70 |
| { |
71 |
4
| super.tearDown();
|
72 |
4
| cache.stop();
|
73 |
4
| cache1.stop();
|
74 |
| |
75 |
4
| DummyTransactionManager.destroy();
|
76 |
| } |
77 |
| |
78 |
| |
79 |
| |
80 |
6
| UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
|
81 |
| { |
82 |
6
| Properties prop = new Properties();
|
83 |
6
| prop.put(Context.INITIAL_CONTEXT_FACTORY,
|
84 |
| "org.jboss.cache.transaction.DummyContextFactory"); |
85 |
6
| return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
|
86 |
| } |
87 |
| |
88 |
5
| private Person createPerson(String id, String name, int age)
|
89 |
| { |
90 |
5
| Person p = new Person();
|
91 |
5
| p.setName(name);
|
92 |
5
| p.setAge(age);
|
93 |
5
| return p;
|
94 |
| } |
95 |
| |
96 |
1
| public void testSimple() throws Exception
|
97 |
| { |
98 |
1
| log.info("testSimple() ....");
|
99 |
1
| UserTransaction tx = getTransaction();
|
100 |
1
| tx.begin();
|
101 |
1
| Person p = createPerson("/person/test1", "Harald Gliebe", 32);
|
102 |
1
| cache.attach("/person/test1", p);
|
103 |
| |
104 |
1
| tx.commit();
|
105 |
1
| tx.begin();
|
106 |
1
| p.setName("Benoit");
|
107 |
1
| tx.commit();
|
108 |
1
| Person p1 = (Person) cache1.find("/person/test1");
|
109 |
1
| assertEquals("Benoit", p.getName());
|
110 |
1
| assertEquals("Benoit", p1.getName());
|
111 |
1
| tx.begin();
|
112 |
1
| p1.setAge(61);
|
113 |
1
| tx.commit();
|
114 |
1
| assertEquals(61, p.getAge());
|
115 |
1
| assertEquals(61, p1.getAge());
|
116 |
| } |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
1
| public void testConcurrentPuts() throws Exception
|
122 |
| { |
123 |
1
| Thread t1 = new Thread()
|
124 |
| { |
125 |
| Transaction tx; |
126 |
| |
127 |
1
| public void run()
|
128 |
| { |
129 |
1
| try
|
130 |
| { |
131 |
1
| Person p = createPerson("/person/test6", "p6", 50);
|
132 |
1
| List<String> lang = new ArrayList<String>();
|
133 |
1
| lang.add("German");
|
134 |
1
| p.setLanguages(lang);
|
135 |
1
| UserTransaction tx = getTransaction();
|
136 |
1
| tx.begin();
|
137 |
1
| cache.attach("/person/test6", p);
|
138 |
1
| TestingUtil.sleepThread(17000);
|
139 |
1
| tx.commit();
|
140 |
| } |
141 |
| catch (RollbackException rollback) |
142 |
| { |
143 |
| ; |
144 |
| } |
145 |
| catch (Exception ex) |
146 |
| { |
147 |
0
| t1_ex = ex;
|
148 |
| } |
149 |
| } |
150 |
| }; |
151 |
| |
152 |
1
| Thread t2 = new Thread()
|
153 |
| { |
154 |
| Transaction tx; |
155 |
| |
156 |
1
| public void run()
|
157 |
| { |
158 |
1
| UserTransaction tx = null;
|
159 |
1
| Person p = createPerson("/person/test6", "p6", 50);
|
160 |
1
| try
|
161 |
| { |
162 |
1
| TestingUtil.sleepThread(1000);
|
163 |
1
| List<String> lang = new ArrayList<String>();
|
164 |
1
| lang.add("German");
|
165 |
1
| p.setLanguages(lang);
|
166 |
1
| tx = getTransaction();
|
167 |
1
| tx.begin();
|
168 |
1
| cache.attach("/person/test6", p);
|
169 |
0
| tx.commit();
|
170 |
| } |
171 |
| catch (RollbackException rollback) |
172 |
| { |
173 |
| ; |
174 |
| } |
175 |
| catch (Exception ex) |
176 |
| { |
177 |
1
| try
|
178 |
| { |
179 |
1
| tx.rollback();
|
180 |
| } |
181 |
| catch (SystemException e) |
182 |
| { |
183 |
0
| e.printStackTrace();
|
184 |
0
| t2_ex = e;
|
185 |
| } |
186 |
| } |
187 |
| |
188 |
1
| cache.attach("/person/test6", p);
|
189 |
| |
190 |
| } |
191 |
| }; |
192 |
| |
193 |
1
| t1.start();
|
194 |
1
| t2.start();
|
195 |
| |
196 |
1
| t1.join();
|
197 |
1
| t2.join();
|
198 |
| |
199 |
| |
200 |
1
| if (t2_ex != null)
|
201 |
0
| fail("Thread1 failed: " + t2_ex);
|
202 |
1
| if (t1_ex != null)
|
203 |
0
| fail("Thread2 failed: " + t1_ex);
|
204 |
| |
205 |
1
| int size = ((Person) cache.find("/person/test6")).getLanguages().size();
|
206 |
1
| assertEquals("number of languages", 1, size);
|
207 |
1
| size = ((Person) cache1.find("/person/test6")).getLanguages().size();
|
208 |
1
| assertEquals("number of languages", 1, size);
|
209 |
| } |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
1
| public void testConcurrentPuts1() throws Exception
|
215 |
| { |
216 |
1
| Thread t1 = new Thread()
|
217 |
| { |
218 |
| Transaction tx; |
219 |
| |
220 |
1
| public void run()
|
221 |
| { |
222 |
1
| try
|
223 |
| { |
224 |
1
| List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
|
225 |
1
| UserTransaction tx = getTransaction();
|
226 |
1
| tx.begin();
|
227 |
1
| lang.add("German");
|
228 |
1
| TestingUtil.sleepThread(17000);
|
229 |
1
| tx.commit();
|
230 |
| } |
231 |
| catch (RollbackException rollback) |
232 |
| { |
233 |
| ; |
234 |
| } |
235 |
| catch (Exception ex) |
236 |
| { |
237 |
0
| t1_ex = ex;
|
238 |
| } |
239 |
| } |
240 |
| }; |
241 |
| |
242 |
1
| Thread t2 = new Thread()
|
243 |
| { |
244 |
| Transaction tx; |
245 |
| |
246 |
1
| public void run()
|
247 |
| { |
248 |
1
| UserTransaction tx = null;
|
249 |
1
| try
|
250 |
| { |
251 |
1
| TestingUtil.sleepThread(1000);
|
252 |
1
| List<String> lang = ((Person) cache1.find("/person/test6")).getLanguages();
|
253 |
1
| tx = getTransaction();
|
254 |
1
| tx.begin();
|
255 |
1
| lang.add("English");
|
256 |
1
| tx.commit();
|
257 |
| } |
258 |
| catch (RollbackException rollback) |
259 |
| { |
260 |
| ; |
261 |
| } |
262 |
| catch (Exception ex) |
263 |
| { |
264 |
0
| try
|
265 |
| { |
266 |
0
| tx.rollback();
|
267 |
| } |
268 |
| catch (SystemException e) |
269 |
| { |
270 |
0
| e.printStackTrace();
|
271 |
0
| t2_ex = e;
|
272 |
| } |
273 |
| } |
274 |
| } |
275 |
| }; |
276 |
| |
277 |
1
| Person p = createPerson("/person/test6", "p6", 50);
|
278 |
1
| cache.attach("/person/test6", p);
|
279 |
1
| List<String> lang = new ArrayList<String>();
|
280 |
1
| lang.add("German");
|
281 |
1
| p.setLanguages(lang);
|
282 |
| |
283 |
1
| t1.start();
|
284 |
1
| t2.start();
|
285 |
| |
286 |
1
| t1.join();
|
287 |
1
| t2.join();
|
288 |
| |
289 |
| |
290 |
1
| if (t2_ex != null)
|
291 |
0
| fail("Thread1 failed: " + t2_ex);
|
292 |
1
| if (t1_ex != null)
|
293 |
0
| fail("Thread2 failed: " + t1_ex);
|
294 |
| |
295 |
1
| int size = ((Person) cache.find("/person/test6")).getLanguages().size();
|
296 |
1
| assertEquals("number of languages", 2, size);
|
297 |
1
| size = ((Person) cache1.find("/person/test6")).getLanguages().size();
|
298 |
1
| assertEquals("number of languages", 2, size);
|
299 |
| } |
300 |
| |
301 |
| |
302 |
| |
303 |
| |
304 |
1
| public void testConcurrentPuts2() throws Exception
|
305 |
| { |
306 |
1
| Thread t1 = new Thread()
|
307 |
| { |
308 |
| Transaction tx; |
309 |
| |
310 |
1
| public void run()
|
311 |
| { |
312 |
1
| try
|
313 |
| { |
314 |
1
| List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
|
315 |
1
| UserTransaction tx = getTransaction();
|
316 |
1
| tx.begin();
|
317 |
1
| lang.add("German");
|
318 |
1
| TestingUtil.sleepThread(17000);
|
319 |
1
| tx.commit();
|
320 |
| } |
321 |
| catch (RollbackException rollback) |
322 |
| { |
323 |
| ; |
324 |
| } |
325 |
| catch (Exception ex) |
326 |
| { |
327 |
0
| t1_ex = ex;
|
328 |
| } |
329 |
| } |
330 |
| }; |
331 |
| |
332 |
1
| Thread t2 = new Thread()
|
333 |
| { |
334 |
| Transaction tx; |
335 |
| |
336 |
1
| public void run()
|
337 |
| { |
338 |
1
| UserTransaction tx = null;
|
339 |
1
| try
|
340 |
| { |
341 |
1
| TestingUtil.sleepThread(1000);
|
342 |
1
| List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
|
343 |
0
| tx = getTransaction();
|
344 |
0
| tx.begin();
|
345 |
0
| lang.add("English");
|
346 |
0
| tx.commit();
|
347 |
| } |
348 |
| catch (RollbackException rollback) |
349 |
| { |
350 |
| ; |
351 |
| } |
352 |
| catch (Exception ex) |
353 |
| { |
354 |
1
| try
|
355 |
| { |
356 |
1
| tx.rollback();
|
357 |
| } |
358 |
| catch (SystemException e) |
359 |
| { |
360 |
0
| e.printStackTrace();
|
361 |
0
| t2_ex = e;
|
362 |
| } |
363 |
| } |
364 |
| } |
365 |
| }; |
366 |
| |
367 |
1
| Person p = createPerson("/person/test6", "p6", 50);
|
368 |
1
| cache.attach("/person/test6", p);
|
369 |
1
| List<String> lang = new ArrayList<String>();
|
370 |
1
| lang.add("German");
|
371 |
1
| p.setLanguages(lang);
|
372 |
| |
373 |
1
| t1.start();
|
374 |
1
| t2.start();
|
375 |
| |
376 |
1
| t1.join();
|
377 |
1
| t2.join();
|
378 |
| |
379 |
| |
380 |
1
| if (t2_ex != null)
|
381 |
0
| fail("Thread1 failed: " + t2_ex);
|
382 |
1
| if (t1_ex != null)
|
383 |
0
| fail("Thread2 failed: " + t1_ex);
|
384 |
| |
385 |
1
| int size = ((Person) cache.find("/person/test6")).getLanguages().size();
|
386 |
1
| assertEquals("number of languages", 2, size);
|
387 |
1
| size = ((Person) cache1.find("/person/test6")).getLanguages().size();
|
388 |
1
| assertEquals("number of languages", 2, size);
|
389 |
| } |
390 |
| |
391 |
0
| void log(String s)
|
392 |
| { |
393 |
0
| long now;
|
394 |
0
| if (start == 0)
|
395 |
0
| start = System.currentTimeMillis();
|
396 |
0
| now = System.currentTimeMillis();
|
397 |
| |
398 |
0
| System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
|
399 |
| } |
400 |
| |
401 |
| |
402 |
1
| public static Test suite() throws Exception
|
403 |
| { |
404 |
1
| return new TestSuite(org.jboss.cache.pojo.rollback.ReplicatedTxTest.class);
|
405 |
| } |
406 |
| |
407 |
| |
408 |
0
| public static void main(String[] args) throws Exception
|
409 |
| { |
410 |
0
| junit.textui.TestRunner.run(org.jboss.cache.pojo.rollback.ReplicatedTxTest.suite());
|
411 |
| } |
412 |
| |
413 |
| } |