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) { |
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); |
174 |
|
java.util.Date d = new java.util.Date(); |
175 |
|
assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); |
176 |
|
break; |
177 |
+ |
default: |
178 |
+ |
throw new AssertionError(); |
179 |
|
} |
180 |
|
} |
181 |
|
|
516 |
|
|
517 |
|
/** |
518 |
|
* A thread that tries to acquire a fair read lock (non-reentrantly) |
519 |
< |
* will block if there is a waiting writer thread. |
519 |
> |
* will block if there is a waiting writer thread |
520 |
|
*/ |
521 |
|
public void testReaderWriterReaderFairFifo() { |
522 |
|
final PublicReentrantReadWriteLock lock = |
593 |
|
} |
594 |
|
|
595 |
|
/** |
596 |
< |
* Read trylock succeeds (barging) even in the presence of waiting readers and/or writers. |
596 |
> |
* Read trylock succeeds (barging) even in the presence of waiting |
597 |
> |
* readers and/or writers |
598 |
|
*/ |
599 |
|
public void testReadTryLockBarging() { testReadTryLockBarging(false); } |
600 |
|
public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); } |
879 |
|
public void testAwait_IMSE(boolean fair) { |
880 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
881 |
|
final Condition c = lock.writeLock().newCondition(); |
882 |
< |
long startTime = System.nanoTime(); |
883 |
< |
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) {} |
882 |
> |
for (AwaitMethod awaitMethod : AwaitMethod.values()) { |
883 |
> |
long startTime = System.nanoTime(); |
884 |
|
try { |
885 |
< |
c.awaitUninterruptibly(); |
885 |
> |
await(c, awaitMethod); |
886 |
|
shouldThrow(); |
887 |
< |
} catch (IllegalMonitorStateException success) {} |
888 |
< |
} catch (InterruptedException ie) { |
889 |
< |
threadUnexpectedException(ie); |
887 |
> |
} catch (IllegalMonitorStateException success) { |
888 |
> |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
889 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
890 |
|
} |
890 |
– |
assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
891 |
|
} |
892 |
|
|
893 |
|
/** |
957 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
958 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
959 |
|
lock.writeLock().unlock(); |
960 |
< |
} catch (InterruptedException e) { |
961 |
< |
threadUnexpectedException(e); |
962 |
< |
} |
960 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
961 |
|
} |
962 |
|
|
963 |
|
/** |
977 |
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
978 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
979 |
|
lock.writeLock().unlock(); |
980 |
< |
} catch (InterruptedException e) { |
983 |
< |
threadUnexpectedException(e); |
984 |
< |
} |
980 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
981 |
|
} |
982 |
|
|
983 |
|
/** |
1009 |
|
} |
1010 |
|
|
1011 |
|
/** |
1012 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
1012 |
> |
* awaitUninterruptibly is uninterruptible |
1013 |
|
*/ |
1014 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
1015 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
1016 |
|
public void testAwaitUninterruptibly(boolean fair) { |
1017 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1018 |
|
final Condition c = lock.writeLock().newCondition(); |
1019 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
1020 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
1019 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
1020 |
> |
|
1021 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1022 |
|
public void realRun() { |
1023 |
+ |
// Interrupt before awaitUninterruptibly |
1024 |
|
lock.writeLock().lock(); |
1025 |
< |
locked.countDown(); |
1025 |
> |
pleaseInterrupt.countDown(); |
1026 |
> |
Thread.currentThread().interrupt(); |
1027 |
|
c.awaitUninterruptibly(); |
1028 |
|
assertTrue(Thread.interrupted()); |
1029 |
|
lock.writeLock().unlock(); |
1030 |
|
}}); |
1031 |
|
|
1032 |
< |
await(locked); |
1032 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1033 |
> |
public void realRun() { |
1034 |
> |
// Interrupt during awaitUninterruptibly |
1035 |
> |
lock.writeLock().lock(); |
1036 |
> |
pleaseInterrupt.countDown(); |
1037 |
> |
c.awaitUninterruptibly(); |
1038 |
> |
assertTrue(Thread.interrupted()); |
1039 |
> |
lock.writeLock().unlock(); |
1040 |
> |
}}); |
1041 |
> |
|
1042 |
> |
await(pleaseInterrupt); |
1043 |
|
lock.writeLock().lock(); |
1044 |
|
lock.writeLock().unlock(); |
1045 |
< |
t.interrupt(); |
1046 |
< |
long timeoutMillis = 10; |
1047 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
1045 |
> |
t2.interrupt(); |
1046 |
> |
|
1047 |
> |
assertThreadStaysAlive(t1); |
1048 |
> |
assertTrue(t2.isAlive()); |
1049 |
> |
|
1050 |
|
lock.writeLock().lock(); |
1051 |
< |
c.signal(); |
1051 |
> |
c.signalAll(); |
1052 |
|
lock.writeLock().unlock(); |
1053 |
< |
awaitTermination(t); |
1053 |
> |
|
1054 |
> |
awaitTermination(t1); |
1055 |
> |
awaitTermination(t2); |
1056 |
|
} |
1057 |
|
|
1058 |
|
/** |
1060 |
|
*/ |
1061 |
|
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); } |
1062 |
|
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); } |
1063 |
+ |
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); } |
1064 |
+ |
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); } |
1065 |
|
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); } |
1066 |
|
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); } |
1067 |
|
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); } |
1099 |
|
*/ |
1100 |
|
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); } |
1101 |
|
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); } |
1102 |
+ |
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); } |
1103 |
+ |
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); } |
1104 |
|
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); } |
1105 |
|
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); } |
1106 |
|
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); } |
1134 |
|
} |
1135 |
|
|
1136 |
|
/** |
1137 |
< |
* signal wakes up waiting threads in FIFO order. |
1137 |
> |
* signal wakes up waiting threads in FIFO order |
1138 |
|
*/ |
1139 |
|
public void testSignalWakesFifo() { testSignalWakesFifo(false); } |
1140 |
|
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); } |
1295 |
|
} |
1296 |
|
|
1297 |
|
/** |
1298 |
< |
* hasQueuedThread reports whether a thread is queued. |
1298 |
> |
* hasQueuedThread reports whether a thread is queued |
1299 |
|
*/ |
1300 |
|
public void testHasQueuedThread() { testHasQueuedThread(false); } |
1301 |
|
public void testHasQueuedThread_fair() { testHasQueuedThread(true); } |
1636 |
|
public void testToString_fair() { testToString(true); } |
1637 |
|
public void testToString(boolean fair) { |
1638 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1639 |
< |
String us = lock.toString(); |
1640 |
< |
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1641 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1642 |
< |
lock.writeLock().lock(); |
1643 |
< |
String ws = lock.toString(); |
1627 |
< |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1628 |
< |
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1639 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1640 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1641 |
> |
lock.writeLock().lock(); |
1642 |
> |
assertTrue(lock.toString().contains("Write locks = 1")); |
1643 |
> |
assertTrue(lock.toString().contains("Read locks = 0")); |
1644 |
|
lock.writeLock().unlock(); |
1645 |
|
lock.readLock().lock(); |
1646 |
|
lock.readLock().lock(); |
1647 |
< |
String rs = lock.toString(); |
1648 |
< |
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1634 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1647 |
> |
assertTrue(lock.toString().contains("Write locks = 0")); |
1648 |
> |
assertTrue(lock.toString().contains("Read locks = 2")); |
1649 |
|
} |
1650 |
|
|
1651 |
|
/** |
1655 |
|
public void testReadLockToString_fair() { testReadLockToString(true); } |
1656 |
|
public void testReadLockToString(boolean fair) { |
1657 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1658 |
< |
String us = lock.readLock().toString(); |
1645 |
< |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1658 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 0")); |
1659 |
|
lock.readLock().lock(); |
1660 |
|
lock.readLock().lock(); |
1661 |
< |
String rs = lock.readLock().toString(); |
1649 |
< |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1661 |
> |
assertTrue(lock.readLock().toString().contains("Read locks = 2")); |
1662 |
|
} |
1663 |
|
|
1664 |
|
/** |
1668 |
|
public void testWriteLockToString_fair() { testWriteLockToString(true); } |
1669 |
|
public void testWriteLockToString(boolean fair) { |
1670 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1671 |
< |
String us = lock.writeLock().toString(); |
1660 |
< |
assertTrue(us.indexOf("Unlocked") >= 0); |
1671 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1672 |
|
lock.writeLock().lock(); |
1673 |
< |
String ls = lock.writeLock().toString(); |
1674 |
< |
assertTrue(ls.indexOf("Locked") >= 0); |
1673 |
> |
assertTrue(lock.writeLock().toString().contains("Locked")); |
1674 |
> |
lock.writeLock().unlock(); |
1675 |
> |
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1676 |
|
} |
1677 |
|
|
1678 |
|
} |