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

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.31 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.41 by jsr166, Sat May 7 02:51:33 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 15 | Line 15 | import java.io.*;
15  
16   public class ReentrantLockTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
21          return new TestSuite(ReentrantLockTest.class);
# Line 56 | Line 56 | public class ReentrantLockTest extends J
56          public Collection<Thread> getWaitingThreads(Condition c) {
57              return super.getWaitingThreads(c);
58          }
59
60
59      }
60  
61      /**
# Line 122 | Line 120 | public class ReentrantLockTest extends J
120          assertFalse(lock.hasQueuedThreads());
121          lock.lock();
122          t1.start();
123 <        Thread.sleep(SHORT_DELAY_MS);
123 >        delay(SHORT_DELAY_MS);
124          assertTrue(lock.hasQueuedThreads());
125          t2.start();
126 <        Thread.sleep(SHORT_DELAY_MS);
126 >        delay(SHORT_DELAY_MS);
127          assertTrue(lock.hasQueuedThreads());
128          t1.interrupt();
129 <        Thread.sleep(SHORT_DELAY_MS);
129 >        delay(SHORT_DELAY_MS);
130          assertTrue(lock.hasQueuedThreads());
131          lock.unlock();
132 <        Thread.sleep(SHORT_DELAY_MS);
132 >        delay(SHORT_DELAY_MS);
133          assertFalse(lock.hasQueuedThreads());
134          t1.join();
135          t2.join();
# Line 147 | Line 145 | public class ReentrantLockTest extends J
145          assertEquals(0, lock.getQueueLength());
146          lock.lock();
147          t1.start();
148 <        Thread.sleep(SHORT_DELAY_MS);
148 >        delay(SHORT_DELAY_MS);
149          assertEquals(1, lock.getQueueLength());
150          t2.start();
151 <        Thread.sleep(SHORT_DELAY_MS);
151 >        delay(SHORT_DELAY_MS);
152          assertEquals(2, lock.getQueueLength());
153          t1.interrupt();
154 <        Thread.sleep(SHORT_DELAY_MS);
154 >        delay(SHORT_DELAY_MS);
155          assertEquals(1, lock.getQueueLength());
156          lock.unlock();
157 <        Thread.sleep(SHORT_DELAY_MS);
157 >        delay(SHORT_DELAY_MS);
158          assertEquals(0, lock.getQueueLength());
159          t1.join();
160          t2.join();
# Line 172 | Line 170 | public class ReentrantLockTest extends J
170          assertEquals(0, lock.getQueueLength());
171          lock.lock();
172          t1.start();
173 <        Thread.sleep(SHORT_DELAY_MS);
173 >        delay(SHORT_DELAY_MS);
174          assertEquals(1, lock.getQueueLength());
175          t2.start();
176 <        Thread.sleep(SHORT_DELAY_MS);
176 >        delay(SHORT_DELAY_MS);
177          assertEquals(2, lock.getQueueLength());
178          t1.interrupt();
179 <        Thread.sleep(SHORT_DELAY_MS);
179 >        delay(SHORT_DELAY_MS);
180          assertEquals(1, lock.getQueueLength());
181          lock.unlock();
182 <        Thread.sleep(SHORT_DELAY_MS);
182 >        delay(SHORT_DELAY_MS);
183          assertEquals(0, lock.getQueueLength());
184          t1.join();
185          t2.join();
# Line 209 | Line 207 | public class ReentrantLockTest extends J
207          assertFalse(sync.hasQueuedThread(t2));
208          sync.lock();
209          t1.start();
210 <        Thread.sleep(SHORT_DELAY_MS);
210 >        delay(SHORT_DELAY_MS);
211          assertTrue(sync.hasQueuedThread(t1));
212          t2.start();
213 <        Thread.sleep(SHORT_DELAY_MS);
213 >        delay(SHORT_DELAY_MS);
214          assertTrue(sync.hasQueuedThread(t1));
215          assertTrue(sync.hasQueuedThread(t2));
216          t1.interrupt();
217 <        Thread.sleep(SHORT_DELAY_MS);
217 >        delay(SHORT_DELAY_MS);
218          assertFalse(sync.hasQueuedThread(t1));
219          assertTrue(sync.hasQueuedThread(t2));
220          sync.unlock();
221 <        Thread.sleep(SHORT_DELAY_MS);
221 >        delay(SHORT_DELAY_MS);
222          assertFalse(sync.hasQueuedThread(t1));
223 <        Thread.sleep(SHORT_DELAY_MS);
223 >        delay(SHORT_DELAY_MS);
224          assertFalse(sync.hasQueuedThread(t2));
225          t1.join();
226          t2.join();
# Line 240 | Line 238 | public class ReentrantLockTest extends J
238          lock.lock();
239          assertTrue(lock.getQueuedThreads().isEmpty());
240          t1.start();
241 <        Thread.sleep(SHORT_DELAY_MS);
241 >        delay(SHORT_DELAY_MS);
242          assertTrue(lock.getQueuedThreads().contains(t1));
243          t2.start();
244 <        Thread.sleep(SHORT_DELAY_MS);
244 >        delay(SHORT_DELAY_MS);
245          assertTrue(lock.getQueuedThreads().contains(t1));
246          assertTrue(lock.getQueuedThreads().contains(t2));
247          t1.interrupt();
248 <        Thread.sleep(SHORT_DELAY_MS);
248 >        delay(SHORT_DELAY_MS);
249          assertFalse(lock.getQueuedThreads().contains(t1));
250          assertTrue(lock.getQueuedThreads().contains(t2));
251          lock.unlock();
252 <        Thread.sleep(SHORT_DELAY_MS);
252 >        delay(SHORT_DELAY_MS);
253          assertTrue(lock.getQueuedThreads().isEmpty());
254          t1.join();
255          t2.join();
# Line 264 | Line 262 | public class ReentrantLockTest extends J
262      public void testInterruptedException2() throws InterruptedException {
263          final ReentrantLock lock = new ReentrantLock();
264          lock.lock();
265 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
265 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
266              public void realRun() throws InterruptedException {
267                  lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS);
268              }});
269  
270 <        t.start();
270 >        delay(SHORT_DELAY_MS);
271          t.interrupt();
272          t.join();
273      }
# Line 281 | Line 279 | public class ReentrantLockTest extends J
279      public void testTryLockWhenLocked() throws InterruptedException {
280          final ReentrantLock lock = new ReentrantLock();
281          lock.lock();
282 <        Thread t = new Thread(new CheckedRunnable() {
282 >        Thread t = newStartedThread(new CheckedRunnable() {
283              public void realRun() {
284 <                threadAssertFalse(lock.tryLock());
284 >                assertFalse(lock.tryLock());
285              }});
286  
289        t.start();
287          t.join();
288          lock.unlock();
289      }
# Line 297 | Line 294 | public class ReentrantLockTest extends J
294      public void testTryLock_Timeout() throws InterruptedException {
295          final ReentrantLock lock = new ReentrantLock();
296          lock.lock();
297 <        Thread t = new Thread(new CheckedRunnable() {
297 >        Thread t = newStartedThread(new CheckedRunnable() {
298              public void realRun() throws InterruptedException {
299 <                threadAssertFalse(lock.tryLock(1, MILLISECONDS));
299 >                assertFalse(lock.tryLock(1, MILLISECONDS));
300              }});
301  
305        t.start();
302          t.join();
303          lock.unlock();
304      }
# Line 332 | Line 328 | public class ReentrantLockTest extends J
328          assertTrue(lock.isLocked());
329          lock.unlock();
330          assertFalse(lock.isLocked());
331 <        Thread t = new Thread(new CheckedRunnable() {
331 >        Thread t = newStartedThread(new CheckedRunnable() {
332              public void realRun() throws InterruptedException {
333                  lock.lock();
334 <                Thread.sleep(SMALL_DELAY_MS);
334 >                delay(SMALL_DELAY_MS);
335                  lock.unlock();
336              }});
337  
338 <        t.start();
343 <        Thread.sleep(SHORT_DELAY_MS);
338 >        delay(SHORT_DELAY_MS);
339          assertTrue(lock.isLocked());
340          t.join();
341          assertFalse(lock.isLocked());
# Line 353 | Line 348 | public class ReentrantLockTest extends J
348      public void testLockInterruptibly1() throws InterruptedException {
349          final ReentrantLock lock = new ReentrantLock();
350          lock.lock();
351 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
352 <        t.start();
358 <        Thread.sleep(SHORT_DELAY_MS);
351 >        Thread t = newStartedThread(new InterruptedLockRunnable(lock));
352 >        delay(SHORT_DELAY_MS);
353          t.interrupt();
354 <        Thread.sleep(SHORT_DELAY_MS);
354 >        delay(SHORT_DELAY_MS);
355          lock.unlock();
356          t.join();
357      }
# Line 368 | Line 362 | public class ReentrantLockTest extends J
362      public void testLockInterruptibly2() throws InterruptedException {
363          final ReentrantLock lock = new ReentrantLock();
364          lock.lockInterruptibly();
365 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
366 <        t.start();
365 >        Thread t = newStartedThread(new InterruptedLockRunnable(lock));
366 >        delay(SHORT_DELAY_MS);
367          t.interrupt();
368          assertTrue(lock.isLocked());
369          assertTrue(lock.isHeldByCurrentThread());
# Line 413 | Line 407 | public class ReentrantLockTest extends J
407      }
408  
409      /**
410 <     *  timed await without a signal times out
410 >     * timed await without a signal times out
411       */
412      public void testAwait_Timeout() throws InterruptedException {
413          final ReentrantLock lock = new ReentrantLock();
# Line 441 | Line 435 | public class ReentrantLockTest extends J
435      public void testAwait() throws InterruptedException {
436          final ReentrantLock lock = new ReentrantLock();
437          final Condition c = lock.newCondition();
438 <        Thread t = new Thread(new CheckedRunnable() {
438 >        Thread t = newStartedThread(new CheckedRunnable() {
439              public void realRun() throws InterruptedException {
440                  lock.lock();
441                  c.await();
442                  lock.unlock();
443              }});
444  
445 <        t.start();
452 <        Thread.sleep(SHORT_DELAY_MS);
445 >        delay(SHORT_DELAY_MS);
446          lock.lock();
447          c.signal();
448          lock.unlock();
# Line 576 | Line 569 | public class ReentrantLockTest extends J
569      public void testHasWaiters() throws InterruptedException {
570          final ReentrantLock lock = new ReentrantLock();
571          final Condition c = lock.newCondition();
572 <        Thread t = new Thread(new CheckedRunnable() {
572 >        Thread t = newStartedThread(new CheckedRunnable() {
573              public void realRun() throws InterruptedException {
574                  lock.lock();
575 <                threadAssertFalse(lock.hasWaiters(c));
576 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
575 >                assertFalse(lock.hasWaiters(c));
576 >                assertEquals(0, lock.getWaitQueueLength(c));
577                  c.await();
578                  lock.unlock();
579              }});
580  
581 <        t.start();
589 <        Thread.sleep(SHORT_DELAY_MS);
581 >        delay(SHORT_DELAY_MS);
582          lock.lock();
583          assertTrue(lock.hasWaiters(c));
584          assertEquals(1, lock.getWaitQueueLength(c));
585          c.signal();
586          lock.unlock();
587 <        Thread.sleep(SHORT_DELAY_MS);
587 >        delay(SHORT_DELAY_MS);
588          lock.lock();
589          assertFalse(lock.hasWaiters(c));
590          assertEquals(0, lock.getWaitQueueLength(c));
# Line 607 | Line 599 | public class ReentrantLockTest extends J
599      public void testGetWaitQueueLength() throws InterruptedException {
600          final ReentrantLock lock = new ReentrantLock();
601          final Condition c = lock.newCondition();
602 <        Thread t1 = new Thread(new CheckedRunnable() {
602 >        Thread t1 = newStartedThread(new CheckedRunnable() {
603              public void realRun() throws InterruptedException {
604                  lock.lock();
605 <                threadAssertFalse(lock.hasWaiters(c));
606 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
605 >                assertFalse(lock.hasWaiters(c));
606 >                assertEquals(0, lock.getWaitQueueLength(c));
607                  c.await();
608                  lock.unlock();
609              }});
610  
611 <        Thread t2 = new Thread(new CheckedRunnable() {
611 >        delay(SHORT_DELAY_MS);
612 >
613 >        Thread t2 = newStartedThread(new CheckedRunnable() {
614              public void realRun() throws InterruptedException {
615                  lock.lock();
616 <                threadAssertTrue(lock.hasWaiters(c));
617 <                threadAssertEquals(1, lock.getWaitQueueLength(c));
616 >                assertTrue(lock.hasWaiters(c));
617 >                assertEquals(1, lock.getWaitQueueLength(c));
618                  c.await();
619                  lock.unlock();
620              }});
621  
622 <        t1.start();
629 <        Thread.sleep(SHORT_DELAY_MS);
630 <        t2.start();
631 <        Thread.sleep(SHORT_DELAY_MS);
622 >        delay(SHORT_DELAY_MS);
623          lock.lock();
624          assertTrue(lock.hasWaiters(c));
625          assertEquals(2, lock.getWaitQueueLength(c));
626          c.signalAll();
627          lock.unlock();
628 <        Thread.sleep(SHORT_DELAY_MS);
628 >        delay(SHORT_DELAY_MS);
629          lock.lock();
630          assertFalse(lock.hasWaiters(c));
631          assertEquals(0, lock.getWaitQueueLength(c));
# Line 654 | Line 645 | public class ReentrantLockTest extends J
645          Thread t1 = new Thread(new CheckedRunnable() {
646              public void realRun() throws InterruptedException {
647                  lock.lock();
648 <                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
648 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
649                  c.await();
650                  lock.unlock();
651              }});
# Line 662 | Line 653 | public class ReentrantLockTest extends J
653          Thread t2 = new Thread(new CheckedRunnable() {
654              public void realRun() throws InterruptedException {
655                  lock.lock();
656 <                threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
656 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
657                  c.await();
658                  lock.unlock();
659              }});
# Line 671 | Line 662 | public class ReentrantLockTest extends J
662          assertTrue(lock.getWaitingThreads(c).isEmpty());
663          lock.unlock();
664          t1.start();
665 <        Thread.sleep(SHORT_DELAY_MS);
665 >        delay(SHORT_DELAY_MS);
666          t2.start();
667 <        Thread.sleep(SHORT_DELAY_MS);
667 >        delay(SHORT_DELAY_MS);
668          lock.lock();
669          assertTrue(lock.hasWaiters(c));
670          assertTrue(lock.getWaitingThreads(c).contains(t1));
671          assertTrue(lock.getWaitingThreads(c).contains(t2));
672          c.signalAll();
673          lock.unlock();
674 <        Thread.sleep(SHORT_DELAY_MS);
674 >        delay(SHORT_DELAY_MS);
675          lock.lock();
676          assertFalse(lock.hasWaiters(c));
677          assertTrue(lock.getWaitingThreads(c).isEmpty());
# Line 729 | Line 720 | public class ReentrantLockTest extends J
720          thread.start();
721  
722          while (!thread.lockStarted) {
723 <            Thread.sleep(100);
723 >            delay(100);
724          }
725  
726          lock.lock();
# Line 752 | Line 743 | public class ReentrantLockTest extends J
743      public void testAwait_Interrupt() throws InterruptedException {
744          final ReentrantLock lock = new ReentrantLock();
745          final Condition c = lock.newCondition();
746 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
746 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
747              public void realRun() throws InterruptedException {
748                  lock.lock();
749                  c.await();
750              }});
751  
752 <        t.start();
762 <        Thread.sleep(SHORT_DELAY_MS);
752 >        delay(SHORT_DELAY_MS);
753          t.interrupt();
754          t.join(SHORT_DELAY_MS);
755          assertFalse(t.isAlive());
# Line 771 | Line 761 | public class ReentrantLockTest extends J
761      public void testAwaitNanos_Interrupt() throws InterruptedException {
762          final ReentrantLock lock = new ReentrantLock();
763          final Condition c = lock.newCondition();
764 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
764 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
765              public void realRun() throws InterruptedException {
766                  lock.lock();
767 <                c.awaitNanos(1000 * 1000 * 1000); // 1 sec
767 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
768              }});
769  
770 <        t.start();
781 <        Thread.sleep(SHORT_DELAY_MS);
770 >        delay(SHORT_DELAY_MS);
771          t.interrupt();
772          t.join(SHORT_DELAY_MS);
773          assertFalse(t.isAlive());
# Line 790 | Line 779 | public class ReentrantLockTest extends J
779      public void testAwaitUntil_Interrupt() throws InterruptedException {
780          final ReentrantLock lock = new ReentrantLock();
781          final Condition c = lock.newCondition();
782 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
782 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
783              public void realRun() throws InterruptedException {
784                  lock.lock();
785                  java.util.Date d = new java.util.Date();
786                  c.awaitUntil(new java.util.Date(d.getTime() + 10000));
787              }});
788  
789 <        t.start();
801 <        Thread.sleep(SHORT_DELAY_MS);
789 >        delay(SHORT_DELAY_MS);
790          t.interrupt();
791          t.join(SHORT_DELAY_MS);
792          assertFalse(t.isAlive());
# Line 810 | Line 798 | public class ReentrantLockTest extends J
798      public void testSignalAll() throws InterruptedException {
799          final ReentrantLock lock = new ReentrantLock();
800          final Condition c = lock.newCondition();
801 <        Thread t1 = new Thread(new CheckedRunnable() {
801 >        Thread t1 = newStartedThread(new CheckedRunnable() {
802              public void realRun() throws InterruptedException {
803                  lock.lock();
804                  c.await();
805                  lock.unlock();
806              }});
807  
808 <        Thread t2 = new Thread(new CheckedRunnable() {
808 >        Thread t2 = newStartedThread(new CheckedRunnable() {
809              public void realRun() throws InterruptedException {
810                  lock.lock();
811                  c.await();
812                  lock.unlock();
813              }});
814  
815 <        t1.start();
828 <        t2.start();
829 <        Thread.sleep(SHORT_DELAY_MS);
815 >        delay(SHORT_DELAY_MS);
816          lock.lock();
817          c.signalAll();
818          lock.unlock();
# Line 842 | Line 828 | public class ReentrantLockTest extends J
828      public void testAwaitLockCount() throws InterruptedException {
829          final ReentrantLock lock = new ReentrantLock();
830          final Condition c = lock.newCondition();
831 <        Thread t1 = new Thread(new CheckedRunnable() {
831 >        Thread t1 = newStartedThread(new CheckedRunnable() {
832              public void realRun() throws InterruptedException {
833                  lock.lock();
834 <                threadAssertEquals(1, lock.getHoldCount());
834 >                assertEquals(1, lock.getHoldCount());
835                  c.await();
836 <                threadAssertEquals(1, lock.getHoldCount());
836 >                assertEquals(1, lock.getHoldCount());
837                  lock.unlock();
838              }});
839  
840 <        Thread t2 = new Thread(new CheckedRunnable() {
840 >        Thread t2 = newStartedThread(new CheckedRunnable() {
841              public void realRun() throws InterruptedException {
842                  lock.lock();
843                  lock.lock();
844 <                threadAssertEquals(2, lock.getHoldCount());
844 >                assertEquals(2, lock.getHoldCount());
845                  c.await();
846 <                threadAssertEquals(2, lock.getHoldCount());
846 >                assertEquals(2, lock.getHoldCount());
847                  lock.unlock();
848                  lock.unlock();
849              }});
850  
851 <        t1.start();
866 <        t2.start();
867 <        Thread.sleep(SHORT_DELAY_MS);
851 >        delay(SHORT_DELAY_MS);
852          lock.lock();
853          c.signalAll();
854          lock.unlock();
# Line 887 | Line 871 | public class ReentrantLockTest extends J
871              new ObjectOutputStream(new BufferedOutputStream(bout));
872          out.writeObject(l);
873          out.close();
874 <        
874 >
875          ByteArrayInputStream bin =
876              new ByteArrayInputStream(bout.toByteArray());
877          ObjectInputStream in =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines