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