21 |
|
import junit.framework.Test; |
22 |
|
import junit.framework.TestSuite; |
23 |
|
|
24 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
25 |
|
public class ReentrantReadWriteLockTest extends JSR166TestCase { |
26 |
|
public static void main(String[] args) { |
27 |
< |
junit.textui.TestRunner.run(suite()); |
27 |
> |
main(suite(), args); |
28 |
|
} |
29 |
|
public static Test suite() { |
30 |
|
return new TestSuite(ReentrantReadWriteLockTest.class); |
156 |
|
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil } |
157 |
|
|
158 |
|
/** |
159 |
< |
* Awaits condition using the specified AwaitMethod. |
159 |
> |
* Awaits condition "indefinitely" using the specified AwaitMethod. |
160 |
|
*/ |
161 |
|
void await(Condition c, AwaitMethod awaitMethod) |
162 |
|
throws InterruptedException { |
163 |
+ |
long timeoutMillis = 2 * LONG_DELAY_MS; |
164 |
|
switch (awaitMethod) { |
165 |
|
case await: |
166 |
|
c.await(); |
167 |
|
break; |
168 |
|
case awaitTimed: |
169 |
< |
assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
169 |
> |
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
170 |
|
break; |
171 |
|
case awaitNanos: |
172 |
< |
long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
173 |
< |
assertTrue(nanosRemaining > 0); |
172 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
173 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
174 |
> |
assertTrue(nanosRemaining > timeoutNanos / 2); |
175 |
> |
assertTrue(nanosRemaining <= timeoutNanos); |
176 |
|
break; |
177 |
|
case awaitUntil: |
178 |
< |
java.util.Date d = new java.util.Date(); |
175 |
< |
assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS))); |
178 |
> |
assertTrue(c.awaitUntil(delayedDate(timeoutMillis))); |
179 |
|
break; |
180 |
|
default: |
181 |
|
throw new AssertionError(); |
232 |
|
public void testGetWriteHoldCount() { testGetWriteHoldCount(false); } |
233 |
|
public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); } |
234 |
|
public void testGetWriteHoldCount(boolean fair) { |
235 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
235 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
236 |
|
for (int i = 1; i <= SIZE; i++) { |
237 |
|
lock.writeLock().lock(); |
238 |
|
assertEquals(i,lock.getWriteHoldCount()); |
239 |
|
} |
240 |
|
for (int i = SIZE; i > 0; i--) { |
241 |
|
lock.writeLock().unlock(); |
242 |
< |
assertEquals(i-1,lock.getWriteHoldCount()); |
242 |
> |
assertEquals(i - 1,lock.getWriteHoldCount()); |
243 |
|
} |
244 |
|
} |
245 |
|
|
249 |
|
public void testGetHoldCount() { testGetHoldCount(false); } |
250 |
|
public void testGetHoldCount_fair() { testGetHoldCount(true); } |
251 |
|
public void testGetHoldCount(boolean fair) { |
252 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
252 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
253 |
|
for (int i = 1; i <= SIZE; i++) { |
254 |
|
lock.writeLock().lock(); |
255 |
|
assertEquals(i,lock.writeLock().getHoldCount()); |
256 |
|
} |
257 |
|
for (int i = SIZE; i > 0; i--) { |
258 |
|
lock.writeLock().unlock(); |
259 |
< |
assertEquals(i-1,lock.writeLock().getHoldCount()); |
259 |
> |
assertEquals(i - 1,lock.writeLock().getHoldCount()); |
260 |
|
} |
261 |
|
} |
262 |
|
|
266 |
|
public void testGetReadHoldCount() { testGetReadHoldCount(false); } |
267 |
|
public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); } |
268 |
|
public void testGetReadHoldCount(boolean fair) { |
269 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
269 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
270 |
|
for (int i = 1; i <= SIZE; i++) { |
271 |
|
lock.readLock().lock(); |
272 |
|
assertEquals(i,lock.getReadHoldCount()); |
273 |
|
} |
274 |
|
for (int i = SIZE; i > 0; i--) { |
275 |
|
lock.readLock().unlock(); |
276 |
< |
assertEquals(i-1,lock.getReadHoldCount()); |
276 |
> |
assertEquals(i - 1,lock.getReadHoldCount()); |
277 |
|
} |
278 |
|
} |
279 |
|
|
283 |
|
public void testWriteUnlock_IMSE() { testWriteUnlock_IMSE(false); } |
284 |
|
public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); } |
285 |
|
public void testWriteUnlock_IMSE(boolean fair) { |
286 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
286 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
287 |
|
try { |
288 |
|
lock.writeLock().unlock(); |
289 |
|
shouldThrow(); |
296 |
|
public void testReadUnlock_IMSE() { testReadUnlock_IMSE(false); } |
297 |
|
public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); } |
298 |
|
public void testReadUnlock_IMSE(boolean fair) { |
299 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
299 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
300 |
|
try { |
301 |
|
lock.readLock().unlock(); |
302 |
|
shouldThrow(); |
789 |
|
public void testWriteTryLock_Timeout(boolean fair) { |
790 |
|
final PublicReentrantReadWriteLock lock = |
791 |
|
new PublicReentrantReadWriteLock(fair); |
792 |
+ |
final long timeoutMillis = timeoutMillis(); |
793 |
|
lock.writeLock().lock(); |
794 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
795 |
|
public void realRun() throws InterruptedException { |
796 |
|
long startTime = System.nanoTime(); |
793 |
– |
long timeoutMillis = 10; |
797 |
|
assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS)); |
798 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
799 |
|
}}); |
813 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
814 |
|
public void realRun() throws InterruptedException { |
815 |
|
long startTime = System.nanoTime(); |
816 |
< |
long timeoutMillis = 10; |
816 |
> |
long timeoutMillis = timeoutMillis(); |
817 |
|
assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS)); |
818 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
819 |
|
}}); |
833 |
|
new PublicReentrantReadWriteLock(fair); |
834 |
|
try { |
835 |
|
lock.writeLock().lockInterruptibly(); |
836 |
< |
} catch (InterruptedException ie) { |
834 |
< |
threadUnexpectedException(ie); |
835 |
< |
} |
836 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
837 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
838 |
|
public void realRun() throws InterruptedException { |
839 |
|
lock.writeLock().lockInterruptibly(); |
858 |
|
lock.readLock().lockInterruptibly(); |
859 |
|
lock.readLock().unlock(); |
860 |
|
lock.writeLock().lockInterruptibly(); |
861 |
< |
} catch (InterruptedException ie) { |
861 |
< |
threadUnexpectedException(ie); |
862 |
< |
} |
861 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
862 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
863 |
|
public void realRun() throws InterruptedException { |
864 |
|
lock.readLock().lockInterruptibly(); |
884 |
|
await(c, awaitMethod); |
885 |
|
shouldThrow(); |
886 |
|
} catch (IllegalMonitorStateException success) { |
887 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
887 |
> |
} catch (InterruptedException fail) { |
888 |
> |
threadUnexpectedException(fail); |
889 |
> |
} |
890 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
891 |
|
} |
892 |
|
} |
925 |
|
public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); } |
926 |
|
public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); } |
927 |
|
public void testAwaitNanos_Timeout(boolean fair) { |
928 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
929 |
+ |
final Condition c = lock.writeLock().newCondition(); |
930 |
+ |
final long timeoutMillis = timeoutMillis(); |
931 |
+ |
lock.writeLock().lock(); |
932 |
+ |
final long startTime = System.nanoTime(); |
933 |
+ |
final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
934 |
|
try { |
928 |
– |
final ReentrantReadWriteLock lock = |
929 |
– |
new ReentrantReadWriteLock(fair); |
930 |
– |
final Condition c = lock.writeLock().newCondition(); |
931 |
– |
lock.writeLock().lock(); |
932 |
– |
long startTime = System.nanoTime(); |
933 |
– |
long timeoutMillis = 10; |
934 |
– |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
935 |
|
long nanosRemaining = c.awaitNanos(timeoutNanos); |
936 |
|
assertTrue(nanosRemaining <= 0); |
937 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
938 |
< |
lock.writeLock().unlock(); |
939 |
< |
} catch (InterruptedException e) { |
940 |
< |
threadUnexpectedException(e); |
941 |
< |
} |
937 |
> |
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
938 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
939 |
> |
lock.writeLock().unlock(); |
940 |
|
} |
941 |
|
|
942 |
|
/** |
945 |
|
public void testAwait_Timeout() { testAwait_Timeout(false); } |
946 |
|
public void testAwait_Timeout_fair() { testAwait_Timeout(true); } |
947 |
|
public void testAwait_Timeout(boolean fair) { |
948 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
949 |
+ |
final Condition c = lock.writeLock().newCondition(); |
950 |
+ |
final long timeoutMillis = timeoutMillis(); |
951 |
+ |
lock.writeLock().lock(); |
952 |
+ |
final long startTime = System.nanoTime(); |
953 |
|
try { |
951 |
– |
final ReentrantReadWriteLock lock = |
952 |
– |
new ReentrantReadWriteLock(fair); |
953 |
– |
final Condition c = lock.writeLock().newCondition(); |
954 |
– |
lock.writeLock().lock(); |
955 |
– |
long startTime = System.nanoTime(); |
956 |
– |
long timeoutMillis = 10; |
954 |
|
assertFalse(c.await(timeoutMillis, MILLISECONDS)); |
958 |
– |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
959 |
– |
lock.writeLock().unlock(); |
955 |
|
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
956 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
957 |
+ |
lock.writeLock().unlock(); |
958 |
|
} |
959 |
|
|
960 |
|
/** |
963 |
|
public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); } |
964 |
|
public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); } |
965 |
|
public void testAwaitUntil_Timeout(boolean fair) { |
966 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
967 |
+ |
final Condition c = lock.writeLock().newCondition(); |
968 |
+ |
lock.writeLock().lock(); |
969 |
+ |
// We shouldn't assume that nanoTime and currentTimeMillis |
970 |
+ |
// use the same time source, so don't use nanoTime here. |
971 |
+ |
final java.util.Date delayedDate = delayedDate(timeoutMillis()); |
972 |
|
try { |
973 |
< |
final ReentrantReadWriteLock lock = |
971 |
< |
new ReentrantReadWriteLock(fair); |
972 |
< |
final Condition c = lock.writeLock().newCondition(); |
973 |
< |
lock.writeLock().lock(); |
974 |
< |
long startTime = System.nanoTime(); |
975 |
< |
long timeoutMillis = 10; |
976 |
< |
java.util.Date d = new java.util.Date(); |
977 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
978 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
979 |
< |
lock.writeLock().unlock(); |
973 |
> |
assertFalse(c.awaitUntil(delayedDate)); |
974 |
|
} catch (InterruptedException fail) { threadUnexpectedException(fail); } |
975 |
+ |
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime()); |
976 |
+ |
lock.writeLock().unlock(); |
977 |
|
} |
978 |
|
|
979 |
|
/** |
1010 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
1011 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
1012 |
|
public void testAwaitUninterruptibly(boolean fair) { |
1013 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1014 |
< |
final Condition c = lock.writeLock().newCondition(); |
1013 |
> |
final Lock lock = new ReentrantReadWriteLock(fair).writeLock(); |
1014 |
> |
final Condition condition = lock.newCondition(); |
1015 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
1016 |
|
|
1017 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1018 |
|
public void realRun() { |
1019 |
|
// Interrupt before awaitUninterruptibly |
1020 |
< |
lock.writeLock().lock(); |
1020 |
> |
lock.lock(); |
1021 |
|
pleaseInterrupt.countDown(); |
1022 |
|
Thread.currentThread().interrupt(); |
1023 |
< |
c.awaitUninterruptibly(); |
1023 |
> |
condition.awaitUninterruptibly(); |
1024 |
|
assertTrue(Thread.interrupted()); |
1025 |
< |
lock.writeLock().unlock(); |
1025 |
> |
lock.unlock(); |
1026 |
|
}}); |
1027 |
|
|
1028 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
1029 |
|
public void realRun() { |
1030 |
|
// Interrupt during awaitUninterruptibly |
1031 |
< |
lock.writeLock().lock(); |
1031 |
> |
lock.lock(); |
1032 |
|
pleaseInterrupt.countDown(); |
1033 |
< |
c.awaitUninterruptibly(); |
1033 |
> |
condition.awaitUninterruptibly(); |
1034 |
|
assertTrue(Thread.interrupted()); |
1035 |
< |
lock.writeLock().unlock(); |
1035 |
> |
lock.unlock(); |
1036 |
|
}}); |
1037 |
|
|
1038 |
|
await(pleaseInterrupt); |
1043 |
– |
lock.writeLock().lock(); |
1044 |
– |
lock.writeLock().unlock(); |
1039 |
|
t2.interrupt(); |
1040 |
< |
|
1041 |
< |
assertThreadStaysAlive(t1); |
1042 |
< |
assertTrue(t2.isAlive()); |
1043 |
< |
|
1044 |
< |
lock.writeLock().lock(); |
1045 |
< |
c.signalAll(); |
1046 |
< |
lock.writeLock().unlock(); |
1040 |
> |
lock.lock(); |
1041 |
> |
lock.unlock(); |
1042 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
1043 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
1044 |
> |
|
1045 |
> |
lock.lock(); |
1046 |
> |
condition.signalAll(); |
1047 |
> |
lock.unlock(); |
1048 |
|
|
1049 |
|
awaitTermination(t1); |
1050 |
|
awaitTermination(t2); |
1228 |
|
public void testSerialization() { testSerialization(false); } |
1229 |
|
public void testSerialization_fair() { testSerialization(true); } |
1230 |
|
public void testSerialization(boolean fair) { |
1231 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1231 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1232 |
|
lock.writeLock().lock(); |
1233 |
|
lock.readLock().lock(); |
1234 |
|
|
1630 |
|
public void testToString() { testToString(false); } |
1631 |
|
public void testToString_fair() { testToString(true); } |
1632 |
|
public void testToString(boolean fair) { |
1633 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1633 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1634 |
|
assertTrue(lock.toString().contains("Write locks = 0")); |
1635 |
|
assertTrue(lock.toString().contains("Read locks = 0")); |
1636 |
|
lock.writeLock().lock(); |
1637 |
|
assertTrue(lock.toString().contains("Write locks = 1")); |
1638 |
|
assertTrue(lock.toString().contains("Read locks = 0")); |
1639 |
+ |
lock.writeLock().lock(); |
1640 |
+ |
assertTrue(lock.toString().contains("Write locks = 2")); |
1641 |
+ |
assertTrue(lock.toString().contains("Read locks = 0")); |
1642 |
+ |
lock.writeLock().unlock(); |
1643 |
|
lock.writeLock().unlock(); |
1644 |
|
lock.readLock().lock(); |
1645 |
+ |
assertTrue(lock.toString().contains("Write locks = 0")); |
1646 |
+ |
assertTrue(lock.toString().contains("Read locks = 1")); |
1647 |
|
lock.readLock().lock(); |
1648 |
|
assertTrue(lock.toString().contains("Write locks = 0")); |
1649 |
|
assertTrue(lock.toString().contains("Read locks = 2")); |
1655 |
|
public void testReadLockToString() { testReadLockToString(false); } |
1656 |
|
public void testReadLockToString_fair() { testReadLockToString(true); } |
1657 |
|
public void testReadLockToString(boolean fair) { |
1658 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1658 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1659 |
|
assertTrue(lock.readLock().toString().contains("Read locks = 0")); |
1660 |
|
lock.readLock().lock(); |
1661 |
+ |
assertTrue(lock.readLock().toString().contains("Read locks = 1")); |
1662 |
|
lock.readLock().lock(); |
1663 |
|
assertTrue(lock.readLock().toString().contains("Read locks = 2")); |
1664 |
+ |
lock.readLock().unlock(); |
1665 |
+ |
assertTrue(lock.readLock().toString().contains("Read locks = 1")); |
1666 |
+ |
lock.readLock().unlock(); |
1667 |
+ |
assertTrue(lock.readLock().toString().contains("Read locks = 0")); |
1668 |
|
} |
1669 |
|
|
1670 |
|
/** |
1673 |
|
public void testWriteLockToString() { testWriteLockToString(false); } |
1674 |
|
public void testWriteLockToString_fair() { testWriteLockToString(true); } |
1675 |
|
public void testWriteLockToString(boolean fair) { |
1676 |
< |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1676 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair); |
1677 |
|
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1678 |
|
lock.writeLock().lock(); |
1679 |
< |
assertTrue(lock.writeLock().toString().contains("Locked")); |
1679 |
> |
assertTrue(lock.writeLock().toString().contains("Locked by")); |
1680 |
|
lock.writeLock().unlock(); |
1681 |
|
assertTrue(lock.writeLock().toString().contains("Unlocked")); |
1682 |
|
} |