6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.concurrent.atomic.AtomicBoolean; |
11 |
– |
import java.util.concurrent.locks.*; |
12 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.io.*; |
11 |
< |
import java.util.*; |
10 |
> |
|
11 |
> |
import java.util.Arrays; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.HashSet; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
> |
import java.util.concurrent.locks.Condition; |
17 |
> |
import java.util.concurrent.locks.Lock; |
18 |
> |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
19 |
> |
|
20 |
> |
import junit.framework.AssertionFailedError; |
21 |
> |
import junit.framework.Test; |
22 |
> |
import junit.framework.TestSuite; |
23 |
|
|
24 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
25 |
|
public static void main(String[] args) { |
87 |
|
long startTime = System.nanoTime(); |
88 |
|
while (!lock.hasQueuedThread(t)) { |
89 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
90 |
< |
throw new AssertionError("timed out"); |
90 |
> |
throw new AssertionFailedError("timed out"); |
91 |
|
Thread.yield(); |
92 |
|
} |
93 |
|
assertTrue(t.isAlive()); |
94 |
< |
assertTrue(lock.getOwner() != t); |
94 |
> |
assertNotSame(t, lock.getOwner()); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
152 |
|
lock.writeLock().unlock(); |
153 |
|
} |
154 |
|
|
155 |
< |
enum AwaitMethod { await, awaitNanos, awaitUntil }; |
155 |
> |
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
156 |
|
|
157 |
|
/** |
158 |
|
* Awaits condition using the specified AwaitMethod. |
163 |
|
case await: |
164 |
|
c.await(); |
165 |
|
break; |
166 |
+ |
case awaitTimed: |
167 |
+ |
assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
168 |
+ |
break; |
169 |
|
case awaitNanos: |
170 |
|
long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
171 |
|
assertTrue(nanosRemaining > 0); |
514 |
|
|
515 |
|
/** |
516 |
|
* A thread that tries to acquire a fair read lock (non-reentrantly) |
517 |
< |
* will block if there is a waiting writer thread. |
517 |
> |
* will block if there is a waiting writer thread |
518 |
|
*/ |
519 |
|
public void testReaderWriterReaderFairFifo() { |
520 |
|
final PublicReentrantReadWriteLock lock = |
591 |
|
} |
592 |
|
|
593 |
|
/** |
594 |
< |
* Read trylock succeeds (barging) even in the presence of waiting readers and/or writers. |
594 |
> |
* Read trylock succeeds (barging) even in the presence of waiting |
595 |
> |
* readers and/or writers |
596 |
|
*/ |
597 |
|
public void testReadTryLockBarging() { testReadTryLockBarging(false); } |
598 |
|
public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); } |
877 |
|
public void testAwait_IMSE(boolean fair) { |
878 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
879 |
|
final Condition c = lock.writeLock().newCondition(); |
880 |
< |
long startTime = System.nanoTime(); |
881 |
< |
try { |
871 |
< |
try { |
872 |
< |
c.await(); |
873 |
< |
shouldThrow(); |
874 |
< |
} catch (IllegalMonitorStateException success) {} |
875 |
< |
try { |
876 |
< |
c.await(LONG_DELAY_MS, MILLISECONDS); |
877 |
< |
shouldThrow(); |
878 |
< |
} catch (IllegalMonitorStateException success) {} |
879 |
< |
try { |
880 |
< |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
881 |
< |
shouldThrow(); |
882 |
< |
} catch (IllegalMonitorStateException success) {} |
880 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
881 |
> |
long startTime = System.nanoTime(); |
882 |
|
try { |
883 |
< |
c.awaitUninterruptibly(); |
883 |
> |
await(c, awaitMethod); |
884 |
|
shouldThrow(); |
885 |
< |
} catch (IllegalMonitorStateException success) {} |
886 |
< |
} catch (InterruptedException ie) { |
887 |
< |
threadUnexpectedException(ie); |
885 |
> |
} catch (IllegalMonitorStateException success) { |
886 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
887 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
888 |
|
} |
890 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
889 |
|
} |
890 |
|
|
891 |
|
/** |
1011 |
|
} |
1012 |
|
|
1013 |
|
/** |
1014 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
1014 |
> |
* awaitUninterruptibly is uninterruptible |
1015 |
|
*/ |
1016 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
1017 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
1018 |
|
public void testAwaitUninterruptibly(boolean fair) { |
1019 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1020 |
|
final Condition c = lock.writeLock().newCondition(); |
1021 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
1022 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
1021 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
1022 |
> |
|
1023 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1024 |
|
public void realRun() { |
1025 |
+ |
// Interrupt before awaitUninterruptibly |
1026 |
|
lock.writeLock().lock(); |
1027 |
< |
locked.countDown(); |
1027 |
> |
pleaseInterrupt.countDown(); |
1028 |
> |
Thread.currentThread().interrupt(); |
1029 |
|
c.awaitUninterruptibly(); |
1030 |
|
assertTrue(Thread.interrupted()); |
1031 |
|
lock.writeLock().unlock(); |
1032 |
|
}}); |
1033 |
|
|
1034 |
< |
await(locked); |
1034 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1035 |
> |
public void realRun() { |
1036 |
> |
// Interrupt during awaitUninterruptibly |
1037 |
> |
lock.writeLock().lock(); |
1038 |
> |
pleaseInterrupt.countDown(); |
1039 |
> |
c.awaitUninterruptibly(); |
1040 |
> |
assertTrue(Thread.interrupted()); |
1041 |
> |
lock.writeLock().unlock(); |
1042 |
> |
}}); |
1043 |
> |
|
1044 |
> |
await(pleaseInterrupt); |
1045 |
|
lock.writeLock().lock(); |
1046 |
|
lock.writeLock().unlock(); |
1047 |
< |
t.interrupt(); |
1048 |
< |
long timeoutMillis = 10; |
1049 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
1047 |
> |
t2.interrupt(); |
1048 |
> |
|
1049 |
> |
assertThreadStaysAlive(t1); |
1050 |
> |
assertTrue(t2.isAlive()); |
1051 |
> |
|
1052 |
|
lock.writeLock().lock(); |
1053 |
< |
c.signal(); |
1053 |
> |
c.signalAll(); |
1054 |
|
lock.writeLock().unlock(); |
1055 |
< |
awaitTermination(t); |
1055 |
> |
|
1056 |
> |
awaitTermination(t1); |
1057 |
> |
awaitTermination(t2); |
1058 |
|
} |
1059 |
|
|
1060 |
|
/** |
1062 |
|
*/ |
1063 |
|
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
1064 |
|
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
1065 |
+ |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
1066 |
+ |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
1067 |
|
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
1068 |
|
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
1069 |
|
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
1101 |
|
*/ |
1102 |
|
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
1103 |
|
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
1104 |
+ |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
1105 |
+ |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
1106 |
|
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
1107 |
|
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
1108 |
|
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
1136 |
|
} |
1137 |
|
|
1138 |
|
/** |
1139 |
< |
* signal wakes up waiting threads in FIFO order. |
1139 |
> |
* signal wakes up waiting threads in FIFO order |
1140 |
|
*/ |
1141 |
|
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1142 |
|
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1297 |
|
} |
1298 |
|
|
1299 |
|
/** |
1300 |
< |
* hasQueuedThread reports whether a thread is queued. |
1300 |
> |
* hasQueuedThread reports whether a thread is queued |
1301 |
|
*/ |
1302 |
|
public void testHasQueuedThread() { testHasQueuedThread(false); } |
1303 |
|
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
1638 |
|
public void testToString_fair() { testToString(true); } |
1639 |
|
public void testToString(boolean fair) { |
1640 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1641 |
< |
String us = lock.toString(); |
1642 |
< |
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1643 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1644 |
< |
lock.writeLock().lock(); |
1645 |
< |
String ws = lock.toString(); |
1627 |
< |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1628 |
< |
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1641 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1642 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1643 |
> |
lock.writeLock().lock(); |
1644 |
> |
assertTrue(lock.toString().contains("Write locks = 1")); |
1645 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1646 |
|
lock.writeLock().unlock(); |
1647 |
|
lock.readLock().lock(); |
1648 |
|
lock.readLock().lock(); |
1649 |
< |
String rs = lock.toString(); |
1650 |
< |
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1634 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1649 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1650 |
> |
assertTrue(lock.toString().contains("Read locks = 2")); |
1651 |
|
} |
1652 |
|
|
1653 |
|
/** |
1657 |
|
public void testReadLockToString_fair() { testReadLockToString(true); } |
1658 |
|
public void testReadLockToString(boolean fair) { |
1659 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1660 |
< |
String us = lock.readLock().toString(); |
1645 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1660 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 0")); |
1661 |
|
lock.readLock().lock(); |
1662 |
|
lock.readLock().lock(); |
1663 |
< |
String rs = lock.readLock().toString(); |
1649 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1663 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 2")); |
1664 |
|
} |
1665 |
|
|
1666 |
|
/** |
1670 |
|
public void testWriteLockToString_fair() { testWriteLockToString(true); } |
1671 |
|
public void testWriteLockToString(boolean fair) { |
1672 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1673 |
< |
String us = lock.writeLock().toString(); |
1660 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1673 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1674 |
|
lock.writeLock().lock(); |
1675 |
< |
String ls = lock.writeLock().toString(); |
1676 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1675 |
> |
assertTrue(lock.writeLock().toString().contains("Locked")); |
1676 |
> |
lock.writeLock().unlock(); |
1677 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1678 |
|
} |
1679 |
|
|
1680 |
|
} |