1 7 package org.jboss.cache.tests.lock; 8 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.Fqn; 16 import org.jboss.cache.GlobalTransaction; 17 import org.jboss.cache.lock.*; 18 19 20 25 public class IdentityLockTest extends TestCase 26 { 27 IdentityLock lock_; 28 Object other_ = new Object (); 29 Log logger_=LogFactory.getLog(IdentityLockTest.class); 30 static Throwable thread_ex=null; 31 final Fqn FQN=Fqn.fromString("/dummyfqn"); 32 33 34 public IdentityLockTest(String name) 35 { 36 super(name); 37 } 38 39 protected void setUp() throws Exception 40 { 41 super.setUp(); 42 lock_ = new IdentityLock(null, FQN); 43 44 } 47 48 protected void tearDown() throws Exception 49 { 50 super.tearDown(); 51 lock_.releaseAll(); 52 lock_ = null; 53 thread_ex=null; 54 } 55 56 protected void setLevelRW() 57 { 58 log("set lock level to RWUpgrade ..."); 59 LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 60 lock_ = new IdentityLock(null, FQN); 61 } 62 63 protected void setLevelSerial() 64 { 65 log("set lock level to SimpleLock ..."); 66 LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE); 67 lock_ = new IdentityLock(null, FQN); 68 } 69 70 protected GlobalTransaction getGlobalTransactionFromThread() 71 { 72 GlobalTransaction gtx = null; 73 if (gtx == null) { 74 gtx = GlobalTransaction.create(null); 75 } 76 77 return gtx; 78 } 79 80 public void testNullOwner_RWLock() throws InterruptedException { 81 setLevelRW(); 82 nullOwner(); 83 } 84 85 public void testNullOwner_SimpleLock() throws InterruptedException { 86 setLevelSerial(); 87 nullOwner(); 88 } 89 90 protected void nullOwner() throws InterruptedException { 91 log("testNullOwner ..."); 92 try { 93 GlobalTransaction gtx = getGlobalTransactionFromThread(); 94 lock_.acquireWriteLock(gtx, 50); 95 lock_.release(gtx); 96 97 lock_.acquireReadLock(gtx, 50); 98 lock_.release(gtx); 99 } catch (LockingException e) { 100 fail(e.toString()); 101 } catch (TimeoutException e) { 102 fail(e.toString()); 103 } 104 } 105 106 public void testNullOwner2_RWLock() throws InterruptedException { 107 setLevelRW(); 108 nullOwner2(); 109 } 110 111 public void testNullOwner2_SimpleLock() throws InterruptedException { 112 setLevelSerial(); 113 nullOwner2(); 114 } 115 116 protected void nullOwner2() throws InterruptedException { 117 log("testNullOwner2 ..."); 118 try { 119 GlobalTransaction gtx = getGlobalTransactionFromThread(); 120 lock_.acquireReadLock(gtx, 50); 121 lock_.acquireWriteLock(gtx, 50); lock_.release(gtx); 123 } catch (LockingException e) { 124 fail(e.toString()); 125 } catch (TimeoutException e2) { 126 fail(e2.toString()); 127 } 128 } 129 130 public void testNullOwner3_RWLock() throws InterruptedException { 131 setLevelRW(); 132 nullOwner3(); 133 } 134 135 public void testNullOwner3_SimpleLock() throws InterruptedException { 136 setLevelSerial(); 137 nullOwner3(); 138 } 139 140 public void nullOwner3() throws InterruptedException { 141 log("testNullOwner3 ..."); 142 try { 143 GlobalTransaction gtx = getGlobalTransactionFromThread(); 144 lock_.acquireWriteLock(gtx, 50); 145 lock_.acquireReadLock(gtx, 50); lock_.release(gtx); 147 } catch (LockingException e) { 148 fail(e.toString()); 149 } catch (TimeoutException e2) { 150 fail(e2.toString()); 151 } 152 } 153 154 public void testAcquireAndRelease_RWLock() throws InterruptedException { 155 setLevelRW(); 156 acquireAndRelease(); 157 } 158 159 public void testAcquireAndRelease_SimpleLock() throws InterruptedException { 160 setLevelSerial(); 161 acquireAndRelease(); 162 } 163 164 public void acquireAndRelease() throws InterruptedException { 165 log("testAcquireAndRelease ..."); 166 try { 167 lock_.acquireReadLock(this, 50); 168 assertTrue("Is the lock owner", lock_.isOwner(this)); 169 assertTrue(lock_.getReaderOwners().contains(this)); 170 171 lock_.acquireReadLock(this, 50); assertTrue("Is the lock owner", lock_.isOwner(this)); 173 assertTrue(lock_.getReaderOwners().contains(this)); 174 175 lock_.acquireWriteLock(this, 50); assertTrue("Is the lock owner", lock_.isOwner(this)); 177 assertTrue(!lock_.getReaderOwners().contains(this)); 178 assertTrue(lock_.getWriterOwner().equals(this)); 179 180 lock_.release(this); 181 assertTrue(!lock_.isOwner(this)); 182 } catch (LockingException e) { 183 fail(e.toString()); 184 } catch (TimeoutException e2) { 185 fail(e2.toString()); 186 } 187 } 188 189 public void acquireAndRelease2_RWLock() throws InterruptedException { 190 setLevelRW(); 191 log("testAcquireAndRelease2 ..."); 192 try { 193 lock_.acquireReadLock(this, 10); 194 } catch (LockingException e) { 195 fail(e.toString()); 196 } catch (TimeoutException e2) { 197 fail(e2.toString()); 198 } 199 200 try { 201 lock_.acquireReadLock(other_, 10); } catch (LockingException e) { 203 fail(e.toString()); 204 } catch (TimeoutException e2) { 205 fail(e2.toString()); 206 } 207 208 try { 209 lock_.acquireWriteLock(other_, 50); } catch (LockingException e) { 211 fail(e.toString()); 212 } catch (TimeoutException e2) { 213 assertTrue(true); 214 } 215 216 lock_.release(this); 217 218 try { 219 lock_.acquireWriteLock(other_, 10); } catch (LockingException e) { 221 fail(e.toString()); 222 } catch (TimeoutException e2) { 223 fail(e2.toString()); 224 } 225 226 lock_.releaseAll(); 227 } 228 229 230 public void acquireAndRelease2_SimpleLock() throws InterruptedException { 231 setLevelSerial(); 232 log("testAcquireAndRelease2 ..."); 233 try { 234 lock_.acquireReadLock(this, 10); 235 } catch (LockingException e) { 236 fail(e.toString()); 237 } catch (TimeoutException e2) { 238 fail(e2.toString()); 239 } 240 241 try { 242 lock_.acquireReadLock(other_, 10); fail("Acquire read lock for other. Should fail."); 244 } catch (LockingException e) { 245 fail(e.toString()); 246 } catch (TimeoutException e2) { 247 assertTrue(true); 249 } 250 251 try { 252 lock_.acquireWriteLock(other_, 50); fail("Acquire read lock for other. Should fail."); 254 } catch (LockingException e) { 255 fail(e.toString()); 256 } catch (TimeoutException e2) { 257 assertTrue(true); 258 } 259 260 lock_.release(this); 261 262 try { 263 lock_.acquireWriteLock(other_, 10); } catch (LockingException e) { 265 fail(e.toString()); 266 } catch (TimeoutException e2) { 267 fail(e2.toString()); 268 } 269 270 lock_.releaseAll(); 271 } 272 273 public void testThreadedAccess_RWLock() throws Throwable { 274 setLevelRW(); 275 log("testThreadedAccess_RWLock ..."); 276 final Object o1 = new Object (); 277 final Object o2 = new Object (); 278 279 System.out.println(""); 280 Thread t1 = new Thread () 282 { 283 public void run() 284 { 285 try { 286 log("o1 acquiring lock"); 287 lock_.acquireReadLock(o1, 50); 288 log("o1: OK"); 289 } catch (Throwable e) { 290 log("o1: FAIL"); 291 thread_ex=e; 292 } 293 } 294 }; 295 296 Thread t2 = new Thread () 298 { 299 public void run() 300 { 301 try { 302 log("o2 acquiring lock"); 303 lock_.acquireWriteLock(o2, 2000); 304 log("o2: OK"); 305 } catch (Throwable e) { 306 log("o2: FAIL"); 307 thread_ex=e; 308 } 309 } 310 }; 311 312 Thread t3 = new Thread () 314 { 315 public void run() 316 { 317 try { 318 log("o1 acquiring lock"); 319 lock_.acquireWriteLock(o1, 10); 320 log("o1: OK"); 321 } catch (Throwable e) { 322 log("o1: FAIL"); 323 thread_ex=e; 324 } 325 } 326 }; 327 328 t1.start(); 329 t2.start(); 330 sleep(1000); 331 332 assertTrue(lock_.isOwner(o1)); 334 sleep(100); 335 assertTrue(lock_.isOwner(o1)); 337 338 t3.start(); 339 sleep(100); 340 assertTrue(lock_.isOwner(o1)); 342 343 log("o1 releasing lock"); 345 lock_.release(o1); 346 log("o1: OK"); 347 348 sleep(200); 349 353 t1.join(20000); 354 t2.join(20000); 355 t3.join(20000); 356 if(thread_ex != null) 357 throw thread_ex; 358 } 359 360 361 public void testThreadedAccess_SimpleLock() throws Throwable { 362 setLevelSerial(); 363 log("testThreadedAccess_SimpleLock() ..."); 364 final Object o1 = new Object (); 365 final Object o2 = new Object (); 366 367 System.out.println(""); 368 Thread t1 = new Thread () 370 { 371 public void run() 372 { 373 try { 374 log("o1 acquiring lock"); 375 lock_.acquireReadLock(o1, 50); 376 log("o1: OK"); 377 } catch (Throwable e) { 378 log("o1: FAIL"); 379 thread_ex=e; 380 } 381 } 382 }; 383 384 Thread t2 = new Thread () 386 { 387 public void run() 388 { 389 try { 390 log("o2 acquiring lock"); 391 lock_.acquireWriteLock(o2, 2000); 392 log("o2: OK"); 393 } catch (Throwable e) { 394 log("o2: FAIL"); 395 thread_ex=e; 396 } 397 } 398 }; 399 400 Thread t3 = new Thread () 402 { 403 public void run() 404 { 405 try { 406 log("o1 acquiring lock"); 407 lock_.acquireWriteLock(o1, 10); 408 log("o1: OK"); 409 } catch (Throwable e) { 410 log("o1: FAIL"); 411 thread_ex=e; 412 } 413 } 414 }; 415 416 t1.start(); 417 t2.start(); 418 sleep(1000); 419 420 assertTrue(lock_.isOwner(o1)); 422 sleep(100); 423 assertTrue(lock_.isOwner(o1)); 425 426 t3.start(); 427 sleep(100); 428 assertTrue(lock_.isOwner(o1)); 430 431 log("o1 releasing lock"); 433 lock_.release(o1); 434 log("o1: OK"); 435 436 sleep(200); 437 441 t1.join(20000); 442 t2.join(20000); 443 t3.join(20000); 444 if(thread_ex != null) 445 throw thread_ex; 446 } 447 448 449 void sleep(long timeout) 450 { 451 try { 452 Thread.sleep(timeout); 453 } catch (InterruptedException e) { 454 } 455 } 456 457 void log(String msg) 458 { 459 logger_.info("-- [" + Thread.currentThread() + "]: " + msg); 461 } 462 463 public static Test suite() 464 { 465 TestSuite s = new TestSuite(IdentityLockTest.class); 466 return s; 467 } 468 469 public static void main(String [] args) 470 { 471 junit.textui.TestRunner.run(suite()); 472 } 473 474 } 475 | Popular Tags |