ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.15 by dl, Mon Dec 29 19:05:40 2003 UTC vs.
Revision 1.23 by dl, Mon Aug 1 19:53:01 2005 UTC

# Line 126 | Line 126 | public class ReentrantReadWriteLockTest
126      /**
127       * getWriteHoldCount returns number of recursive holds
128       */
129 <    public void testGetHoldCount() {
129 >    public void testGetWriteHoldCount() {
130          ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
131          for(int i = 1; i <= SIZE; i++) {
132              lock.writeLock().lock();
# Line 137 | Line 137 | public class ReentrantReadWriteLockTest
137              assertEquals(i-1,lock.getWriteHoldCount());
138          }
139      }
140 +
141 +    /**
142 +     * getReadHoldCount returns number of recursive holds
143 +     */
144 +    public void testGetReadHoldCount() {
145 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
146 +        for(int i = 1; i <= SIZE; i++) {
147 +            lock.readLock().lock();
148 +            assertEquals(i,lock.getReadHoldCount());
149 +        }
150 +        for(int i = SIZE; i > 0; i--) {
151 +            lock.readLock().unlock();
152 +            assertEquals(i-1,lock.getReadHoldCount());
153 +        }
154 +    }
155      
156  
157      /**
# Line 156 | Line 171 | public class ReentrantReadWriteLockTest
171       */
172      public void testWriteLockInterruptibly_Interrupted() {
173          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
159        lock.writeLock().lock();
174          Thread t = new Thread(new Runnable() {
175                  public void run() {
176                      try {
177                          lock.writeLock().lockInterruptibly();
178 <                        threadShouldThrow();
178 >                        lock.writeLock().unlock();
179 >                        lock.writeLock().lockInterruptibly();
180 >                        lock.writeLock().unlock();
181                      } catch(InterruptedException success){}
182                  }
183              });
184          try {
185 +            lock.writeLock().lock();
186              t.start();
187              t.interrupt();
188              lock.writeLock().unlock();
# Line 185 | Line 202 | public class ReentrantReadWriteLockTest
202                  public void run() {
203                      try {
204                          lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
188                        threadShouldThrow();
205                      } catch(InterruptedException success){}
206                  }
207              });
# Line 209 | Line 225 | public class ReentrantReadWriteLockTest
225                  public void run() {
226                      try {
227                          lock.readLock().lockInterruptibly();
212                        threadShouldThrow();
228                      } catch(InterruptedException success){}
229                  }
230              });
# Line 376 | Line 391 | public class ReentrantReadWriteLockTest
391          }
392      }
393  
394 +    /**
395 +     * Read trylock succeeds if write locked by current thread
396 +     */
397 +    public void testReadHoldingWriteLock() {
398 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
399 +        lock.writeLock().lock();
400 +        assertTrue(lock.readLock().tryLock());
401 +        lock.readLock().unlock();
402 +        lock.writeLock().unlock();
403 +    }
404 +
405 +    /**
406 +     * Read lock succeeds if write locked by current thread even if
407 +     * other threads are waiting for readlock
408 +     */
409 +    public void testReadHoldingWriteLock2() {
410 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
411 +        lock.writeLock().lock();
412 +        Thread t1 = new Thread(new Runnable() {
413 +                public void run() {
414 +                    lock.readLock().lock();
415 +                    lock.readLock().unlock();
416 +                }
417 +            });
418 +        Thread t2 = new Thread(new Runnable() {
419 +                public void run() {
420 +                    lock.readLock().lock();
421 +                    lock.readLock().unlock();
422 +                }
423 +            });
424 +
425 +        try {
426 +            t1.start();
427 +            t2.start();
428 +            lock.readLock().lock();
429 +            lock.readLock().unlock();
430 +            Thread.sleep(SHORT_DELAY_MS);
431 +            lock.readLock().lock();
432 +            lock.readLock().unlock();
433 +            lock.writeLock().unlock();
434 +            t1.join(MEDIUM_DELAY_MS);
435 +            t2.join(MEDIUM_DELAY_MS);
436 +            assertTrue(!t1.isAlive());
437 +            assertTrue(!t2.isAlive());
438 +          
439 +        } catch(Exception e){
440 +            unexpectedException();
441 +        }
442 +    }
443 +
444 +    /**
445 +     *  Read lock succeeds if write locked by current thread even if
446 +     * other threads are waiting for writelock
447 +     */
448 +    public void testReadHoldingWriteLock3() {
449 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
450 +        lock.writeLock().lock();
451 +        Thread t1 = new Thread(new Runnable() {
452 +                public void run() {
453 +                    lock.writeLock().lock();
454 +                    lock.writeLock().unlock();
455 +                }
456 +            });
457 +        Thread t2 = new Thread(new Runnable() {
458 +                public void run() {
459 +                    lock.writeLock().lock();
460 +                    lock.writeLock().unlock();
461 +                }
462 +            });
463 +
464 +        try {
465 +            t1.start();
466 +            t2.start();
467 +            lock.readLock().lock();
468 +            lock.readLock().unlock();
469 +            Thread.sleep(SHORT_DELAY_MS);
470 +            lock.readLock().lock();
471 +            lock.readLock().unlock();
472 +            lock.writeLock().unlock();
473 +            t1.join(MEDIUM_DELAY_MS);
474 +            t2.join(MEDIUM_DELAY_MS);
475 +            assertTrue(!t1.isAlive());
476 +            assertTrue(!t2.isAlive());
477 +          
478 +        } catch(Exception e){
479 +            unexpectedException();
480 +        }
481 +    }
482 +
483 +
484 +    /**
485 +     *  Write lock succeeds if write locked by current thread even if
486 +     * other threads are waiting for writelock
487 +     */
488 +    public void testWriteHoldingWriteLock4() {
489 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
490 +        lock.writeLock().lock();
491 +        Thread t1 = new Thread(new Runnable() {
492 +                public void run() {
493 +                    lock.writeLock().lock();
494 +                    lock.writeLock().unlock();
495 +                }
496 +            });
497 +        Thread t2 = new Thread(new Runnable() {
498 +                public void run() {
499 +                    lock.writeLock().lock();
500 +                    lock.writeLock().unlock();
501 +                }
502 +            });
503 +
504 +        try {
505 +            t1.start();
506 +            t2.start();
507 +            lock.writeLock().lock();
508 +            lock.writeLock().unlock();
509 +            Thread.sleep(SHORT_DELAY_MS);
510 +            lock.writeLock().lock();
511 +            lock.writeLock().unlock();
512 +            lock.writeLock().unlock();
513 +            t1.join(MEDIUM_DELAY_MS);
514 +            t2.join(MEDIUM_DELAY_MS);
515 +            assertTrue(!t1.isAlive());
516 +            assertTrue(!t2.isAlive());
517 +          
518 +        } catch(Exception e){
519 +            unexpectedException();
520 +        }
521 +    }
522 +
523 +
524 +    /**
525 +     * Fair Read trylock succeeds if write locked by current thread
526 +     */
527 +    public void testReadHoldingWriteLockFair() {
528 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
529 +        lock.writeLock().lock();
530 +        assertTrue(lock.readLock().tryLock());
531 +        lock.readLock().unlock();
532 +        lock.writeLock().unlock();
533 +    }
534 +
535 +    /**
536 +     * Fair Read lock succeeds if write locked by current thread even if
537 +     * other threads are waiting for readlock
538 +     */
539 +    public void testReadHoldingWriteLockFair2() {
540 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
541 +        lock.writeLock().lock();
542 +        Thread t1 = new Thread(new Runnable() {
543 +                public void run() {
544 +                    lock.readLock().lock();
545 +                    lock.readLock().unlock();
546 +                }
547 +            });
548 +        Thread t2 = new Thread(new Runnable() {
549 +                public void run() {
550 +                    lock.readLock().lock();
551 +                    lock.readLock().unlock();
552 +                }
553 +            });
554 +
555 +        try {
556 +            t1.start();
557 +            t2.start();
558 +            lock.readLock().lock();
559 +            lock.readLock().unlock();
560 +            Thread.sleep(SHORT_DELAY_MS);
561 +            lock.readLock().lock();
562 +            lock.readLock().unlock();
563 +            lock.writeLock().unlock();
564 +            t1.join(MEDIUM_DELAY_MS);
565 +            t2.join(MEDIUM_DELAY_MS);
566 +            assertTrue(!t1.isAlive());
567 +            assertTrue(!t2.isAlive());
568 +          
569 +        } catch(Exception e){
570 +            unexpectedException();
571 +        }
572 +    }
573 +
574 +
575 +    /**
576 +     * Fair Read lock succeeds if write locked by current thread even if
577 +     * other threads are waiting for writelock
578 +     */
579 +    public void testReadHoldingWriteLockFair3() {
580 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
581 +        lock.writeLock().lock();
582 +        Thread t1 = new Thread(new Runnable() {
583 +                public void run() {
584 +                    lock.writeLock().lock();
585 +                    lock.writeLock().unlock();
586 +                }
587 +            });
588 +        Thread t2 = new Thread(new Runnable() {
589 +                public void run() {
590 +                    lock.writeLock().lock();
591 +                    lock.writeLock().unlock();
592 +                }
593 +            });
594 +
595 +        try {
596 +            t1.start();
597 +            t2.start();
598 +            lock.readLock().lock();
599 +            lock.readLock().unlock();
600 +            Thread.sleep(SHORT_DELAY_MS);
601 +            lock.readLock().lock();
602 +            lock.readLock().unlock();
603 +            lock.writeLock().unlock();
604 +            t1.join(MEDIUM_DELAY_MS);
605 +            t2.join(MEDIUM_DELAY_MS);
606 +            assertTrue(!t1.isAlive());
607 +            assertTrue(!t2.isAlive());
608 +          
609 +        } catch(Exception e){
610 +            unexpectedException();
611 +        }
612 +    }
613 +
614 +
615 +    /**
616 +     * Fair Write lock succeeds if write locked by current thread even if
617 +     * other threads are waiting for writelock
618 +     */
619 +    public void testWriteHoldingWriteLockFair4() {
620 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
621 +        lock.writeLock().lock();
622 +        Thread t1 = new Thread(new Runnable() {
623 +                public void run() {
624 +                    lock.writeLock().lock();
625 +                    lock.writeLock().unlock();
626 +                }
627 +            });
628 +        Thread t2 = new Thread(new Runnable() {
629 +                public void run() {
630 +                    lock.writeLock().lock();
631 +                    lock.writeLock().unlock();
632 +                }
633 +            });
634 +
635 +        try {
636 +            t1.start();
637 +            t2.start();
638 +            Thread.sleep(SHORT_DELAY_MS);
639 +            assertTrue(lock.isWriteLockedByCurrentThread());
640 +            assertTrue(lock.getWriteHoldCount() == 1);
641 +            lock.writeLock().lock();
642 +            assertTrue(lock.getWriteHoldCount() == 2);
643 +            lock.writeLock().unlock();
644 +            lock.writeLock().lock();
645 +            lock.writeLock().unlock();
646 +            lock.writeLock().unlock();
647 +            t1.join(MEDIUM_DELAY_MS);
648 +            t2.join(MEDIUM_DELAY_MS);
649 +            assertTrue(!t1.isAlive());
650 +            assertTrue(!t2.isAlive());
651 +          
652 +        } catch(Exception e){
653 +            unexpectedException();
654 +        }
655 +    }
656 +
657  
658      /**
659       * Read tryLock succeeds if readlocked but not writelocked
# Line 591 | Line 869 | public class ReentrantReadWriteLockTest
869          final Condition c = lock.writeLock().newCondition();
870          try {
871              lock.writeLock().lock();
594            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
872              lock.writeLock().unlock();
873          }
874          catch (Exception ex) {
# Line 608 | Line 885 | public class ReentrantReadWriteLockTest
885          try {
886              lock.writeLock().lock();
887              java.util.Date d = new java.util.Date();
611            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
888              lock.writeLock().unlock();
889          }
890          catch (Exception ex) {
# Line 649 | Line 925 | public class ReentrantReadWriteLockTest
925          }
926      }
927  
928 +    /** A helper class for uninterruptible wait tests */
929 +    class UninterruptableThread extends Thread {
930 +        private Lock lock;
931 +        private Condition c;
932 +        
933 +        public volatile boolean canAwake = false;
934 +        public volatile boolean interrupted = false;
935 +        public volatile boolean lockStarted = false;
936 +        
937 +        public UninterruptableThread(Lock lock, Condition c) {
938 +            this.lock = lock;
939 +            this.c = c;
940 +        }
941 +        
942 +        public synchronized void run() {
943 +            lock.lock();
944 +            lockStarted = true;
945 +            
946 +            while (!canAwake) {
947 +                c.awaitUninterruptibly();
948 +            }
949 +            
950 +            interrupted = isInterrupted();
951 +            lock.unlock();
952 +        }
953 +    }
954 +
955      /**
956       * awaitUninterruptibly doesn't abort on interrupt
957       */
958      public void testAwaitUninterruptibly() {
959 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
959 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
960          final Condition c = lock.writeLock().newCondition();
961 <        Thread t = new Thread(new Runnable() {
659 <                public void run() {
660 <                    lock.writeLock().lock();
661 <                    c.awaitUninterruptibly();
662 <                    lock.writeLock().unlock();
663 <                }
664 <            });
961 >        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
962  
963          try {
964 <            t.start();
965 <            Thread.sleep(SHORT_DELAY_MS);
966 <            t.interrupt();
964 >            thread.start();
965 >
966 >            while (!thread.lockStarted) {
967 >                Thread.sleep(100);
968 >            }
969 >
970              lock.writeLock().lock();
971 <            c.signal();
972 <            lock.writeLock().unlock();
973 <            assert(t.isInterrupted());
974 <            t.join(SHORT_DELAY_MS);
975 <            assertFalse(t.isAlive());
976 <        }
977 <        catch (Exception ex) {
971 >            try {
972 >                thread.interrupt();
973 >                thread.canAwake = true;
974 >                c.signal();
975 >            } finally {
976 >                lock.writeLock().unlock();
977 >            }
978 >
979 >            thread.join();
980 >            assertTrue(thread.interrupted);
981 >            assertFalse(thread.isAlive());
982 >        } catch (Exception ex) {
983              unexpectedException();
984          }
985      }
# Line 877 | Line 1182 | public class ReentrantReadWriteLockTest
1182      }
1183  
1184      /**
1185 +     * hasQueuedThread(null) throws NPE
1186 +     */
1187 +    public void testHasQueuedThreadNPE() {
1188 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1189 +        try {
1190 +            sync.hasQueuedThread(null);
1191 +            shouldThrow();
1192 +        } catch (NullPointerException success) {
1193 +        }
1194 +    }
1195 +
1196 +    /**
1197 +     * hasQueuedThread reports whether a thread is queued.
1198 +     */
1199 +    public void testHasQueuedThread() {
1200 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1201 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1202 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1203 +        try {
1204 +            assertFalse(sync.hasQueuedThread(t1));
1205 +            assertFalse(sync.hasQueuedThread(t2));
1206 +            sync.writeLock().lock();
1207 +            t1.start();
1208 +            Thread.sleep(SHORT_DELAY_MS);
1209 +            assertTrue(sync.hasQueuedThread(t1));
1210 +            t2.start();
1211 +            Thread.sleep(SHORT_DELAY_MS);
1212 +            assertTrue(sync.hasQueuedThread(t1));
1213 +            assertTrue(sync.hasQueuedThread(t2));
1214 +            t1.interrupt();
1215 +            Thread.sleep(SHORT_DELAY_MS);
1216 +            assertFalse(sync.hasQueuedThread(t1));
1217 +            assertTrue(sync.hasQueuedThread(t2));
1218 +            sync.writeLock().unlock();
1219 +            Thread.sleep(SHORT_DELAY_MS);
1220 +            assertFalse(sync.hasQueuedThread(t1));
1221 +            Thread.sleep(SHORT_DELAY_MS);
1222 +            assertFalse(sync.hasQueuedThread(t2));
1223 +            t1.join();
1224 +            t2.join();
1225 +        } catch(Exception e){
1226 +            unexpectedException();
1227 +        }
1228 +    }
1229 +
1230 +
1231 +    /**
1232       * getQueueLength reports number of waiting threads
1233       */
1234      public void testGetQueueLength() {
# Line 1224 | Line 1576 | public class ReentrantReadWriteLockTest
1576          }
1577      }
1578  
1579 +    /**
1580 +     * toString indicates current lock state
1581 +     */
1582 +    public void testToString() {
1583 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1584 +        String us = lock.toString();
1585 +        assertTrue(us.indexOf("Write locks = 0") >= 0);
1586 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1587 +        lock.writeLock().lock();
1588 +        String ws = lock.toString();
1589 +        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1590 +        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1591 +        lock.writeLock().unlock();
1592 +        lock.readLock().lock();
1593 +        lock.readLock().lock();
1594 +        String rs = lock.toString();
1595 +        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1596 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1597 +    }
1598 +
1599 +    /**
1600 +     * readLock.toString indicates current lock state
1601 +     */
1602 +    public void testReadLockToString() {
1603 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1604 +        String us = lock.readLock().toString();
1605 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1606 +        lock.readLock().lock();
1607 +        lock.readLock().lock();
1608 +        String rs = lock.readLock().toString();
1609 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1610 +    }
1611 +
1612 +    /**
1613 +     * writeLock.toString indicates current lock state
1614 +     */
1615 +    public void testWriteLockToString() {
1616 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1617 +        String us = lock.writeLock().toString();
1618 +        assertTrue(us.indexOf("Unlocked") >= 0);
1619 +        lock.writeLock().lock();
1620 +        String ls = lock.writeLock().toString();
1621 +        assertTrue(ls.indexOf("Locked") >= 0);
1622 +    }
1623 +
1624   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines