1 package org.jboss.cache.tests.lock; 2 3 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 4 import EDU.oswego.cs.dl.util.concurrent.FIFOSemaphore; 5 import EDU.oswego.cs.dl.util.concurrent.Sync; 6 import junit.framework.Test; 7 import junit.framework.TestCase; 8 import junit.framework.TestSuite; 9 import org.jboss.cache.lock.*; 10 11 17 public class LockTest extends TestCase { 18 int value=10; 19 Throwable t1_ex, t2_ex; 20 long start=0; 21 final long TIMEOUT=5000; 22 final long SLEEP=500; 23 24 25 public LockTest(String name) { 26 super(name); 27 } 28 29 30 31 public void tearDown() throws Exception { 32 super.tearDown(); 33 t1_ex=t2_ex=null; 34 } 35 36 37 class MyFIFOSemaphore extends FIFOSemaphore { 38 public MyFIFOSemaphore(long l) { 39 super(l); 40 } 41 42 public void acquire() throws InterruptedException { 43 super.acquire(); 45 } 47 48 public void release() { 49 super.release(); 51 } 53 } 54 55 56 67 public void testReadUncommitted() throws Throwable { 68 final LockStrategy s=new LockStrategyReadUncommitted(); 69 final FIFOSemaphore sem=new MyFIFOSemaphore(1); 70 final CyclicBarrier barrier=new CyclicBarrier(2); 71 72 Thread t1=new Thread ("t1") { 73 Sync lock=null; 74 75 public void run() { 76 try { 77 sem.acquire(); 79 barrier.barrier(); lock=s.readLock(); 83 lock.attempt(TIMEOUT); 84 log("1st read: value is " + value); 85 assertEquals(10, value); 86 sem.release(); _sleep(100); 88 89 sem.acquire(); log("2nd read: value is " + value + "; we should see t2's uncommitted change (20)"); 91 assertEquals(20, value); sem.release(); 93 _sleep(100); 94 95 sem.acquire(); log("3rd read: value is still " + value + "; we should see t2's committed change"); 97 assertEquals(20, value); 98 } 99 catch(Throwable ex) { 100 t1_ex=ex; 101 } 102 finally { 103 if(lock != null) 104 lock.release(); 105 sem.release(); 106 } 107 } 108 }; 109 110 111 Thread t2=new Thread ("t2") { 112 Sync lock=null; 113 114 public void run() { 115 try { 116 _sleep(100); 117 barrier.barrier(); 119 sem.acquire(); 121 lock=s.writeLock(); 122 lock.attempt(TIMEOUT); 123 log("changing value from " + value + " to 20"); 124 value=20; 125 sem.release(); _sleep(100); 127 128 sem.acquire(); log("committing the TX"); 130 lock.release(); 131 } 132 catch(Throwable ex) { 133 t2_ex=ex; 134 } 135 finally { 136 if(lock != null) 137 lock.release(); 138 sem.release(); 139 } 140 } 141 }; 142 143 t1.start(); 144 t2.start(); 145 t1.join(); 146 t2.join(); 147 if(t1_ex != null) 148 throw t1_ex; 149 if(t2_ex != null) 150 throw t2_ex; 151 } 152 153 154 155 167 232 233 234 public void testWriteThanRead() throws Throwable { 235 final LockStrategy s=new LockStrategyReadCommitted(); 236 237 Thread t1=new Thread ("t1") { 238 Sync lock=null; 239 240 public void run() { 241 try { 242 _sleep(100); 243 lock=s.readLock(); 244 lock.attempt(TIMEOUT); 245 log("1st read: value is " + value); 246 assertEquals(20, value); 247 _sleep(SLEEP); 248 249 log("2nd read: value is " + value + "; we should see t2's uncommitted change (20)"); 250 assertEquals(20, value); _sleep(SLEEP); 252 } 253 catch(Throwable ex) { 254 t1_ex=ex; 255 } 256 finally { 257 lock.release(); 258 } 259 } 260 }; 261 262 263 Thread t2=new Thread ("t2") { 264 Sync lock=null; 265 266 public void run() { 267 try { 268 lock=s.writeLock(); 269 lock.attempt(TIMEOUT); 270 log("changing value from " + value + " to 20"); 271 value=20; 272 _sleep(SLEEP); 273 274 log("committing the TX"); 275 lock.release(); 276 } 277 catch(Throwable ex) { 278 t2_ex=ex; 279 } 280 finally { 281 lock.release(); 282 } 283 } 284 }; 285 286 t2.start(); 287 t1.start(); 288 t2.join(); 289 t1.join(); 290 if(t1_ex != null) 291 throw t1_ex; 292 if(t2_ex != null) 293 throw t2_ex; 294 } 295 296 297 298 299 316 public void testRepeatableRead() throws Throwable { 317 final LockStrategy s=new LockStrategyRepeatableRead(); 318 319 Thread t1=new Thread ("t1") { 320 Sync lock=null; 321 322 public void run() { 323 try { 324 lock=s.readLock(); 325 lock.attempt(TIMEOUT); 326 log("1st read: value is " + value); 327 assertEquals(10, value); 328 _sleep(SLEEP); 329 330 log("2nd read: value is " + value + "; we should *not* see t2's uncommitted change (20)"); 331 assertEquals(10, value); 332 _sleep(SLEEP); 333 334 log("3rd read: value is still " + value + "; we should not see t2's committed change"); 335 assertEquals(10, value); 336 lock.release(); 337 338 _sleep(SLEEP); 339 lock.attempt(TIMEOUT); 340 log("4th read: value is now " + value + "; we should see t2's committed change in our new TX"); 341 assertEquals(20, value); 342 } 343 catch(Throwable ex) { 344 t1_ex=ex; 345 } 346 finally { 347 lock.release(); 348 } 349 } 350 }; 351 352 353 Thread t2=new Thread ("t2") { 354 Sync lock=null; 355 356 public void run() { 357 try { 358 _sleep(100); 359 lock=s.writeLock(); 360 lock.attempt(TIMEOUT); 361 log("changing value from " + value + " to 20"); 362 value=20; 363 _sleep(SLEEP); 364 365 log("committing the TX"); 366 lock.release(); 367 } 368 catch(Throwable ex) { 369 t2_ex=ex; 370 } 371 finally { 372 lock.release(); 373 } 374 } 375 }; 376 377 t1.start(); 378 t2.start(); 379 t1.join(); 380 t2.join(); 381 if(t1_ex != null) 382 throw t1_ex; 383 if(t2_ex != null) 384 throw t2_ex; 385 } 386 387 388 401 public void testSerializable() throws Throwable { 402 final LockStrategy s=new LockStrategySerializable(); 403 404 Thread t1=new Thread ("t1") { 405 Sync lock=null; 406 407 public void run() { 408 try { 409 lock=s.readLock(); 410 lock.attempt(TIMEOUT); 411 log("1st read: value is " + value); 412 assertEquals(10, value); 413 lock.release(); 414 _sleep(SLEEP); 415 416 lock.attempt(TIMEOUT); 417 log("2nd read: value is " + value + "; we should see t2's committed change (20)"); 418 assertEquals(20, value); 419 } 420 catch(Throwable ex) { 421 t1_ex=ex; 422 } 423 finally { 424 lock.release(); 425 } 426 } 427 }; 428 429 430 Thread t2=new Thread ("t2") { 431 Sync lock=null; 432 433 public void run() { 434 try { 435 _sleep(100); 436 lock=s.writeLock(); 437 lock.attempt(TIMEOUT); 438 log("changing value from " + value + " to 20"); 439 value=20; 440 log("committing the TX"); 441 lock.release(); 442 } 443 catch(Throwable ex) { 444 t2_ex=ex; 445 } 446 finally { 447 lock.release(); 448 } 449 } 450 }; 451 452 t1.start(); 453 t2.start(); 454 t1.join(); 455 t2.join(); 456 if(t1_ex != null) 457 throw t1_ex; 458 if(t2_ex != null) 459 throw t2_ex; 460 } 461 462 463 464 void _sleep(long time) { 465 Thread.yield(); 466 try {Thread.sleep(time);} catch(InterruptedException e) {} 467 } 468 469 470 void log(String s) { 471 long now; 472 if(start == 0) 473 start=System.currentTimeMillis(); 474 now=System.currentTimeMillis(); 475 476 System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s); 477 } 478 479 480 public static void main(String [] args) throws Exception { 481 junit.textui.TestRunner.run(suite()); 482 } 483 484 public static Test suite() { 486 TestSuite suite=new TestSuite(); 487 suite.addTestSuite(LockTest.class); 488 return suite; 489 } 490 491 492 493 494 } 495 496 | Popular Tags |