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