1 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 ; 41 42 public class TestPessimistic extends SpeedoTestHelper { 43 44 public TestPessimistic(String s) { 45 super(s); 46 } 47 48 protected String getLoggerName() { 49 return LOG_NAME + ".rt.concurrency.TestPessimistic"; 50 } 51 52 public Properties getPMFProperties() { 53 Properties 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 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 id = pm.getObjectId(ba); 68 Assert.assertNotNull("Null object identifier", id); 69 pm.currentTransaction().commit(); 70 pm.close(); 71 72 ba = null; 73 74 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 Thread t = new Thread ((new Runnable () { 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 logger.log(BasicLevel.INFO, "Thread1: commit"); 106 pm1.currentTransaction().commit(); 107 pm1.close(); 108 109 assertUnlock(t); 110 111 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 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 id = pm.getObjectId(ba); 133 Assert.assertNotNull("Null object identifier", id); 134 pm.currentTransaction().commit(); 135 pm.close(); 136 137 ba = null; 138 139 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 Thread t = assertLock(new Runnable () { 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 logger.log(BasicLevel.INFO, "Thread1: commit"); 170 pm1.currentTransaction().commit(); 171 pm1.close(); 172 173 assertUnlock(t); 174 175 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 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 id = pm.getObjectId(ba); 197 Assert.assertNotNull("Null object identifier", id); 198 pm.currentTransaction().commit(); 199 pm.close(); 200 201 ba = null; 202 203 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 Thread t = assertLock(new Runnable () { 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 logger.log(BasicLevel.INFO, "Thread1: commit"); 234 pm1.currentTransaction().commit(); 235 pm1.close(); 236 237 assertUnlock(t); 238 239 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 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 id = pm.getObjectId(ba); 261 Assert.assertNotNull("Null object identifier", id); 262 pm.currentTransaction().commit(); 263 pm.close(); 264 265 ba = null; 266 267 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 Thread t = new Thread ((new Runnable () { 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 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 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 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 id = pm.getObjectId(ba); 326 Assert.assertNotNull("Null object identifier", id); 327 pm.currentTransaction().commit(); 328 pm.close(); 329 330 ba = null; 331 332 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 Thread t = assertLock(new Runnable () { 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 logger.log(BasicLevel.INFO, "Thread1: commit"); 369 pm1.currentTransaction().commit(); 370 pm1.close(); 371 372 assertUnlock(t); 374 375 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 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 id = pm.getObjectId(ba); 397 Assert.assertNotNull("Null object identifier", id); 398 pm.currentTransaction().commit(); 399 pm.close(); 400 401 ba = null; 402 403 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 Thread t = assertLock(new Runnable () { 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 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 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 assertLock (Runnable r) { 465 Thread t = new Thread (r); 466 t.start(); 467 try { 468 t.join(5000); 469 } catch (InterruptedException _) { 470 } 472 assertTrue(t.isAlive()); 473 logger.log(BasicLevel.INFO, "Thread2: locked"); 474 return t; 475 } 476 477 private void assertUnlock (Thread t) { 478 try { 479 t.join(5000); 480 } catch (InterruptedException _) { 481 } 483 assertTrue(!t.isAlive()); 484 } 485 } 486 | Popular Tags |