leosutic 2003/04/02 01:25:26
Modified: compatibility/src/test/org/apache/avalon/excalibur/concurrent/test
ReadWriteLockTestCase.java
Log:
Rewrote a test case to avoid a race-condition caused failure.
Revision Changes Path
1.8 +20 -16
avalon-excalibur/compatibility/src/test/org/apache/avalon/excalibur/concurrent/test/ReadWriteLockTestCase.java
Index: ReadWriteLockTestCase.java
===================================================================
RCS file:
/home/cvs/avalon-excalibur/compatibility/src/test/org/apache/avalon/excalibur/concurrent/test/ReadWriteLockTestCase.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ReadWriteLockTestCase.java 1 Apr 2003 07:34:36 -0000 1.7
+++ ReadWriteLockTestCase.java 2 Apr 2003 09:25:26 -0000 1.8
@@ -354,6 +354,16 @@
lock.tryAquireWrite() );
}
+ private static class TestReadWriteLock extends ReadWriteLock {
+ public int getNumReadLocksHeld() {
+ return super.getNumReadLocksHeld();
+ }
+
+ public int getNumWaitingForWrite() {
+ return super.getNumWaitingForWrite();
+ }
+ }
+
/**
* Tests a condition pointed out to me (L.Sutic) by Avi Drissman
* <a href="[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>. If you hold
@@ -364,31 +374,29 @@
*/
public void testDeadLock() throws Exception
{
- ReadWriteLock lock = new ReadWriteLock();
- TriesReadLock rla = new TriesReadLock( lock );
- TriesReadLock rlb = new TriesReadLock( lock );
+ TestReadWriteLock lock = new TestReadWriteLock();
TriesWriteLock wla = new TriesWriteLock( lock );
- TriesWriteLock wlb = new TriesWriteLock( lock );
//
// Grab a read lock.
//
- rla.start();
- Thread.sleep( 100 );
- assertTrue( rla.hasSuccess() );
+ assertTrue( lock.tryAcquireRead () );
//
// Try to grab a write lock. (The attempt stalls,
// because we are holding a read lock.)
//
wla.start();
- Thread.sleep( 100 );
- assertTrue( !wla.hasSuccess() );
+ while( lock.getNumWaitingForWrite() == 0 ) {
+ Thread.sleep( 100 );
+ }
+
//
// Interupt the thread waiting for the write lock...
//
wla.interrupt();
+ assertTrue( !wla.hasSuccess() );
//
// ...and release the read lock.
@@ -398,8 +406,8 @@
//
// Avoid race condition.
//
- rla.join();
wla.join();
+ assertTrue( !wla.hasSuccess() );
//
// Right, we are in the condition described by Drissman.
@@ -407,14 +415,10 @@
// Before the fix, the assertion immediately below
// would fail.
//
- rlb.start();
- Thread.sleep( 100 );
- assertTrue( rlb.hasSuccess() );
+ assertTrue( lock.tryAcquireRead () );
lock.release();
- wlb.start();
- Thread.sleep( 100 );
- assertTrue( wlb.hasSuccess() );
+ assertTrue( lock.tryAcquireWrite () );
lock.release();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]