KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > concurrency > TestPessimistic


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27 package org.objectweb.speedo.runtime.concurrency;
28
29 import org.objectweb.speedo.SpeedoTestHelper;
30 import org.objectweb.speedo.api.ExceptionHelper;
31 import org.objectweb.speedo.pobjects.basic.BasicA;
32 import org.objectweb.util.monolog.api.BasicLevel;
33
34 import javax.jdo.PersistenceManager;
35 import javax.jdo.JDOException;
36 import javax.jdo.JDOFatalException;
37
38 import junit.framework.Assert;
39
40 import java.util.Properties JavaDoc;
41
42 public class TestPessimistic extends SpeedoTestHelper {
43
44     public TestPessimistic(String JavaDoc s) {
45         super(s);
46     }
47
48     protected String JavaDoc getLoggerName() {
49         return LOG_NAME + ".rt.concurrency.TestPessimistic";
50     }
51
52     public Properties JavaDoc getPMFProperties() {
53         Properties JavaDoc p = super.getPMFProperties();
54         p.setProperty("javax.jdo.option.Optimistic", "false");
55         return p;
56     }
57     
58     public void testConcurrentReads() {
59         logger.log(BasicLevel.INFO, "test concurrent reads");
60         // setup
61
BasicA ba = new BasicA();
62         ba.writeF1("1");
63         ba.writeF2(2);
64         PersistenceManager pm = pmf.getPersistenceManager();
65         pm.currentTransaction().begin();
66         pm.makePersistent(ba);
67         final Object JavaDoc id = pm.getObjectId(ba);
68         Assert.assertNotNull("Null object identifier", id);
69         pm.currentTransaction().commit();
70         pm.close();
71
72         ba = null;
73
74         // begin transaction 1
75
PersistenceManager pm1 = pmf.getPersistenceManager();
76         logger.log(BasicLevel.INFO, "Thread1: begin");
77         pm1.currentTransaction().begin();
78         logger.log(BasicLevel.INFO, "Thread1: getObjectById");
79         ba = (BasicA) pm1.getObjectById(id, true);
80         Assert.assertNotNull("Object not found", ba);
81         logger.log(BasicLevel.INFO, "Thread1: read");
82         ba.readF2();
83         logger.log(BasicLevel.INFO, "Thread1: read done");
84
85         // begin transaction 2
86
Thread JavaDoc t = new Thread JavaDoc((new Runnable JavaDoc () {
87             public void run () {
88                 PersistenceManager pm2 = pmf.getPersistenceManager();
89                 logger.log(BasicLevel.INFO, "Thread2: begin");
90                 pm2.currentTransaction().begin();
91                 logger.log(BasicLevel.INFO, "Thread2: getObjectById");
92                 BasicA ba = (BasicA) pm2.getObjectById(id, true);
93                 Assert.assertNotNull("Object not found", ba);
94                 logger.log(BasicLevel.INFO, "Thread2: read");
95                 ba.readF2();
96                 logger.log(BasicLevel.INFO, "Thread2: read done");
97                 logger.log(BasicLevel.INFO, "Thread2: commit");
98                 pm2.currentTransaction().commit();
99                 pm2.close();
100             }
101         }));
102         t.start();
103
104         // end transaction 1
105
logger.log(BasicLevel.INFO, "Thread1: commit");
106         pm1.currentTransaction().commit();
107         pm1.close();
108
109         assertUnlock(t);
110
111         // clean up
112
pm = pmf.getPersistenceManager();
113         pm.currentTransaction().begin();
114         ba = (BasicA) pm.getObjectById(id, true);
115         Assert.assertNotNull("Object not found", ba);
116         Assert.assertEquals("Bad f1 value", "1", ba.readF1());
117         Assert.assertEquals("Bad f2 value", 2, ba.readF2());
118         pm.deletePersistent(ba);
119         pm.currentTransaction().commit();
120         pm.close();
121     }
122
123     public void testReadConcurrentWrite() {
124         logger.log(BasicLevel.INFO, "test read concurrent write");
125         // setup
126
BasicA ba = new BasicA();
127         ba.writeF1("1");
128         ba.writeF2(2);
129         PersistenceManager pm = pmf.getPersistenceManager();
130         pm.currentTransaction().begin();
131         pm.makePersistent(ba);
132         final Object JavaDoc id = pm.getObjectId(ba);
133         Assert.assertNotNull("Null object identifier", id);
134         pm.currentTransaction().commit();
135         pm.close();
136
137         ba = null;
138
139         // begin transaction 1
140
PersistenceManager pm1 = pmf.getPersistenceManager();
141         logger.log(BasicLevel.INFO, "Thread1: begin");
142         pm1.currentTransaction().begin();
143         logger.log(BasicLevel.INFO, "Thread1: getObjectById");
144         ba = (BasicA) pm1.getObjectById(id, true);
145         Assert.assertNotNull("Object not found", ba);
146         logger.log(BasicLevel.INFO, "Thread1: read");
147         ba.readF2();
148         logger.log(BasicLevel.INFO, "Thread1: read done");
149
150         // begin transaction 2
151
Thread JavaDoc t = assertLock(new Runnable JavaDoc () {
152             public void run () {
153                 PersistenceManager pm2 = pmf.getPersistenceManager();
154                 logger.log(BasicLevel.INFO, "Thread2: begin");
155                 pm2.currentTransaction().begin();
156                 logger.log(BasicLevel.INFO, "Thread2: getObjectById");
157                 BasicA ba = (BasicA) pm2.getObjectById(id, true);
158                 Assert.assertNotNull("Object not found", ba);
159                 logger.log(BasicLevel.INFO, "Thread2: write");
160                 ba.writeF2(ba.readF2() + 1);
161                 logger.log(BasicLevel.INFO, "Thread2: write done");
162                 logger.log(BasicLevel.INFO, "Thread2: commit");
163                 pm2.currentTransaction().commit();
164                 pm2.close();
165             }
166         });
167
168         // end transaction 1
169
logger.log(BasicLevel.INFO, "Thread1: commit");
170         pm1.currentTransaction().commit();
171         pm1.close();
172
173         assertUnlock(t);
174
175         // clean up
176
pm = pmf.getPersistenceManager();
177         pm.currentTransaction().begin();
178         ba = (BasicA) pm.getObjectById(id, true);
179         Assert.assertNotNull("Object not found", ba);
180         Assert.assertEquals("Bad f1 value", "1", ba.readF1());
181         Assert.assertEquals("Bad f2 value", 3, ba.readF2());
182         pm.deletePersistent(ba);
183         pm.currentTransaction().commit();
184         pm.close();
185     }
186
187     public void testWriteConcurrentRead() {
188         logger.log(BasicLevel.INFO, "test write concurrent read");
189         // setup
190
BasicA ba = new BasicA();
191         ba.writeF1("1");
192         ba.writeF2(2);
193         PersistenceManager pm = pmf.getPersistenceManager();
194         pm.currentTransaction().begin();
195         pm.makePersistent(ba);
196         final Object JavaDoc id = pm.getObjectId(ba);
197         Assert.assertNotNull("Null object identifier", id);
198         pm.currentTransaction().commit();
199         pm.close();
200
201         ba = null;
202
203         // begin transaction 1
204
PersistenceManager pm1 = pmf.getPersistenceManager();
205         logger.log(BasicLevel.INFO, "Thread1: begin");
206         pm1.currentTransaction().begin();
207         logger.log(BasicLevel.INFO, "Thread1: getObjectById");
208         ba = (BasicA) pm1.getObjectById(id, true);
209         Assert.assertNotNull("Object not found", ba);
210         logger.log(BasicLevel.INFO, "Thread1: write");
211         ba.writeF2(ba.readF2() + 1);
212         logger.log(BasicLevel.INFO, "Thread1: write done");
213
214         // begin transaction 2
215
Thread JavaDoc t = assertLock(new Runnable JavaDoc () {
216             public void run () {
217                 PersistenceManager pm2 = pmf.getPersistenceManager();
218                 logger.log(BasicLevel.INFO, "Thread2: begin");
219                 pm2.currentTransaction().begin();
220                 logger.log(BasicLevel.INFO, "Thread2: getObjectById");
221                 BasicA ba = (BasicA) pm2.getObjectById(id, true);
222                 Assert.assertNotNull("Object not found", ba);
223                 logger.log(BasicLevel.INFO, "Thread2: read");
224                 ba.readF2();
225                 logger.log(BasicLevel.INFO, "Thread2: read done");
226                 logger.log(BasicLevel.INFO, "Thread2: commit");
227                 pm2.currentTransaction().commit();
228                 pm2.close();
229             }
230         });
231
232         // end transaction 1
233
logger.log(BasicLevel.INFO, "Thread1: commit");
234         pm1.currentTransaction().commit();
235         pm1.close();
236
237         assertUnlock(t);
238
239         // clean up
240
pm = pmf.getPersistenceManager();
241         pm.currentTransaction().begin();
242         ba = (BasicA) pm.getObjectById(id, true);
243         Assert.assertNotNull("Object not found", ba);
244         Assert.assertEquals("Bad f1 value", "1", ba.readF1());
245         Assert.assertEquals("Bad f2 value", 3, ba.readF2());
246         pm.deletePersistent(ba);
247         pm.currentTransaction().commit();
248         pm.close();
249     }
250
251     public void testWriteConcurrentRead2() {
252         logger.log(BasicLevel.INFO, "test write concurrent read 2");
253         // setup
254
BasicA ba = new BasicA();
255         ba.writeF1("1");
256         ba.writeF2(2);
257         PersistenceManager pm = pmf.getPersistenceManager();
258         pm.currentTransaction().begin();
259         pm.makePersistent(ba);
260         final Object JavaDoc id = pm.getObjectId(ba);
261         Assert.assertNotNull("Null object identifier", id);
262         pm.currentTransaction().commit();
263         pm.close();
264
265         ba = null;
266
267         // begin transaction 1
268
PersistenceManager pm1 = pmf.getPersistenceManager();
269         logger.log(BasicLevel.INFO, "Thread1: begin");
270         pm1.currentTransaction().begin();
271         logger.log(BasicLevel.INFO, "Thread1: getObjectById");
272         BasicA ba1 = (BasicA) pm1.getObjectById(id, true);
273         Assert.assertNotNull("Object not found", ba1);
274
275         // begin transaction 2
276
Thread JavaDoc t = new Thread JavaDoc((new Runnable JavaDoc () {
277             public void run () {
278                 PersistenceManager pm2 = pmf.getPersistenceManager();
279                 logger.log(BasicLevel.INFO, "Thread2: begin");
280                 pm2.currentTransaction().begin();
281                 logger.log(BasicLevel.INFO, "Thread2: getObjectById");
282                 BasicA ba2 = (BasicA) pm2.getObjectById(id, true);
283                 Assert.assertNotNull("Object not found", ba2);
284                 logger.log(BasicLevel.INFO, "Thread2: read");
285                 ba2.readF2();
286                 logger.log(BasicLevel.INFO, "Thread2: read done");
287                 logger.log(BasicLevel.INFO, "Thread2: commit");
288                 pm2.currentTransaction().commit();
289                 pm2.close();
290             }
291         }));
292         t.start();
293
294         // end transaction 1
295
logger.log(BasicLevel.INFO, "Thread1: write");
296         ba1.writeF2(ba1.readF2() + 1);
297         logger.log(BasicLevel.INFO, "Thread1: write done");
298         logger.log(BasicLevel.INFO, "Thread1: commit");
299         pm1.currentTransaction().commit();
300         pm1.close();
301
302         assertUnlock(t);
303
304         // clean up
305
pm = pmf.getPersistenceManager();
306         pm.currentTransaction().begin();
307         ba = (BasicA) pm.getObjectById(id, true);
308         Assert.assertNotNull("Object not found", ba);
309         Assert.assertEquals("Bad f1 value", "1", ba.readF1());
310         Assert.assertEquals("Bad f2 value", 3, ba.readF2());
311         pm.deletePersistent(ba);
312         pm.currentTransaction().commit();
313         pm.close();
314     }
315
316     public void testConcurrentWrites() {
317         logger.log(BasicLevel.INFO, "test concurrent writes");
318         // setup
319
BasicA ba = new BasicA();
320         ba.writeF1("1");
321         ba.writeF2(2);
322         PersistenceManager pm = pmf.getPersistenceManager();
323         pm.currentTransaction().begin();
324         pm.makePersistent(ba);
325         final Object JavaDoc id = pm.getObjectId(ba);
326         Assert.assertNotNull("Null object identifier", id);
327         pm.currentTransaction().commit();
328         pm.close();
329
330         ba = null;
331
332         // begin transaction 1
333
PersistenceManager pm1 = pmf.getPersistenceManager();
334         logger.log(BasicLevel.INFO, "Thread1: begin");
335         pm1.currentTransaction().begin();
336         logger.log(BasicLevel.INFO, "Thread1: getObjectById");
337         ba = (BasicA) pm1.getObjectById(id, true);
338         Assert.assertNotNull("Object not found", ba);
339         logger.log(BasicLevel.INFO, "Thread1: write");
340         ba.writeF2(ba.readF2() + 1);
341         logger.log(BasicLevel.INFO, "Thread1: write done");
342
343         // begin transaction 2
344
Thread JavaDoc t = assertLock(new Runnable JavaDoc () {
345             public void run () {
346                 PersistenceManager pm2 = pmf.getPersistenceManager();
347                 try {
348                     logger.log(BasicLevel.INFO, "Thread2: begin");
349                     pm2.currentTransaction().begin();
350                     logger.log(BasicLevel.INFO, "Thread2: getObjectById");
351                     BasicA ba = (BasicA) pm2.getObjectById(id, true);
352                     Assert.assertNotNull("Object not found", ba);
353                     logger.log(BasicLevel.INFO, "Thread2: write");
354                     ba.writeF2(ba.readF2() + 1);
355                     logger.log(BasicLevel.INFO, "Thread2: write done");
356
357                     logger.log(BasicLevel.INFO, "Thread2: commit");
358                     pm2.currentTransaction().commit();
359                 } catch (JDOFatalException e) {
360
361                 } finally {
362                     pm2.close();
363                 }
364             }
365         });
366
367         // end transaction 1
368
logger.log(BasicLevel.INFO, "Thread1: commit");
369         pm1.currentTransaction().commit();
370         pm1.close();
371
372         // end transaction 2
373
assertUnlock(t);
374
375         // clean up
376
pm = pmf.getPersistenceManager();
377         pm.currentTransaction().begin();
378         ba = (BasicA) pm.getObjectById(id, true);
379         Assert.assertNotNull("Object not found", ba);
380         Assert.assertEquals("Bad f1 value", "1", ba.readF1());
381         Assert.assertEquals("Bad f2 value", 4, ba.readF2());
382         pm.deletePersistent(ba);
383         pm.currentTransaction().commit();
384         pm.close();
385     }
386
387     public void testConcurrentWrites2() {
388         logger.log(BasicLevel.INFO, "test concurrent writes 2");
389         // setup
390
BasicA ba = new BasicA();
391         ba.writeF1("1");
392         ba.writeF2(2);
393         PersistenceManager pm = pmf.getPersistenceManager();
394         pm.currentTransaction().begin();
395         pm.makePersistent(ba);
396         final Object JavaDoc id = pm.getObjectId(ba);
397         Assert.assertNotNull("Null object identifier", id);
398         pm.currentTransaction().commit();
399         pm.close();
400
401         ba = null;
402
403         // begin transaction 1
404
PersistenceManager pm1 = pmf.getPersistenceManager();
405         logger.log(BasicLevel.INFO, "Thread1: begin");
406         pm1.currentTransaction().begin();
407         logger.log(BasicLevel.INFO, "Thread1: getObjectById");
408         ba = (BasicA) pm1.getObjectById(id, true);
409         Assert.assertNotNull("Object not found", ba);
410
411         // begin transaction 2
412
Thread JavaDoc t = assertLock(new Runnable JavaDoc () {
413             public void run () {
414                 PersistenceManager pm2 = pmf.getPersistenceManager();
415                 try {
416                     logger.log(BasicLevel.INFO, "Thread2: begin");
417                     pm2.currentTransaction().begin();
418                     logger.log(BasicLevel.INFO, "Thread2: getObjectById");
419                     BasicA ba = (BasicA) pm2.getObjectById(id, true);
420                     Assert.assertNotNull("Object not found", ba);
421                     logger.log(BasicLevel.INFO, "Thread2: write");
422                     ba.writeF2(ba.readF2() + 1);
423                     logger.log(BasicLevel.INFO, "Thread2: write done");
424
425                     logger.log(BasicLevel.INFO, "Thread2: commit");
426                     pm2.currentTransaction().commit();
427                 } catch (JDOFatalException e) {
428                 } finally {
429                     pm2.close();
430                 }
431             }
432         });
433
434         // end transaction 1
435
logger.log(BasicLevel.INFO, "Thread1: write");
436         boolean ok = true;
437         try {
438             ba.writeF2(ba.readF2() + 1);
439             ok = true;
440             logger.log(BasicLevel.INFO, "Thread1: write done");
441             logger.log(BasicLevel.INFO, "Thread1: commit");
442             pm1.currentTransaction().commit();
443         } catch (JDOException e) {
444             logger.log(BasicLevel.INFO, "Thread1: write aborted"
445                     + ExceptionHelper.getNested(e).getMessage());
446             ok = false;
447         } finally {
448             pm1.close();
449         }
450         assertUnlock(t);
451
452         // clean up
453
pm = pmf.getPersistenceManager();
454         pm.currentTransaction().begin();
455         ba = (BasicA) pm.getObjectById(id, true);
456         Assert.assertNotNull("Object not found", ba);
457         Assert.assertEquals("Bad f1 value", "1", ba.readF1());
458         Assert.assertEquals("Bad f2 value", ( ok ? 4 : 3), ba.readF2());
459         pm.deletePersistent(ba);
460         pm.currentTransaction().commit();
461         pm.close();
462     }
463
464     private Thread JavaDoc assertLock (Runnable JavaDoc r) {
465         Thread JavaDoc t = new Thread JavaDoc(r);
466         t.start();
467         try {
468             t.join(5000);
469         } catch (InterruptedException JavaDoc _) {
470             // ignored
471
}
472         assertTrue(t.isAlive());
473         logger.log(BasicLevel.INFO, "Thread2: locked");
474         return t;
475     }
476
477     private void assertUnlock (Thread JavaDoc t) {
478         try {
479             t.join(5000);
480         } catch (InterruptedException JavaDoc _) {
481             // ignored
482
}
483         assertTrue(!t.isAlive());
484     }
485 }
486
Popular Tags