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