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