ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/StampedLockTest.java
Revision: 1.3
Committed: Sat Feb 9 19:33:08 2013 UTC (11 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +3 -1 lines
Log Message:
nag tck maintainers

File Contents

# Content
1 /*
2 * Written by Doug Lea and Martin Buchholz
3 * with assistance from members of JCP JSR-166 Expert Group and
4 * released to the public domain, as explained at
5 * http://creativecommons.org/publicdomain/zero/1.0/
6 * Other contributors include Andrew Wright, Jeffrey Hayes,
7 * Pat Fisher, Mike Judd.
8 */
9
10 import junit.framework.*;
11 import java.util.concurrent.atomic.AtomicBoolean;
12 import java.util.concurrent.locks.Condition;
13 import java.util.concurrent.locks.Lock;
14 import java.util.concurrent.locks.ReadWriteLock;
15 import java.util.concurrent.locks.StampedLock;
16 import java.util.concurrent.CountDownLatch;
17 import static java.util.concurrent.TimeUnit.MILLISECONDS;
18 import java.util.*;
19
20 public class StampedLockTest extends JSR166TestCase {
21 public static void main(String[] args) {
22 junit.textui.TestRunner.run(suite());
23 }
24 public static Test suite() {
25 return new TestSuite(StampedLockTest.class);
26 }
27
28 // XXXX Just a skeleton implementation for now.
29 public void testTODO() {
30 fail("Please add some real tests!");
31 }
32
33 public void testConstructor() { new StampedLock(); }
34
35 // /**
36 // * A runnable calling lockInterruptibly
37 // */
38 // class InterruptibleLockRunnable extends CheckedRunnable {
39 // final ReentrantReadWriteLock lock;
40 // InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; }
41 // public void realRun() throws InterruptedException {
42 // lock.writeLock().lockInterruptibly();
43 // }
44 // }
45
46 // /**
47 // * A runnable calling lockInterruptibly that expects to be
48 // * interrupted
49 // */
50 // class InterruptedLockRunnable extends CheckedInterruptedRunnable {
51 // final ReentrantReadWriteLock lock;
52 // InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; }
53 // public void realRun() throws InterruptedException {
54 // lock.writeLock().lockInterruptibly();
55 // }
56 // }
57
58 // /**
59 // * Subclass to expose protected methods
60 // */
61 // static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock {
62 // PublicReentrantReadWriteLock() { super(); }
63 // PublicReentrantReadWriteLock(boolean fair) { super(fair); }
64 // public Thread getOwner() {
65 // return super.getOwner();
66 // }
67 // public Collection<Thread> getQueuedThreads() {
68 // return super.getQueuedThreads();
69 // }
70 // public Collection<Thread> getWaitingThreads(Condition c) {
71 // return super.getWaitingThreads(c);
72 // }
73 // }
74
75 // /**
76 // * Releases write lock, checking that it had a hold count of 1.
77 // */
78 // void releaseWriteLock(PublicReentrantReadWriteLock lock) {
79 // ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
80 // assertWriteLockedByMoi(lock);
81 // assertEquals(1, lock.getWriteHoldCount());
82 // writeLock.unlock();
83 // assertNotWriteLocked(lock);
84 // }
85
86 // /**
87 // * Spin-waits until lock.hasQueuedThread(t) becomes true.
88 // */
89 // void waitForQueuedThread(PublicReentrantReadWriteLock lock, Thread t) {
90 // long startTime = System.nanoTime();
91 // while (!lock.hasQueuedThread(t)) {
92 // if (millisElapsedSince(startTime) > LONG_DELAY_MS)
93 // throw new AssertionFailedError("timed out");
94 // Thread.yield();
95 // }
96 // assertTrue(t.isAlive());
97 // assertTrue(lock.getOwner() != t);
98 // }
99
100 // /**
101 // * Checks that lock is not write-locked.
102 // */
103 // void assertNotWriteLocked(PublicReentrantReadWriteLock lock) {
104 // assertFalse(lock.isWriteLocked());
105 // assertFalse(lock.isWriteLockedByCurrentThread());
106 // assertFalse(lock.writeLock().isHeldByCurrentThread());
107 // assertEquals(0, lock.getWriteHoldCount());
108 // assertEquals(0, lock.writeLock().getHoldCount());
109 // assertNull(lock.getOwner());
110 // }
111
112 // /**
113 // * Checks that lock is write-locked by the given thread.
114 // */
115 // void assertWriteLockedBy(PublicReentrantReadWriteLock lock, Thread t) {
116 // assertTrue(lock.isWriteLocked());
117 // assertSame(t, lock.getOwner());
118 // assertEquals(t == Thread.currentThread(),
119 // lock.isWriteLockedByCurrentThread());
120 // assertEquals(t == Thread.currentThread(),
121 // lock.writeLock().isHeldByCurrentThread());
122 // assertEquals(t == Thread.currentThread(),
123 // lock.getWriteHoldCount() > 0);
124 // assertEquals(t == Thread.currentThread(),
125 // lock.writeLock().getHoldCount() > 0);
126 // assertEquals(0, lock.getReadLockCount());
127 // }
128
129 // /**
130 // * Checks that lock is write-locked by the current thread.
131 // */
132 // void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
133 // assertWriteLockedBy(lock, Thread.currentThread());
134 // }
135
136 // /**
137 // * Checks that condition c has no waiters.
138 // */
139 // void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
140 // assertHasWaiters(lock, c, new Thread[] {});
141 // }
142
143 // /**
144 // * Checks that condition c has exactly the given waiter threads.
145 // */
146 // void assertHasWaiters(PublicReentrantReadWriteLock lock, Condition c,
147 // Thread... threads) {
148 // lock.writeLock().lock();
149 // assertEquals(threads.length > 0, lock.hasWaiters(c));
150 // assertEquals(threads.length, lock.getWaitQueueLength(c));
151 // assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
152 // assertEquals(threads.length, lock.getWaitingThreads(c).size());
153 // assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
154 // new HashSet<Thread>(Arrays.asList(threads)));
155 // lock.writeLock().unlock();
156 // }
157
158 // enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
159
160 // /**
161 // * Awaits condition using the specified AwaitMethod.
162 // */
163 // void await(Condition c, AwaitMethod awaitMethod)
164 // throws InterruptedException {
165 // switch (awaitMethod) {
166 // case await:
167 // c.await();
168 // break;
169 // case awaitTimed:
170 // assertTrue(c.await(2 * LONG_DELAY_MS, MILLISECONDS));
171 // break;
172 // case awaitNanos:
173 // long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
174 // assertTrue(nanosRemaining > 0);
175 // break;
176 // case awaitUntil:
177 // java.util.Date d = new java.util.Date();
178 // assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
179 // break;
180 // }
181 // }
182
183 // /**
184 // * Constructor sets given fairness, and is in unlocked state
185 // */
186 // public void testConstructor() {
187 // PublicReentrantReadWriteLock lock;
188
189 // lock = new PublicReentrantReadWriteLock();
190 // assertFalse(lock.isFair());
191 // assertNotWriteLocked(lock);
192 // assertEquals(0, lock.getReadLockCount());
193
194 // lock = new PublicReentrantReadWriteLock(true);
195 // assertTrue(lock.isFair());
196 // assertNotWriteLocked(lock);
197 // assertEquals(0, lock.getReadLockCount());
198
199 // lock = new PublicReentrantReadWriteLock(false);
200 // assertFalse(lock.isFair());
201 // assertNotWriteLocked(lock);
202 // assertEquals(0, lock.getReadLockCount());
203 // }
204
205 // /**
206 // * write-locking and read-locking an unlocked lock succeed
207 // */
208 // public void testLock() { testLock(false); }
209 // public void testLock_fair() { testLock(true); }
210 // public void testLock(boolean fair) {
211 // PublicReentrantReadWriteLock lock =
212 // new PublicReentrantReadWriteLock(fair);
213 // assertNotWriteLocked(lock);
214 // lock.writeLock().lock();
215 // assertWriteLockedByMoi(lock);
216 // lock.writeLock().unlock();
217 // assertNotWriteLocked(lock);
218 // assertEquals(0, lock.getReadLockCount());
219 // lock.readLock().lock();
220 // assertNotWriteLocked(lock);
221 // assertEquals(1, lock.getReadLockCount());
222 // lock.readLock().unlock();
223 // assertNotWriteLocked(lock);
224 // assertEquals(0, lock.getReadLockCount());
225 // }
226
227 // /**
228 // * getWriteHoldCount returns number of recursive holds
229 // */
230 // public void testGetWriteHoldCount() { testGetWriteHoldCount(false); }
231 // public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); }
232 // public void testGetWriteHoldCount(boolean fair) {
233 // ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
234 // for (int i = 1; i <= SIZE; i++) {
235 // lock.writeLock().lock();
236 // assertEquals(i,lock.getWriteHoldCount());
237 // }
238 // for (int i = SIZE; i > 0; i--) {
239 // lock.writeLock().unlock();
240 // assertEquals(i-1,lock.getWriteHoldCount());
241 // }
242 // }
243
244 // /**
245 // * writelock.getHoldCount returns number of recursive holds
246 // */
247 // public void testGetHoldCount() { testGetHoldCount(false); }
248 // public void testGetHoldCount_fair() { testGetHoldCount(true); }
249 // public void testGetHoldCount(boolean fair) {
250 // ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
251 // for (int i = 1; i <= SIZE; i++) {
252 // lock.writeLock().lock();
253 // assertEquals(i,lock.writeLock().getHoldCount());
254 // }
255 // for (int i = SIZE; i > 0; i--) {
256 // lock.writeLock().unlock();
257 // assertEquals(i-1,lock.writeLock().getHoldCount());
258 // }
259 // }
260
261 // /**
262 // * getReadHoldCount returns number of recursive holds
263 // */
264 // public void testGetReadHoldCount() { testGetReadHoldCount(false); }
265 // public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); }
266 // public void testGetReadHoldCount(boolean fair) {
267 // ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
268 // for (int i = 1; i <= SIZE; i++) {
269 // lock.readLock().lock();
270 // assertEquals(i,lock.getReadHoldCount());
271 // }
272 // for (int i = SIZE; i > 0; i--) {
273 // lock.readLock().unlock();
274 // assertEquals(i-1,lock.getReadHoldCount());
275 // }
276 // }
277
278 // /**
279 // * write-unlocking an unlocked lock throws IllegalMonitorStateException
280 // */
281 // public void testWriteUnlock_IMSE() { testWriteUnlock_IMSE(false); }
282 // public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); }
283 // public void testWriteUnlock_IMSE(boolean fair) {
284 // ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
285 // try {
286 // lock.writeLock().unlock();
287 // shouldThrow();
288 // } catch (IllegalMonitorStateException success) {}
289 // }
290
291 // /**
292 // * read-unlocking an unlocked lock throws IllegalMonitorStateException
293 // */
294 // public void testReadUnlock_IMSE() { testReadUnlock_IMSE(false); }
295 // public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); }
296 // public void testReadUnlock_IMSE(boolean fair) {
297 // ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
298 // try {
299 // lock.readLock().unlock();
300 // shouldThrow();
301 // } catch (IllegalMonitorStateException success) {}
302 // }
303
304 // /**
305 // * write-lockInterruptibly is interruptible
306 // */
307 // public void testWriteLockInterruptibly_Interruptible() { testWriteLockInterruptibly_Interruptible(false); }
308 // public void testWriteLockInterruptibly_Interruptible_fair() { testWriteLockInterruptibly_Interruptible(true); }
309 // public void testWriteLockInterruptibly_Interruptible(boolean fair) {
310 // final PublicReentrantReadWriteLock lock =
311 // new PublicReentrantReadWriteLock(fair);
312 // lock.writeLock().lock();
313 // Thread t = newStartedThread(new CheckedInterruptedRunnable() {
314 // public void realRun() throws InterruptedException {
315 // lock.writeLock().lockInterruptibly();
316 // }});
317
318 // waitForQueuedThread(lock, t);
319 // t.interrupt();
320 // awaitTermination(t);
321 // releaseWriteLock(lock);
322 // }
323
324 // /**
325 // * timed write-tryLock is interruptible
326 // */
327 // public void testWriteTryLock_Interruptible() { testWriteTryLock_Interruptible(false); }
328 // public void testWriteTryLock_Interruptible_fair() { testWriteTryLock_Interruptible(true); }
329 // public void testWriteTryLock_Interruptible(boolean fair) {
330 // final PublicReentrantReadWriteLock lock =
331 // new PublicReentrantReadWriteLock(fair);
332 // lock.writeLock().lock();
333 // Thread t = newStartedThread(new CheckedInterruptedRunnable() {
334 // public void realRun() throws InterruptedException {
335 // lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
336 // }});
337
338 // waitForQueuedThread(lock, t);
339 // t.interrupt();
340 // awaitTermination(t);
341 // releaseWriteLock(lock);
342 // }
343
344 // /**
345 // * read-lockInterruptibly is interruptible
346 // */
347 // public void testReadLockInterruptibly_Interruptible() { testReadLockInterruptibly_Interruptible(false); }
348 // public void testReadLockInterruptibly_Interruptible_fair() { testReadLockInterruptibly_Interruptible(true); }
349 // public void testReadLockInterruptibly_Interruptible(boolean fair) {
350 // final PublicReentrantReadWriteLock lock =
351 // new PublicReentrantReadWriteLock(fair);
352 // lock.writeLock().lock();
353 // Thread t = newStartedThread(new CheckedInterruptedRunnable() {
354 // public void realRun() throws InterruptedException {
355 // lock.readLock().lockInterruptibly();
356 // }});
357
358 // waitForQueuedThread(lock, t);
359 // t.interrupt();
360 // awaitTermination(t);
361 // releaseWriteLock(lock);
362 // }
363
364 // /**
365 // * timed read-tryLock is interruptible
366 // */
367 // public void testReadTryLock_Interruptible() { testReadTryLock_Interruptible(false); }
368 // public void testReadTryLock_Interruptible_fair() { testReadTryLock_Interruptible(true); }
369 // public void testReadTryLock_Interruptible(boolean fair) {
370 // final PublicReentrantReadWriteLock lock =
371 // new PublicReentrantReadWriteLock(fair);
372 // lock.writeLock().lock();
373 // Thread t = newStartedThread(new CheckedInterruptedRunnable() {
374 // public void realRun() throws InterruptedException {
375 // lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
376 // }});
377
378 // waitForQueuedThread(lock, t);
379 // t.interrupt();
380 // awaitTermination(t);
381 // releaseWriteLock(lock);
382 // }
383
384 // /**
385 // * write-tryLock on an unlocked lock succeeds
386 // */
387 // public void testWriteTryLock() { testWriteTryLock(false); }
388 // public void testWriteTryLock_fair() { testWriteTryLock(true); }
389 // public void testWriteTryLock(boolean fair) {
390 // final PublicReentrantReadWriteLock lock =
391 // new PublicReentrantReadWriteLock(fair);
392 // assertTrue(lock.writeLock().tryLock());
393 // assertWriteLockedByMoi(lock);
394 // assertTrue(lock.writeLock().tryLock());
395 // assertWriteLockedByMoi(lock);
396 // lock.writeLock().unlock();
397 // releaseWriteLock(lock);
398 // }
399
400 // /**
401 // * write-tryLock fails if locked
402 // */
403 // public void testWriteTryLockWhenLocked() { testWriteTryLockWhenLocked(false); }
404 // public void testWriteTryLockWhenLocked_fair() { testWriteTryLockWhenLocked(true); }
405 // public void testWriteTryLockWhenLocked(boolean fair) {
406 // final PublicReentrantReadWriteLock lock =
407 // new PublicReentrantReadWriteLock(fair);
408 // lock.writeLock().lock();
409 // Thread t = newStartedThread(new CheckedRunnable() {
410 // public void realRun() {
411 // assertFalse(lock.writeLock().tryLock());
412 // }});
413
414 // awaitTermination(t);
415 // releaseWriteLock(lock);
416 // }
417
418 // /**
419 // * read-tryLock fails if locked
420 // */
421 // public void testReadTryLockWhenLocked() { testReadTryLockWhenLocked(false); }
422 // public void testReadTryLockWhenLocked_fair() { testReadTryLockWhenLocked(true); }
423 // public void testReadTryLockWhenLocked(boolean fair) {
424 // final PublicReentrantReadWriteLock lock =
425 // new PublicReentrantReadWriteLock(fair);
426 // lock.writeLock().lock();
427 // Thread t = newStartedThread(new CheckedRunnable() {
428 // public void realRun() {
429 // assertFalse(lock.readLock().tryLock());
430 // }});
431
432 // awaitTermination(t);
433 // releaseWriteLock(lock);
434 // }
435
436 // /**
437 // * Multiple threads can hold a read lock when not write-locked
438 // */
439 // public void testMultipleReadLocks() { testMultipleReadLocks(false); }
440 // public void testMultipleReadLocks_fair() { testMultipleReadLocks(true); }
441 // public void testMultipleReadLocks(boolean fair) {
442 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
443 // lock.readLock().lock();
444 // Thread t = newStartedThread(new CheckedRunnable() {
445 // public void realRun() throws InterruptedException {
446 // assertTrue(lock.readLock().tryLock());
447 // lock.readLock().unlock();
448 // assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS));
449 // lock.readLock().unlock();
450 // lock.readLock().lock();
451 // lock.readLock().unlock();
452 // }});
453
454 // awaitTermination(t);
455 // lock.readLock().unlock();
456 // }
457
458 // /**
459 // * A writelock succeeds only after a reading thread unlocks
460 // */
461 // public void testWriteAfterReadLock() { testWriteAfterReadLock(false); }
462 // public void testWriteAfterReadLock_fair() { testWriteAfterReadLock(true); }
463 // public void testWriteAfterReadLock(boolean fair) {
464 // final PublicReentrantReadWriteLock lock =
465 // new PublicReentrantReadWriteLock(fair);
466 // lock.readLock().lock();
467 // Thread t = newStartedThread(new CheckedRunnable() {
468 // public void realRun() {
469 // assertEquals(1, lock.getReadLockCount());
470 // lock.writeLock().lock();
471 // assertEquals(0, lock.getReadLockCount());
472 // lock.writeLock().unlock();
473 // }});
474 // waitForQueuedThread(lock, t);
475 // assertNotWriteLocked(lock);
476 // assertEquals(1, lock.getReadLockCount());
477 // lock.readLock().unlock();
478 // assertEquals(0, lock.getReadLockCount());
479 // awaitTermination(t);
480 // assertNotWriteLocked(lock);
481 // }
482
483 // /**
484 // * A writelock succeeds only after reading threads unlock
485 // */
486 // public void testWriteAfterMultipleReadLocks() { testWriteAfterMultipleReadLocks(false); }
487 // public void testWriteAfterMultipleReadLocks_fair() { testWriteAfterMultipleReadLocks(true); }
488 // public void testWriteAfterMultipleReadLocks(boolean fair) {
489 // final PublicReentrantReadWriteLock lock =
490 // new PublicReentrantReadWriteLock(fair);
491 // lock.readLock().lock();
492 // lock.readLock().lock();
493 // Thread t1 = newStartedThread(new CheckedRunnable() {
494 // public void realRun() {
495 // lock.readLock().lock();
496 // assertEquals(3, lock.getReadLockCount());
497 // lock.readLock().unlock();
498 // }});
499 // awaitTermination(t1);
500
501 // Thread t2 = newStartedThread(new CheckedRunnable() {
502 // public void realRun() {
503 // assertEquals(2, lock.getReadLockCount());
504 // lock.writeLock().lock();
505 // assertEquals(0, lock.getReadLockCount());
506 // lock.writeLock().unlock();
507 // }});
508 // waitForQueuedThread(lock, t2);
509 // assertNotWriteLocked(lock);
510 // assertEquals(2, lock.getReadLockCount());
511 // lock.readLock().unlock();
512 // lock.readLock().unlock();
513 // assertEquals(0, lock.getReadLockCount());
514 // awaitTermination(t2);
515 // assertNotWriteLocked(lock);
516 // }
517
518 // /**
519 // * A thread that tries to acquire a fair read lock (non-reentrantly)
520 // * will block if there is a waiting writer thread
521 // */
522 // public void testReaderWriterReaderFairFifo() {
523 // final PublicReentrantReadWriteLock lock =
524 // new PublicReentrantReadWriteLock(true);
525 // final AtomicBoolean t1GotLock = new AtomicBoolean(false);
526
527 // lock.readLock().lock();
528 // Thread t1 = newStartedThread(new CheckedRunnable() {
529 // public void realRun() {
530 // assertEquals(1, lock.getReadLockCount());
531 // lock.writeLock().lock();
532 // assertEquals(0, lock.getReadLockCount());
533 // t1GotLock.set(true);
534 // lock.writeLock().unlock();
535 // }});
536 // waitForQueuedThread(lock, t1);
537
538 // Thread t2 = newStartedThread(new CheckedRunnable() {
539 // public void realRun() {
540 // assertEquals(1, lock.getReadLockCount());
541 // lock.readLock().lock();
542 // assertEquals(1, lock.getReadLockCount());
543 // assertTrue(t1GotLock.get());
544 // lock.readLock().unlock();
545 // }});
546 // waitForQueuedThread(lock, t2);
547 // assertTrue(t1.isAlive());
548 // assertNotWriteLocked(lock);
549 // assertEquals(1, lock.getReadLockCount());
550 // lock.readLock().unlock();
551 // awaitTermination(t1);
552 // awaitTermination(t2);
553 // assertNotWriteLocked(lock);
554 // }
555
556 // /**
557 // * Readlocks succeed only after a writing thread unlocks
558 // */
559 // public void testReadAfterWriteLock() { testReadAfterWriteLock(false); }
560 // public void testReadAfterWriteLock_fair() { testReadAfterWriteLock(true); }
561 // public void testReadAfterWriteLock(boolean fair) {
562 // final PublicReentrantReadWriteLock lock =
563 // new PublicReentrantReadWriteLock(fair);
564 // lock.writeLock().lock();
565 // Thread t1 = newStartedThread(new CheckedRunnable() {
566 // public void realRun() {
567 // lock.readLock().lock();
568 // lock.readLock().unlock();
569 // }});
570 // Thread t2 = newStartedThread(new CheckedRunnable() {
571 // public void realRun() {
572 // lock.readLock().lock();
573 // lock.readLock().unlock();
574 // }});
575
576 // waitForQueuedThread(lock, t1);
577 // waitForQueuedThread(lock, t2);
578 // releaseWriteLock(lock);
579 // awaitTermination(t1);
580 // awaitTermination(t2);
581 // }
582
583 // /**
584 // * Read trylock succeeds if write locked by current thread
585 // */
586 // public void testReadHoldingWriteLock() { testReadHoldingWriteLock(false); }
587 // public void testReadHoldingWriteLock_fair() { testReadHoldingWriteLock(true); }
588 // public void testReadHoldingWriteLock(boolean fair) {
589 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
590 // lock.writeLock().lock();
591 // assertTrue(lock.readLock().tryLock());
592 // lock.readLock().unlock();
593 // lock.writeLock().unlock();
594 // }
595
596 // /**
597 // * Read trylock succeeds (barging) even in the presence of waiting
598 // * readers and/or writers
599 // */
600 // public void testReadTryLockBarging() { testReadTryLockBarging(false); }
601 // public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
602 // public void testReadTryLockBarging(boolean fair) {
603 // final PublicReentrantReadWriteLock lock =
604 // new PublicReentrantReadWriteLock(fair);
605 // lock.readLock().lock();
606
607 // Thread t1 = newStartedThread(new CheckedRunnable() {
608 // public void realRun() {
609 // lock.writeLock().lock();
610 // lock.writeLock().unlock();
611 // }});
612
613 // waitForQueuedThread(lock, t1);
614
615 // Thread t2 = newStartedThread(new CheckedRunnable() {
616 // public void realRun() {
617 // lock.readLock().lock();
618 // lock.readLock().unlock();
619 // }});
620
621 // if (fair)
622 // waitForQueuedThread(lock, t2);
623
624 // Thread t3 = newStartedThread(new CheckedRunnable() {
625 // public void realRun() {
626 // lock.readLock().tryLock();
627 // lock.readLock().unlock();
628 // }});
629
630 // assertTrue(lock.getReadLockCount() > 0);
631 // awaitTermination(t3);
632 // assertTrue(t1.isAlive());
633 // if (fair) assertTrue(t2.isAlive());
634 // lock.readLock().unlock();
635 // awaitTermination(t1);
636 // awaitTermination(t2);
637 // }
638
639 // /**
640 // * Read lock succeeds if write locked by current thread even if
641 // * other threads are waiting for readlock
642 // */
643 // public void testReadHoldingWriteLock2() { testReadHoldingWriteLock2(false); }
644 // public void testReadHoldingWriteLock2_fair() { testReadHoldingWriteLock2(true); }
645 // public void testReadHoldingWriteLock2(boolean fair) {
646 // final PublicReentrantReadWriteLock lock =
647 // new PublicReentrantReadWriteLock(fair);
648 // lock.writeLock().lock();
649 // lock.readLock().lock();
650 // lock.readLock().unlock();
651
652 // Thread t1 = newStartedThread(new CheckedRunnable() {
653 // public void realRun() {
654 // lock.readLock().lock();
655 // lock.readLock().unlock();
656 // }});
657 // Thread t2 = newStartedThread(new CheckedRunnable() {
658 // public void realRun() {
659 // lock.readLock().lock();
660 // lock.readLock().unlock();
661 // }});
662
663 // waitForQueuedThread(lock, t1);
664 // waitForQueuedThread(lock, t2);
665 // assertWriteLockedByMoi(lock);
666 // lock.readLock().lock();
667 // lock.readLock().unlock();
668 // releaseWriteLock(lock);
669 // awaitTermination(t1);
670 // awaitTermination(t2);
671 // }
672
673 // /**
674 // * Read lock succeeds if write locked by current thread even if
675 // * other threads are waiting for writelock
676 // */
677 // public void testReadHoldingWriteLock3() { testReadHoldingWriteLock3(false); }
678 // public void testReadHoldingWriteLock3_fair() { testReadHoldingWriteLock3(true); }
679 // public void testReadHoldingWriteLock3(boolean fair) {
680 // final PublicReentrantReadWriteLock lock =
681 // new PublicReentrantReadWriteLock(fair);
682 // lock.writeLock().lock();
683 // lock.readLock().lock();
684 // lock.readLock().unlock();
685
686 // Thread t1 = newStartedThread(new CheckedRunnable() {
687 // public void realRun() {
688 // lock.writeLock().lock();
689 // lock.writeLock().unlock();
690 // }});
691 // Thread t2 = newStartedThread(new CheckedRunnable() {
692 // public void realRun() {
693 // lock.writeLock().lock();
694 // lock.writeLock().unlock();
695 // }});
696
697 // waitForQueuedThread(lock, t1);
698 // waitForQueuedThread(lock, t2);
699 // assertWriteLockedByMoi(lock);
700 // lock.readLock().lock();
701 // lock.readLock().unlock();
702 // assertWriteLockedByMoi(lock);
703 // lock.writeLock().unlock();
704 // awaitTermination(t1);
705 // awaitTermination(t2);
706 // }
707
708 // /**
709 // * Write lock succeeds if write locked by current thread even if
710 // * other threads are waiting for writelock
711 // */
712 // public void testWriteHoldingWriteLock4() { testWriteHoldingWriteLock4(false); }
713 // public void testWriteHoldingWriteLock4_fair() { testWriteHoldingWriteLock4(true); }
714 // public void testWriteHoldingWriteLock4(boolean fair) {
715 // final PublicReentrantReadWriteLock lock =
716 // new PublicReentrantReadWriteLock(fair);
717 // lock.writeLock().lock();
718 // lock.writeLock().lock();
719 // lock.writeLock().unlock();
720
721 // Thread t1 = newStartedThread(new CheckedRunnable() {
722 // public void realRun() {
723 // lock.writeLock().lock();
724 // lock.writeLock().unlock();
725 // }});
726 // Thread t2 = newStartedThread(new CheckedRunnable() {
727 // public void realRun() {
728 // lock.writeLock().lock();
729 // lock.writeLock().unlock();
730 // }});
731
732 // waitForQueuedThread(lock, t1);
733 // waitForQueuedThread(lock, t2);
734 // assertWriteLockedByMoi(lock);
735 // assertEquals(1, lock.getWriteHoldCount());
736 // lock.writeLock().lock();
737 // assertWriteLockedByMoi(lock);
738 // assertEquals(2, lock.getWriteHoldCount());
739 // lock.writeLock().unlock();
740 // assertWriteLockedByMoi(lock);
741 // assertEquals(1, lock.getWriteHoldCount());
742 // lock.writeLock().unlock();
743 // awaitTermination(t1);
744 // awaitTermination(t2);
745 // }
746
747 // /**
748 // * Read tryLock succeeds if readlocked but not writelocked
749 // */
750 // public void testTryLockWhenReadLocked() { testTryLockWhenReadLocked(false); }
751 // public void testTryLockWhenReadLocked_fair() { testTryLockWhenReadLocked(true); }
752 // public void testTryLockWhenReadLocked(boolean fair) {
753 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
754 // lock.readLock().lock();
755 // Thread t = newStartedThread(new CheckedRunnable() {
756 // public void realRun() {
757 // assertTrue(lock.readLock().tryLock());
758 // lock.readLock().unlock();
759 // }});
760
761 // awaitTermination(t);
762 // lock.readLock().unlock();
763 // }
764
765 // /**
766 // * write tryLock fails when readlocked
767 // */
768 // public void testWriteTryLockWhenReadLocked() { testWriteTryLockWhenReadLocked(false); }
769 // public void testWriteTryLockWhenReadLocked_fair() { testWriteTryLockWhenReadLocked(true); }
770 // public void testWriteTryLockWhenReadLocked(boolean fair) {
771 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
772 // lock.readLock().lock();
773 // Thread t = newStartedThread(new CheckedRunnable() {
774 // public void realRun() {
775 // assertFalse(lock.writeLock().tryLock());
776 // }});
777
778 // awaitTermination(t);
779 // lock.readLock().unlock();
780 // }
781
782 // /**
783 // * write timed tryLock times out if locked
784 // */
785 // public void testWriteTryLock_Timeout() { testWriteTryLock_Timeout(false); }
786 // public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
787 // public void testWriteTryLock_Timeout(boolean fair) {
788 // final PublicReentrantReadWriteLock lock =
789 // new PublicReentrantReadWriteLock(fair);
790 // lock.writeLock().lock();
791 // Thread t = newStartedThread(new CheckedRunnable() {
792 // public void realRun() throws InterruptedException {
793 // long startTime = System.nanoTime();
794 // long timeoutMillis = 10;
795 // assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS));
796 // assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
797 // }});
798
799 // awaitTermination(t);
800 // releaseWriteLock(lock);
801 // }
802
803 // /**
804 // * read timed tryLock times out if write-locked
805 // */
806 // public void testReadTryLock_Timeout() { testReadTryLock_Timeout(false); }
807 // public void testReadTryLock_Timeout_fair() { testReadTryLock_Timeout(true); }
808 // public void testReadTryLock_Timeout(boolean fair) {
809 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
810 // lock.writeLock().lock();
811 // Thread t = newStartedThread(new CheckedRunnable() {
812 // public void realRun() throws InterruptedException {
813 // long startTime = System.nanoTime();
814 // long timeoutMillis = 10;
815 // assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS));
816 // assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
817 // }});
818
819 // awaitTermination(t);
820 // assertTrue(lock.writeLock().isHeldByCurrentThread());
821 // lock.writeLock().unlock();
822 // }
823
824 // /**
825 // * write lockInterruptibly succeeds if unlocked, else is interruptible
826 // */
827 // public void testWriteLockInterruptibly() { testWriteLockInterruptibly(false); }
828 // public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
829 // public void testWriteLockInterruptibly(boolean fair) {
830 // final PublicReentrantReadWriteLock lock =
831 // new PublicReentrantReadWriteLock(fair);
832 // try {
833 // lock.writeLock().lockInterruptibly();
834 // } catch (InterruptedException ie) {
835 // threadUnexpectedException(ie);
836 // }
837 // Thread t = newStartedThread(new CheckedInterruptedRunnable() {
838 // public void realRun() throws InterruptedException {
839 // lock.writeLock().lockInterruptibly();
840 // }});
841
842 // waitForQueuedThread(lock, t);
843 // t.interrupt();
844 // assertTrue(lock.writeLock().isHeldByCurrentThread());
845 // awaitTermination(t);
846 // releaseWriteLock(lock);
847 // }
848
849 // /**
850 // * read lockInterruptibly succeeds if lock free else is interruptible
851 // */
852 // public void testReadLockInterruptibly() { testReadLockInterruptibly(false); }
853 // public void testReadLockInterruptibly_fair() { testReadLockInterruptibly(true); }
854 // public void testReadLockInterruptibly(boolean fair) {
855 // final PublicReentrantReadWriteLock lock =
856 // new PublicReentrantReadWriteLock(fair);
857 // try {
858 // lock.readLock().lockInterruptibly();
859 // lock.readLock().unlock();
860 // lock.writeLock().lockInterruptibly();
861 // } catch (InterruptedException ie) {
862 // threadUnexpectedException(ie);
863 // }
864 // Thread t = newStartedThread(new CheckedInterruptedRunnable() {
865 // public void realRun() throws InterruptedException {
866 // lock.readLock().lockInterruptibly();
867 // }});
868
869 // waitForQueuedThread(lock, t);
870 // t.interrupt();
871 // awaitTermination(t);
872 // releaseWriteLock(lock);
873 // }
874
875 // /**
876 // * Calling await without holding lock throws IllegalMonitorStateException
877 // */
878 // public void testAwait_IMSE() { testAwait_IMSE(false); }
879 // public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
880 // public void testAwait_IMSE(boolean fair) {
881 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
882 // final Condition c = lock.writeLock().newCondition();
883 // for (AwaitMethod awaitMethod : AwaitMethod.values()) {
884 // long startTime = System.nanoTime();
885 // try {
886 // await(c, awaitMethod);
887 // shouldThrow();
888 // } catch (IllegalMonitorStateException success) {
889 // } catch (InterruptedException e) { threadUnexpectedException(e); }
890 // assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
891 // }
892 // }
893
894 // /**
895 // * Calling signal without holding lock throws IllegalMonitorStateException
896 // */
897 // public void testSignal_IMSE() { testSignal_IMSE(false); }
898 // public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
899 // public void testSignal_IMSE(boolean fair) {
900 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
901 // final Condition c = lock.writeLock().newCondition();
902 // try {
903 // c.signal();
904 // shouldThrow();
905 // } catch (IllegalMonitorStateException success) {}
906 // }
907
908 // /**
909 // * Calling signalAll without holding lock throws IllegalMonitorStateException
910 // */
911 // public void testSignalAll_IMSE() { testSignalAll_IMSE(false); }
912 // public void testSignalAll_IMSE_fair() { testSignalAll_IMSE(true); }
913 // public void testSignalAll_IMSE(boolean fair) {
914 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
915 // final Condition c = lock.writeLock().newCondition();
916 // try {
917 // c.signalAll();
918 // shouldThrow();
919 // } catch (IllegalMonitorStateException success) {}
920 // }
921
922 // /**
923 // * awaitNanos without a signal times out
924 // */
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 // try {
929 // final ReentrantReadWriteLock lock =
930 // new ReentrantReadWriteLock(fair);
931 // final Condition c = lock.writeLock().newCondition();
932 // lock.writeLock().lock();
933 // long startTime = System.nanoTime();
934 // long timeoutMillis = 10;
935 // long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
936 // long nanosRemaining = c.awaitNanos(timeoutNanos);
937 // assertTrue(nanosRemaining <= 0);
938 // assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
939 // lock.writeLock().unlock();
940 // } catch (InterruptedException e) {
941 // threadUnexpectedException(e);
942 // }
943 // }
944
945 // /**
946 // * timed await without a signal times out
947 // */
948 // public void testAwait_Timeout() { testAwait_Timeout(false); }
949 // public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
950 // public void testAwait_Timeout(boolean fair) {
951 // try {
952 // final ReentrantReadWriteLock lock =
953 // new ReentrantReadWriteLock(fair);
954 // final Condition c = lock.writeLock().newCondition();
955 // lock.writeLock().lock();
956 // long startTime = System.nanoTime();
957 // long timeoutMillis = 10;
958 // assertFalse(c.await(timeoutMillis, MILLISECONDS));
959 // assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
960 // lock.writeLock().unlock();
961 // } catch (InterruptedException e) {
962 // threadUnexpectedException(e);
963 // }
964 // }
965
966 // /**
967 // * awaitUntil without a signal times out
968 // */
969 // public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); }
970 // public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
971 // public void testAwaitUntil_Timeout(boolean fair) {
972 // try {
973 // final ReentrantReadWriteLock lock =
974 // new ReentrantReadWriteLock(fair);
975 // final Condition c = lock.writeLock().newCondition();
976 // lock.writeLock().lock();
977 // long startTime = System.nanoTime();
978 // long timeoutMillis = 10;
979 // java.util.Date d = new java.util.Date();
980 // assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
981 // assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
982 // lock.writeLock().unlock();
983 // } catch (InterruptedException e) {
984 // threadUnexpectedException(e);
985 // }
986 // }
987
988 // /**
989 // * await returns when signalled
990 // */
991 // public void testAwait() { testAwait(false); }
992 // public void testAwait_fair() { testAwait(true); }
993 // public void testAwait(boolean fair) {
994 // final PublicReentrantReadWriteLock lock =
995 // new PublicReentrantReadWriteLock(fair);
996 // final Condition c = lock.writeLock().newCondition();
997 // final CountDownLatch locked = new CountDownLatch(1);
998 // Thread t = newStartedThread(new CheckedRunnable() {
999 // public void realRun() throws InterruptedException {
1000 // lock.writeLock().lock();
1001 // locked.countDown();
1002 // c.await();
1003 // lock.writeLock().unlock();
1004 // }});
1005
1006 // await(locked);
1007 // lock.writeLock().lock();
1008 // assertHasWaiters(lock, c, t);
1009 // c.signal();
1010 // assertHasNoWaiters(lock, c);
1011 // assertTrue(t.isAlive());
1012 // lock.writeLock().unlock();
1013 // awaitTermination(t);
1014 // }
1015
1016 // /**
1017 // * awaitUninterruptibly is uninterruptible
1018 // */
1019 // public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); }
1020 // public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
1021 // public void testAwaitUninterruptibly(boolean fair) {
1022 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1023 // final Condition c = lock.writeLock().newCondition();
1024 // final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
1025
1026 // Thread t1 = newStartedThread(new CheckedRunnable() {
1027 // public void realRun() {
1028 // // Interrupt before awaitUninterruptibly
1029 // lock.writeLock().lock();
1030 // pleaseInterrupt.countDown();
1031 // Thread.currentThread().interrupt();
1032 // c.awaitUninterruptibly();
1033 // assertTrue(Thread.interrupted());
1034 // lock.writeLock().unlock();
1035 // }});
1036
1037 // Thread t2 = newStartedThread(new CheckedRunnable() {
1038 // public void realRun() {
1039 // // Interrupt during awaitUninterruptibly
1040 // lock.writeLock().lock();
1041 // pleaseInterrupt.countDown();
1042 // c.awaitUninterruptibly();
1043 // assertTrue(Thread.interrupted());
1044 // lock.writeLock().unlock();
1045 // }});
1046
1047 // await(pleaseInterrupt);
1048 // lock.writeLock().lock();
1049 // lock.writeLock().unlock();
1050 // t2.interrupt();
1051
1052 // assertThreadStaysAlive(t1);
1053 // assertTrue(t2.isAlive());
1054
1055 // lock.writeLock().lock();
1056 // c.signalAll();
1057 // lock.writeLock().unlock();
1058
1059 // awaitTermination(t1);
1060 // awaitTermination(t2);
1061 // }
1062
1063 // /**
1064 // * await/awaitNanos/awaitUntil is interruptible
1065 // */
1066 // public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); }
1067 // public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); }
1068 // public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); }
1069 // public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); }
1070 // public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); }
1071 // public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); }
1072 // public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); }
1073 // public void testInterruptible_awaitUntil_fair() { testInterruptible(true, AwaitMethod.awaitUntil); }
1074 // public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
1075 // final PublicReentrantReadWriteLock lock =
1076 // new PublicReentrantReadWriteLock(fair);
1077 // final Condition c = lock.writeLock().newCondition();
1078 // final CountDownLatch locked = new CountDownLatch(1);
1079 // Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1080 // public void realRun() throws InterruptedException {
1081 // lock.writeLock().lock();
1082 // assertWriteLockedByMoi(lock);
1083 // assertHasNoWaiters(lock, c);
1084 // locked.countDown();
1085 // try {
1086 // await(c, awaitMethod);
1087 // } finally {
1088 // assertWriteLockedByMoi(lock);
1089 // assertHasNoWaiters(lock, c);
1090 // lock.writeLock().unlock();
1091 // assertFalse(Thread.interrupted());
1092 // }
1093 // }});
1094
1095 // await(locked);
1096 // assertHasWaiters(lock, c, t);
1097 // t.interrupt();
1098 // awaitTermination(t);
1099 // assertNotWriteLocked(lock);
1100 // }
1101
1102 // /**
1103 // * signalAll wakes up all threads
1104 // */
1105 // public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); }
1106 // public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); }
1107 // public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); }
1108 // public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); }
1109 // public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); }
1110 // public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); }
1111 // public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); }
1112 // public void testSignalAll_awaitUntil_fair() { testSignalAll(true, AwaitMethod.awaitUntil); }
1113 // public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
1114 // final PublicReentrantReadWriteLock lock =
1115 // new PublicReentrantReadWriteLock(fair);
1116 // final Condition c = lock.writeLock().newCondition();
1117 // final CountDownLatch locked = new CountDownLatch(2);
1118 // final Lock writeLock = lock.writeLock();
1119 // class Awaiter extends CheckedRunnable {
1120 // public void realRun() throws InterruptedException {
1121 // writeLock.lock();
1122 // locked.countDown();
1123 // await(c, awaitMethod);
1124 // writeLock.unlock();
1125 // }
1126 // }
1127
1128 // Thread t1 = newStartedThread(new Awaiter());
1129 // Thread t2 = newStartedThread(new Awaiter());
1130
1131 // await(locked);
1132 // writeLock.lock();
1133 // assertHasWaiters(lock, c, t1, t2);
1134 // c.signalAll();
1135 // assertHasNoWaiters(lock, c);
1136 // writeLock.unlock();
1137 // awaitTermination(t1);
1138 // awaitTermination(t2);
1139 // }
1140
1141 // /**
1142 // * signal wakes up waiting threads in FIFO order
1143 // */
1144 // public void testSignalWakesFifo() { testSignalWakesFifo(false); }
1145 // public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1146 // public void testSignalWakesFifo(boolean fair) {
1147 // final PublicReentrantReadWriteLock lock =
1148 // new PublicReentrantReadWriteLock(fair);
1149 // final Condition c = lock.writeLock().newCondition();
1150 // final CountDownLatch locked1 = new CountDownLatch(1);
1151 // final CountDownLatch locked2 = new CountDownLatch(1);
1152 // final Lock writeLock = lock.writeLock();
1153 // Thread t1 = newStartedThread(new CheckedRunnable() {
1154 // public void realRun() throws InterruptedException {
1155 // writeLock.lock();
1156 // locked1.countDown();
1157 // c.await();
1158 // writeLock.unlock();
1159 // }});
1160
1161 // await(locked1);
1162
1163 // Thread t2 = newStartedThread(new CheckedRunnable() {
1164 // public void realRun() throws InterruptedException {
1165 // writeLock.lock();
1166 // locked2.countDown();
1167 // c.await();
1168 // writeLock.unlock();
1169 // }});
1170
1171 // await(locked2);
1172
1173 // writeLock.lock();
1174 // assertHasWaiters(lock, c, t1, t2);
1175 // assertFalse(lock.hasQueuedThreads());
1176 // c.signal();
1177 // assertHasWaiters(lock, c, t2);
1178 // assertTrue(lock.hasQueuedThread(t1));
1179 // assertFalse(lock.hasQueuedThread(t2));
1180 // c.signal();
1181 // assertHasNoWaiters(lock, c);
1182 // assertTrue(lock.hasQueuedThread(t1));
1183 // assertTrue(lock.hasQueuedThread(t2));
1184 // writeLock.unlock();
1185 // awaitTermination(t1);
1186 // awaitTermination(t2);
1187 // }
1188
1189 // /**
1190 // * await after multiple reentrant locking preserves lock count
1191 // */
1192 // public void testAwaitLockCount() { testAwaitLockCount(false); }
1193 // public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1194 // public void testAwaitLockCount(boolean fair) {
1195 // final PublicReentrantReadWriteLock lock =
1196 // new PublicReentrantReadWriteLock(fair);
1197 // final Condition c = lock.writeLock().newCondition();
1198 // final CountDownLatch locked = new CountDownLatch(2);
1199 // Thread t1 = newStartedThread(new CheckedRunnable() {
1200 // public void realRun() throws InterruptedException {
1201 // lock.writeLock().lock();
1202 // assertWriteLockedByMoi(lock);
1203 // assertEquals(1, lock.writeLock().getHoldCount());
1204 // locked.countDown();
1205 // c.await();
1206 // assertWriteLockedByMoi(lock);
1207 // assertEquals(1, lock.writeLock().getHoldCount());
1208 // lock.writeLock().unlock();
1209 // }});
1210
1211 // Thread t2 = newStartedThread(new CheckedRunnable() {
1212 // public void realRun() throws InterruptedException {
1213 // lock.writeLock().lock();
1214 // lock.writeLock().lock();
1215 // assertWriteLockedByMoi(lock);
1216 // assertEquals(2, lock.writeLock().getHoldCount());
1217 // locked.countDown();
1218 // c.await();
1219 // assertWriteLockedByMoi(lock);
1220 // assertEquals(2, lock.writeLock().getHoldCount());
1221 // lock.writeLock().unlock();
1222 // lock.writeLock().unlock();
1223 // }});
1224
1225 // await(locked);
1226 // lock.writeLock().lock();
1227 // assertHasWaiters(lock, c, t1, t2);
1228 // c.signalAll();
1229 // assertHasNoWaiters(lock, c);
1230 // lock.writeLock().unlock();
1231 // awaitTermination(t1);
1232 // awaitTermination(t2);
1233 // }
1234
1235 // /**
1236 // * A serialized lock deserializes as unlocked
1237 // */
1238 // public void testSerialization() { testSerialization(false); }
1239 // public void testSerialization_fair() { testSerialization(true); }
1240 // public void testSerialization(boolean fair) {
1241 // ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1242 // lock.writeLock().lock();
1243 // lock.readLock().lock();
1244
1245 // ReentrantReadWriteLock clone = serialClone(lock);
1246 // assertEquals(lock.isFair(), clone.isFair());
1247 // assertTrue(lock.isWriteLocked());
1248 // assertFalse(clone.isWriteLocked());
1249 // assertEquals(1, lock.getReadLockCount());
1250 // assertEquals(0, clone.getReadLockCount());
1251 // clone.writeLock().lock();
1252 // clone.readLock().lock();
1253 // assertTrue(clone.isWriteLocked());
1254 // assertEquals(1, clone.getReadLockCount());
1255 // clone.readLock().unlock();
1256 // clone.writeLock().unlock();
1257 // assertFalse(clone.isWriteLocked());
1258 // assertEquals(1, lock.getReadLockCount());
1259 // assertEquals(0, clone.getReadLockCount());
1260 // }
1261
1262 // /**
1263 // * hasQueuedThreads reports whether there are waiting threads
1264 // */
1265 // public void testHasQueuedThreads() { testHasQueuedThreads(false); }
1266 // public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
1267 // public void testHasQueuedThreads(boolean fair) {
1268 // final PublicReentrantReadWriteLock lock =
1269 // new PublicReentrantReadWriteLock(fair);
1270 // Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1271 // Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1272 // assertFalse(lock.hasQueuedThreads());
1273 // lock.writeLock().lock();
1274 // assertFalse(lock.hasQueuedThreads());
1275 // t1.start();
1276 // waitForQueuedThread(lock, t1);
1277 // assertTrue(lock.hasQueuedThreads());
1278 // t2.start();
1279 // waitForQueuedThread(lock, t2);
1280 // assertTrue(lock.hasQueuedThreads());
1281 // t1.interrupt();
1282 // awaitTermination(t1);
1283 // assertTrue(lock.hasQueuedThreads());
1284 // lock.writeLock().unlock();
1285 // awaitTermination(t2);
1286 // assertFalse(lock.hasQueuedThreads());
1287 // }
1288
1289 // /**
1290 // * hasQueuedThread(null) throws NPE
1291 // */
1292 // public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); }
1293 // public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
1294 // public void testHasQueuedThreadNPE(boolean fair) {
1295 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1296 // try {
1297 // lock.hasQueuedThread(null);
1298 // shouldThrow();
1299 // } catch (NullPointerException success) {}
1300 // }
1301
1302 // /**
1303 // * hasQueuedThread reports whether a thread is queued
1304 // */
1305 // public void testHasQueuedThread() { testHasQueuedThread(false); }
1306 // public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
1307 // public void testHasQueuedThread(boolean fair) {
1308 // final PublicReentrantReadWriteLock lock =
1309 // new PublicReentrantReadWriteLock(fair);
1310 // Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1311 // Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1312 // assertFalse(lock.hasQueuedThread(t1));
1313 // assertFalse(lock.hasQueuedThread(t2));
1314 // lock.writeLock().lock();
1315 // t1.start();
1316 // waitForQueuedThread(lock, t1);
1317 // assertTrue(lock.hasQueuedThread(t1));
1318 // assertFalse(lock.hasQueuedThread(t2));
1319 // t2.start();
1320 // waitForQueuedThread(lock, t2);
1321 // assertTrue(lock.hasQueuedThread(t1));
1322 // assertTrue(lock.hasQueuedThread(t2));
1323 // t1.interrupt();
1324 // awaitTermination(t1);
1325 // assertFalse(lock.hasQueuedThread(t1));
1326 // assertTrue(lock.hasQueuedThread(t2));
1327 // lock.writeLock().unlock();
1328 // awaitTermination(t2);
1329 // assertFalse(lock.hasQueuedThread(t1));
1330 // assertFalse(lock.hasQueuedThread(t2));
1331 // }
1332
1333 // /**
1334 // * getQueueLength reports number of waiting threads
1335 // */
1336 // public void testGetQueueLength() { testGetQueueLength(false); }
1337 // public void testGetQueueLength_fair() { testGetQueueLength(true); }
1338 // public void testGetQueueLength(boolean fair) {
1339 // final PublicReentrantReadWriteLock lock =
1340 // new PublicReentrantReadWriteLock(fair);
1341 // Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1342 // Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1343 // assertEquals(0, lock.getQueueLength());
1344 // lock.writeLock().lock();
1345 // t1.start();
1346 // waitForQueuedThread(lock, t1);
1347 // assertEquals(1, lock.getQueueLength());
1348 // t2.start();
1349 // waitForQueuedThread(lock, t2);
1350 // assertEquals(2, lock.getQueueLength());
1351 // t1.interrupt();
1352 // awaitTermination(t1);
1353 // assertEquals(1, lock.getQueueLength());
1354 // lock.writeLock().unlock();
1355 // awaitTermination(t2);
1356 // assertEquals(0, lock.getQueueLength());
1357 // }
1358
1359 // /**
1360 // * getQueuedThreads includes waiting threads
1361 // */
1362 // public void testGetQueuedThreads() { testGetQueuedThreads(false); }
1363 // public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
1364 // public void testGetQueuedThreads(boolean fair) {
1365 // final PublicReentrantReadWriteLock lock =
1366 // new PublicReentrantReadWriteLock(fair);
1367 // Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1368 // Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1369 // assertTrue(lock.getQueuedThreads().isEmpty());
1370 // lock.writeLock().lock();
1371 // assertTrue(lock.getQueuedThreads().isEmpty());
1372 // t1.start();
1373 // waitForQueuedThread(lock, t1);
1374 // assertEquals(1, lock.getQueuedThreads().size());
1375 // assertTrue(lock.getQueuedThreads().contains(t1));
1376 // t2.start();
1377 // waitForQueuedThread(lock, t2);
1378 // assertEquals(2, lock.getQueuedThreads().size());
1379 // assertTrue(lock.getQueuedThreads().contains(t1));
1380 // assertTrue(lock.getQueuedThreads().contains(t2));
1381 // t1.interrupt();
1382 // awaitTermination(t1);
1383 // assertFalse(lock.getQueuedThreads().contains(t1));
1384 // assertTrue(lock.getQueuedThreads().contains(t2));
1385 // assertEquals(1, lock.getQueuedThreads().size());
1386 // lock.writeLock().unlock();
1387 // awaitTermination(t2);
1388 // assertTrue(lock.getQueuedThreads().isEmpty());
1389 // }
1390
1391 // /**
1392 // * hasWaiters throws NPE if null
1393 // */
1394 // public void testHasWaitersNPE() { testHasWaitersNPE(false); }
1395 // public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
1396 // public void testHasWaitersNPE(boolean fair) {
1397 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1398 // try {
1399 // lock.hasWaiters(null);
1400 // shouldThrow();
1401 // } catch (NullPointerException success) {}
1402 // }
1403
1404 // /**
1405 // * getWaitQueueLength throws NPE if null
1406 // */
1407 // public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); }
1408 // public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
1409 // public void testGetWaitQueueLengthNPE(boolean fair) {
1410 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1411 // try {
1412 // lock.getWaitQueueLength(null);
1413 // shouldThrow();
1414 // } catch (NullPointerException success) {}
1415 // }
1416
1417 // /**
1418 // * getWaitingThreads throws NPE if null
1419 // */
1420 // public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); }
1421 // public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
1422 // public void testGetWaitingThreadsNPE(boolean fair) {
1423 // final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(fair);
1424 // try {
1425 // lock.getWaitingThreads(null);
1426 // shouldThrow();
1427 // } catch (NullPointerException success) {}
1428 // }
1429
1430 // /**
1431 // * hasWaiters throws IllegalArgumentException if not owned
1432 // */
1433 // public void testHasWaitersIAE() { testHasWaitersIAE(false); }
1434 // public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
1435 // public void testHasWaitersIAE(boolean fair) {
1436 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1437 // final Condition c = lock.writeLock().newCondition();
1438 // final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1439 // try {
1440 // lock2.hasWaiters(c);
1441 // shouldThrow();
1442 // } catch (IllegalArgumentException success) {}
1443 // }
1444
1445 // /**
1446 // * hasWaiters throws IllegalMonitorStateException if not locked
1447 // */
1448 // public void testHasWaitersIMSE() { testHasWaitersIMSE(false); }
1449 // public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
1450 // public void testHasWaitersIMSE(boolean fair) {
1451 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1452 // final Condition c = lock.writeLock().newCondition();
1453 // try {
1454 // lock.hasWaiters(c);
1455 // shouldThrow();
1456 // } catch (IllegalMonitorStateException success) {}
1457 // }
1458
1459 // /**
1460 // * getWaitQueueLength throws IllegalArgumentException if not owned
1461 // */
1462 // public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); }
1463 // public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
1464 // public void testGetWaitQueueLengthIAE(boolean fair) {
1465 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1466 // final Condition c = lock.writeLock().newCondition();
1467 // final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock(fair);
1468 // try {
1469 // lock2.getWaitQueueLength(c);
1470 // shouldThrow();
1471 // } catch (IllegalArgumentException success) {}
1472 // }
1473
1474 // /**
1475 // * getWaitQueueLength throws IllegalMonitorStateException if not locked
1476 // */
1477 // public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); }
1478 // public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
1479 // public void testGetWaitQueueLengthIMSE(boolean fair) {
1480 // final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1481 // final Condition c = lock.writeLock().newCondition();
1482 // try {
1483 // lock.getWaitQueueLength(c);
1484 // shouldThrow();
1485 // } catch (IllegalMonitorStateException success) {}
1486 // }
1487
1488 // /**
1489 // * getWaitingThreads throws IllegalArgumentException if not owned
1490 // */
1491 // public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); }
1492 // public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
1493 // public void testGetWaitingThreadsIAE(boolean fair) {
1494 // final PublicReentrantReadWriteLock lock =
1495 // new PublicReentrantReadWriteLock(fair);
1496 // final Condition c = lock.writeLock().newCondition();
1497 // final PublicReentrantReadWriteLock lock2 =
1498 // new PublicReentrantReadWriteLock(fair);
1499 // try {
1500 // lock2.getWaitingThreads(c);
1501 // shouldThrow();
1502 // } catch (IllegalArgumentException success) {}
1503 // }
1504
1505 // /**
1506 // * getWaitingThreads throws IllegalMonitorStateException if not locked
1507 // */
1508 // public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); }
1509 // public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
1510 // public void testGetWaitingThreadsIMSE(boolean fair) {
1511 // final PublicReentrantReadWriteLock lock =
1512 // new PublicReentrantReadWriteLock(fair);
1513 // final Condition c = lock.writeLock().newCondition();
1514 // try {
1515 // lock.getWaitingThreads(c);
1516 // shouldThrow();
1517 // } catch (IllegalMonitorStateException success) {}
1518 // }
1519
1520 // /**
1521 // * hasWaiters returns true when a thread is waiting, else false
1522 // */
1523 // public void testHasWaiters() { testHasWaiters(false); }
1524 // public void testHasWaiters_fair() { testHasWaiters(true); }
1525 // public void testHasWaiters(boolean fair) {
1526 // final PublicReentrantReadWriteLock lock =
1527 // new PublicReentrantReadWriteLock(fair);
1528 // final Condition c = lock.writeLock().newCondition();
1529 // final CountDownLatch locked = new CountDownLatch(1);
1530 // Thread t = newStartedThread(new CheckedRunnable() {
1531 // public void realRun() throws InterruptedException {
1532 // lock.writeLock().lock();
1533 // assertHasNoWaiters(lock, c);
1534 // assertFalse(lock.hasWaiters(c));
1535 // locked.countDown();
1536 // c.await();
1537 // assertHasNoWaiters(lock, c);
1538 // assertFalse(lock.hasWaiters(c));
1539 // lock.writeLock().unlock();
1540 // }});
1541
1542 // await(locked);
1543 // lock.writeLock().lock();
1544 // assertHasWaiters(lock, c, t);
1545 // assertTrue(lock.hasWaiters(c));
1546 // c.signal();
1547 // assertHasNoWaiters(lock, c);
1548 // assertFalse(lock.hasWaiters(c));
1549 // lock.writeLock().unlock();
1550 // awaitTermination(t);
1551 // assertHasNoWaiters(lock, c);
1552 // }
1553
1554 // /**
1555 // * getWaitQueueLength returns number of waiting threads
1556 // */
1557 // public void testGetWaitQueueLength() { testGetWaitQueueLength(false); }
1558 // public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
1559 // public void testGetWaitQueueLength(boolean fair) {
1560 // final PublicReentrantReadWriteLock lock =
1561 // new PublicReentrantReadWriteLock(fair);
1562 // final Condition c = lock.writeLock().newCondition();
1563 // final CountDownLatch locked = new CountDownLatch(1);
1564 // Thread t = newStartedThread(new CheckedRunnable() {
1565 // public void realRun() throws InterruptedException {
1566 // lock.writeLock().lock();
1567 // assertEquals(0, lock.getWaitQueueLength(c));
1568 // locked.countDown();
1569 // c.await();
1570 // lock.writeLock().unlock();
1571 // }});
1572
1573 // await(locked);
1574 // lock.writeLock().lock();
1575 // assertHasWaiters(lock, c, t);
1576 // assertEquals(1, lock.getWaitQueueLength(c));
1577 // c.signal();
1578 // assertHasNoWaiters(lock, c);
1579 // assertEquals(0, lock.getWaitQueueLength(c));
1580 // lock.writeLock().unlock();
1581 // awaitTermination(t);
1582 // }
1583
1584 // /**
1585 // * getWaitingThreads returns only and all waiting threads
1586 // */
1587 // public void testGetWaitingThreads() { testGetWaitingThreads(false); }
1588 // public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
1589 // public void testGetWaitingThreads(boolean fair) {
1590 // final PublicReentrantReadWriteLock lock =
1591 // new PublicReentrantReadWriteLock(fair);
1592 // final Condition c = lock.writeLock().newCondition();
1593 // final CountDownLatch locked1 = new CountDownLatch(1);
1594 // final CountDownLatch locked2 = new CountDownLatch(1);
1595 // Thread t1 = new Thread(new CheckedRunnable() {
1596 // public void realRun() throws InterruptedException {
1597 // lock.writeLock().lock();
1598 // assertTrue(lock.getWaitingThreads(c).isEmpty());
1599 // locked1.countDown();
1600 // c.await();
1601 // lock.writeLock().unlock();
1602 // }});
1603
1604 // Thread t2 = new Thread(new CheckedRunnable() {
1605 // public void realRun() throws InterruptedException {
1606 // lock.writeLock().lock();
1607 // assertFalse(lock.getWaitingThreads(c).isEmpty());
1608 // locked2.countDown();
1609 // c.await();
1610 // lock.writeLock().unlock();
1611 // }});
1612
1613 // lock.writeLock().lock();
1614 // assertTrue(lock.getWaitingThreads(c).isEmpty());
1615 // lock.writeLock().unlock();
1616
1617 // t1.start();
1618 // await(locked1);
1619 // t2.start();
1620 // await(locked2);
1621
1622 // lock.writeLock().lock();
1623 // assertTrue(lock.hasWaiters(c));
1624 // assertTrue(lock.getWaitingThreads(c).contains(t1));
1625 // assertTrue(lock.getWaitingThreads(c).contains(t2));
1626 // assertEquals(2, lock.getWaitingThreads(c).size());
1627 // c.signalAll();
1628 // assertHasNoWaiters(lock, c);
1629 // lock.writeLock().unlock();
1630
1631 // awaitTermination(t1);
1632 // awaitTermination(t2);
1633
1634 // assertHasNoWaiters(lock, c);
1635 // }
1636
1637 // /**
1638 // * toString indicates current lock state
1639 // */
1640 // public void testToString() { testToString(false); }
1641 // public void testToString_fair() { testToString(true); }
1642 // public void testToString(boolean fair) {
1643 // ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1644 // assertTrue(lock.toString().contains("Write locks = 0"));
1645 // assertTrue(lock.toString().contains("Read locks = 0"));
1646 // lock.writeLock().lock();
1647 // assertTrue(lock.toString().contains("Write locks = 1"));
1648 // assertTrue(lock.toString().contains("Read locks = 0"));
1649 // lock.writeLock().unlock();
1650 // lock.readLock().lock();
1651 // lock.readLock().lock();
1652 // assertTrue(lock.toString().contains("Write locks = 0"));
1653 // assertTrue(lock.toString().contains("Read locks = 2"));
1654 // }
1655
1656 // /**
1657 // * readLock.toString indicates current lock state
1658 // */
1659 // public void testReadLockToString() { testReadLockToString(false); }
1660 // public void testReadLockToString_fair() { testReadLockToString(true); }
1661 // public void testReadLockToString(boolean fair) {
1662 // ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1663 // assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1664 // lock.readLock().lock();
1665 // lock.readLock().lock();
1666 // assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1667 // }
1668
1669 // /**
1670 // * writeLock.toString indicates current lock state
1671 // */
1672 // public void testWriteLockToString() { testWriteLockToString(false); }
1673 // public void testWriteLockToString_fair() { testWriteLockToString(true); }
1674 // public void testWriteLockToString(boolean fair) {
1675 // ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1676 // assertTrue(lock.writeLock().toString().contains("Unlocked"));
1677 // lock.writeLock().lock();
1678 // assertTrue(lock.writeLock().toString().contains("Locked"));
1679 // lock.writeLock().unlock();
1680 // assertTrue(lock.writeLock().toString().contains("Unlocked"));
1681 // }
1682
1683 }