1 8 package org.apache.avalon.excalibur.concurrent.test; 9 10 import junit.framework.TestCase; 11 import org.apache.avalon.excalibur.concurrent.ReadWriteLock; 12 13 18 public class ReadWriteLockTestCase 19 extends TestCase 20 { 21 26 static class TriesWriteLock 27 extends Thread 28 { 29 protected ReadWriteLock m_lock; 30 protected boolean m_success = false; 31 32 public TriesWriteLock(ReadWriteLock lock) 33 { 34 m_lock = lock; 35 } 36 37 public boolean hasSuccess() 38 { 39 return m_success; 40 } 41 42 public void run () 43 { 44 try 45 { 46 m_lock.aquireWrite(); 47 m_success = true; 48 } 49 catch (Exception e) 50 { 51 } 54 } 55 } 56 57 62 static class TriesReadLock 63 extends Thread 64 { 65 protected ReadWriteLock m_lock; 66 protected boolean m_success = false; 67 68 public TriesReadLock( final ReadWriteLock lock ) 69 { 70 m_lock = lock; 71 } 72 73 public boolean hasSuccess() 74 { 75 return m_success; 76 } 77 78 public void run () 79 { 80 try 81 { 82 m_lock.aquireRead(); 83 m_success = true; 84 } 85 catch( final Exception e ) 86 { 87 } 90 } 91 } 92 93 public ReadWriteLockTestCase (String name) { 94 super (name); 95 } 96 97 101 public void testRWLock() 102 throws Exception 103 { 104 final ReadWriteLock lock = new ReadWriteLock(); 105 final TriesWriteLock wl = new TriesWriteLock( lock ); 106 final TriesReadLock rl = new TriesReadLock( lock ); 107 108 rl.start (); 109 Thread.currentThread().sleep( 100 ); 110 assertTrue( "Attempted to aquire read lock.", rl.hasSuccess () ); 111 112 wl.start (); 113 Thread.currentThread().sleep( 100 ); 114 assertTrue( "Attempted to aquire write lock.", !wl.hasSuccess () ); 115 116 lock.release(); 117 118 Thread.currentThread().sleep( 100 ); 119 assertTrue( "Attempted to aquire write lock after releasing read lock.", 120 wl.hasSuccess () ); 121 122 lock.release(); 123 124 final TriesReadLock r2 = new TriesReadLock( lock ); 128 r2.start (); 129 Thread.currentThread().sleep( 100 ); 130 assertTrue( "Attempted to aquire read lock.", r2.hasSuccess () ); 131 132 lock.release(); 133 } 134 135 139 public void testIllegalState () throws Exception 140 { 141 ReadWriteLock lock = new ReadWriteLock(); 142 try 143 { 144 lock.release(); 145 fail( "ReadWriteLock did *not* signal illegal state when an attempt was made to release an unlocked lock." ); 146 } 147 catch (IllegalStateException ise) 148 { 149 } 151 } 152 153 158 public void testMultipleWriters () throws Exception 159 { 160 ReadWriteLock lock = new ReadWriteLock(); 161 TriesReadLock rla = new TriesReadLock( lock ); 162 TriesReadLock rlb = new TriesReadLock( lock ); 163 TriesWriteLock wla = new TriesWriteLock( lock ); 164 TriesWriteLock wlb = new TriesWriteLock( lock ); 165 166 rla.start (); 167 Thread.currentThread().sleep( 100 ); 168 assertTrue( "Attempted to aquire read lock.", rla.hasSuccess () ); 169 170 wla.start (); 171 wlb.start (); 172 173 Thread.currentThread().sleep( 100 ); 178 179 rlb.start (); 180 Thread.currentThread().sleep( 100 ); 181 182 assertTrue( "Attempted to aquire write lock, and succeeded even though it shouldn't be possible (rla has the lock).", 190 !wla.hasSuccess() && !wlb.hasSuccess() && !rlb.hasSuccess() ); 191 192 lock.release(); 197 Thread.currentThread().sleep( 100 ); 198 199 assertTrue( "Attempted to aquire write lock after releasing read lock.", 207 (wla.hasSuccess () || wlb.hasSuccess () ) && !rlb.hasSuccess () 208 && !(wla.hasSuccess () && wlb.hasSuccess () )); 209 210 lock.release(); 214 Thread.currentThread().sleep( 100 ); 215 216 assertTrue( "Attempted to aquire write lock after releasing read lock.", 223 wla.hasSuccess () && wlb.hasSuccess () && !rlb.hasSuccess () ); 224 225 lock.release(); 229 Thread.currentThread().sleep( 100 ); 230 assertTrue( "Attempted to aquire write lock after releasing read lock.", 231 wla.hasSuccess () && wlb.hasSuccess () && rlb.hasSuccess () ); 232 } 233 234 238 public void testMultipleReaders () throws Exception 239 { 240 ReadWriteLock lock = new ReadWriteLock(); 241 TriesReadLock rla = new TriesReadLock( lock ); 242 TriesReadLock rlb = new TriesReadLock( lock ); 243 TriesWriteLock wla = new TriesWriteLock( lock ); 244 245 rla.start (); 246 rlb.start (); 247 Thread.currentThread().sleep( 100 ); 248 assertTrue( "Attempted to aquire read multiple read locks.", 249 rla.hasSuccess () && rlb.hasSuccess () ); 250 251 wla.start (); 252 Thread.currentThread().sleep( 100 ); 253 assertTrue( "Write lock aquired even though read locks are held.", !wla.hasSuccess () ); 254 255 lock.release (); 256 Thread.currentThread().sleep( 100 ); 257 assertTrue( "Write lock aquired even though read locks are held. (There should be one read lock left)", 258 !wla.hasSuccess () ); 259 260 lock.release (); 261 Thread.currentThread().sleep( 100 ); 262 assertTrue( "Write lock not aquired even though lock should be released.", 263 wla.hasSuccess () ); 264 } 265 266 269 public void testTrying () throws Exception 270 { 271 ReadWriteLock lock = new ReadWriteLock(); 272 TriesReadLock rla = new TriesReadLock( lock ); 273 TriesReadLock rlb = new TriesReadLock( lock ); 274 TriesWriteLock wla = new TriesWriteLock( lock ); 275 TriesWriteLock wlb = new TriesWriteLock( lock ); 276 277 rla.start (); 282 Thread.currentThread().sleep( 100 ); 283 assertTrue( "Could not aquire a read lock.", rla.hasSuccess() ); 284 285 assertTrue( "Could not aquire a read lock, even though only a read lock is held.", 286 lock.tryAquireRead() ); 287 288 assertTrue( "Could aquire a write lock.", !lock.tryAquireWrite() ); 289 290 lock.release(); 294 lock.release(); 295 296 assertTrue( "Could not aquire a write lock.", lock.tryAquireWrite() ); 301 assertTrue( "Could aquire a read lock.", !lock.tryAquireRead() ); 302 assertTrue( "Could aquire a write lock.", !lock.tryAquireWrite() ); 303 304 lock.release(); 308 309 assertTrue( "Could not aquire a write lock after releasing the lock.", 310 lock.tryAquireWrite() ); 311 } 312 313 321 public void testDeadLock () throws Exception { 322 ReadWriteLock lock = new ReadWriteLock(); 323 TriesReadLock rla = new TriesReadLock( lock ); 324 TriesReadLock rlb = new TriesReadLock( lock ); 325 TriesWriteLock wla = new TriesWriteLock( lock ); 326 TriesWriteLock wlb = new TriesWriteLock( lock ); 327 328 rla.start(); 332 Thread.currentThread().sleep( 100 ); 333 assertTrue( rla.hasSuccess() ); 334 335 wla.start(); 340 Thread.currentThread().sleep( 100 ); 341 assertTrue( !wla.hasSuccess() ); 342 343 wla.interrupt(); 347 348 lock.release(); 352 353 rlb.start(); 360 Thread.currentThread().sleep( 100 ); 361 assertTrue( rlb.hasSuccess() ); 362 lock.release(); 363 364 wlb.start(); 365 Thread.currentThread().sleep( 100 ); 366 assertTrue( wlb.hasSuccess() ); 367 lock.release(); 368 } 369 } 370 371 372 | Popular Tags |