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.29 by jsr166, Tue Nov 17 13:31:53 2009 UTC vs.
Revision 1.43 by jsr166, Sat May 7 03:36:23 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 9 | Line 9
9   import junit.framework.*;
10   import java.util.concurrent.locks.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.util.*;
14   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);
21 >        return new TestSuite(ReentrantLockTest.class);
22      }
23  
24      /**
# Line 55 | Line 56 | public class ReentrantLockTest extends J
56          public Collection<Thread> getWaitingThreads(Condition c) {
57              return super.getWaitingThreads(c);
58          }
59 +    }
60  
61 +    /**
62 +     * Checks that condition c has no waiters.
63 +     */
64 +    void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
65 +        assertHasWaiters(lock, c, new Thread[] {});
66 +    }
67  
68 +    /**
69 +     * Checks that condition c has exactly the given waiter threads.
70 +     */
71 +    void assertHasWaiters(PublicReentrantLock lock, Condition c,
72 +                          Thread... threads) {
73 +        lock.lock();
74 +        assertEquals(threads.length > 0, lock.hasWaiters(c));
75 +        assertEquals(threads.length, lock.getWaitQueueLength(c));
76 +        assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
77 +        assertEquals(threads.length, lock.getWaitingThreads(c).size());
78 +        assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
79 +                     new HashSet<Thread>(Arrays.asList(threads)));
80 +        lock.unlock();
81      }
82  
83      /**
# Line 72 | Line 93 | public class ReentrantLockTest extends J
93       * locking an unlocked lock succeeds
94       */
95      public void testLock() {
96 <        ReentrantLock rl = new ReentrantLock();
96 >        ReentrantLock rl = new ReentrantLock();
97          rl.lock();
98          assertTrue(rl.isLocked());
99          rl.unlock();
# Line 83 | Line 104 | public class ReentrantLockTest extends J
104       * locking an unlocked fair lock succeeds
105       */
106      public void testFairLock() {
107 <        ReentrantLock rl = new ReentrantLock(true);
107 >        ReentrantLock rl = new ReentrantLock(true);
108          rl.lock();
109          assertTrue(rl.isLocked());
110          rl.unlock();
# Line 93 | Line 114 | public class ReentrantLockTest extends J
114       * Unlocking an unlocked lock throws IllegalMonitorStateException
115       */
116      public void testUnlock_IllegalMonitorStateException() {
117 <        ReentrantLock rl = new ReentrantLock();
118 <        try {
119 <            rl.unlock();
120 <            shouldThrow();
121 <        } catch (IllegalMonitorStateException success) {}
117 >        ReentrantLock rl = new ReentrantLock();
118 >        try {
119 >            rl.unlock();
120 >            shouldThrow();
121 >        } catch (IllegalMonitorStateException success) {}
122      }
123  
124      /**
125       * tryLock on an unlocked lock succeeds
126       */
127      public void testTryLock() {
128 <        ReentrantLock rl = new ReentrantLock();
128 >        ReentrantLock rl = new ReentrantLock();
129          assertTrue(rl.tryLock());
130          assertTrue(rl.isLocked());
131          rl.unlock();
# Line 115 | Line 136 | public class ReentrantLockTest extends J
136       * hasQueuedThreads reports whether there are waiting threads
137       */
138      public void testhasQueuedThreads() throws InterruptedException {
139 <        final ReentrantLock lock = new ReentrantLock();
139 >        final ReentrantLock lock = new ReentrantLock();
140          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
141          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
142          assertFalse(lock.hasQueuedThreads());
143          lock.lock();
144          t1.start();
145 <        Thread.sleep(SHORT_DELAY_MS);
145 >        delay(SHORT_DELAY_MS);
146          assertTrue(lock.hasQueuedThreads());
147          t2.start();
148 <        Thread.sleep(SHORT_DELAY_MS);
148 >        delay(SHORT_DELAY_MS);
149          assertTrue(lock.hasQueuedThreads());
150          t1.interrupt();
151 <        Thread.sleep(SHORT_DELAY_MS);
151 >        delay(SHORT_DELAY_MS);
152          assertTrue(lock.hasQueuedThreads());
153          lock.unlock();
154 <        Thread.sleep(SHORT_DELAY_MS);
154 >        delay(SHORT_DELAY_MS);
155          assertFalse(lock.hasQueuedThreads());
156 <        t1.join();
157 <        t2.join();
156 >        awaitTermination(t1);
157 >        awaitTermination(t2);
158      }
159  
160      /**
161       * getQueueLength reports number of waiting threads
162       */
163      public void testGetQueueLength() throws InterruptedException {
164 <        final ReentrantLock lock = new ReentrantLock();
164 >        final ReentrantLock lock = new ReentrantLock();
165          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
166          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
167          assertEquals(0, lock.getQueueLength());
168          lock.lock();
169          t1.start();
170 <        Thread.sleep(SHORT_DELAY_MS);
170 >        delay(SHORT_DELAY_MS);
171          assertEquals(1, lock.getQueueLength());
172          t2.start();
173 <        Thread.sleep(SHORT_DELAY_MS);
173 >        delay(SHORT_DELAY_MS);
174          assertEquals(2, lock.getQueueLength());
175          t1.interrupt();
176 <        Thread.sleep(SHORT_DELAY_MS);
176 >        delay(SHORT_DELAY_MS);
177          assertEquals(1, lock.getQueueLength());
178          lock.unlock();
179 <        Thread.sleep(SHORT_DELAY_MS);
179 >        delay(SHORT_DELAY_MS);
180          assertEquals(0, lock.getQueueLength());
181 <        t1.join();
182 <        t2.join();
181 >        awaitTermination(t1);
182 >        awaitTermination(t2);
183      }
184  
185      /**
186       * getQueueLength reports number of waiting threads
187       */
188      public void testGetQueueLength_fair() throws InterruptedException {
189 <        final ReentrantLock lock = new ReentrantLock(true);
189 >        final ReentrantLock lock = new ReentrantLock(true);
190          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
191          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
192          assertEquals(0, lock.getQueueLength());
193          lock.lock();
194          t1.start();
195 <        Thread.sleep(SHORT_DELAY_MS);
195 >        delay(SHORT_DELAY_MS);
196          assertEquals(1, lock.getQueueLength());
197          t2.start();
198 <        Thread.sleep(SHORT_DELAY_MS);
198 >        delay(SHORT_DELAY_MS);
199          assertEquals(2, lock.getQueueLength());
200          t1.interrupt();
201 <        Thread.sleep(SHORT_DELAY_MS);
201 >        delay(SHORT_DELAY_MS);
202          assertEquals(1, lock.getQueueLength());
203          lock.unlock();
204 <        Thread.sleep(SHORT_DELAY_MS);
204 >        delay(SHORT_DELAY_MS);
205          assertEquals(0, lock.getQueueLength());
206 <        t1.join();
207 <        t2.join();
206 >        awaitTermination(t1);
207 >        awaitTermination(t2);
208      }
209  
210      /**
211       * hasQueuedThread(null) throws NPE
212       */
213      public void testHasQueuedThreadNPE() {
214 <        final ReentrantLock sync = new ReentrantLock();
214 >        final ReentrantLock sync = new ReentrantLock();
215          try {
216              sync.hasQueuedThread(null);
217              shouldThrow();
# Line 201 | Line 222 | public class ReentrantLockTest extends J
222       * hasQueuedThread reports whether a thread is queued.
223       */
224      public void testHasQueuedThread() throws InterruptedException {
225 <        final ReentrantLock sync = new ReentrantLock();
225 >        final ReentrantLock sync = new ReentrantLock();
226          Thread t1 = new Thread(new InterruptedLockRunnable(sync));
227          Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
228          assertFalse(sync.hasQueuedThread(t1));
229          assertFalse(sync.hasQueuedThread(t2));
230          sync.lock();
231          t1.start();
232 <        Thread.sleep(SHORT_DELAY_MS);
232 >        delay(SHORT_DELAY_MS);
233          assertTrue(sync.hasQueuedThread(t1));
234          t2.start();
235 <        Thread.sleep(SHORT_DELAY_MS);
235 >        delay(SHORT_DELAY_MS);
236          assertTrue(sync.hasQueuedThread(t1));
237          assertTrue(sync.hasQueuedThread(t2));
238          t1.interrupt();
239 <        Thread.sleep(SHORT_DELAY_MS);
239 >        delay(SHORT_DELAY_MS);
240          assertFalse(sync.hasQueuedThread(t1));
241          assertTrue(sync.hasQueuedThread(t2));
242          sync.unlock();
243 <        Thread.sleep(SHORT_DELAY_MS);
243 >        delay(SHORT_DELAY_MS);
244          assertFalse(sync.hasQueuedThread(t1));
245 <        Thread.sleep(SHORT_DELAY_MS);
245 >        delay(SHORT_DELAY_MS);
246          assertFalse(sync.hasQueuedThread(t2));
247 <        t1.join();
248 <        t2.join();
247 >        awaitTermination(t1);
248 >        awaitTermination(t2);
249      }
250  
251  
# Line 232 | Line 253 | public class ReentrantLockTest extends J
253       * getQueuedThreads includes waiting threads
254       */
255      public void testGetQueuedThreads() throws InterruptedException {
256 <        final PublicReentrantLock lock = new PublicReentrantLock();
256 >        final PublicReentrantLock lock = new PublicReentrantLock();
257          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
258          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
259          assertTrue(lock.getQueuedThreads().isEmpty());
260          lock.lock();
261          assertTrue(lock.getQueuedThreads().isEmpty());
262          t1.start();
263 <        Thread.sleep(SHORT_DELAY_MS);
263 >        delay(SHORT_DELAY_MS);
264          assertTrue(lock.getQueuedThreads().contains(t1));
265          t2.start();
266 <        Thread.sleep(SHORT_DELAY_MS);
266 >        delay(SHORT_DELAY_MS);
267          assertTrue(lock.getQueuedThreads().contains(t1));
268          assertTrue(lock.getQueuedThreads().contains(t2));
269          t1.interrupt();
270 <        Thread.sleep(SHORT_DELAY_MS);
270 >        delay(SHORT_DELAY_MS);
271          assertFalse(lock.getQueuedThreads().contains(t1));
272          assertTrue(lock.getQueuedThreads().contains(t2));
273          lock.unlock();
274 <        Thread.sleep(SHORT_DELAY_MS);
274 >        delay(SHORT_DELAY_MS);
275          assertTrue(lock.getQueuedThreads().isEmpty());
276 <        t1.join();
277 <        t2.join();
276 >        awaitTermination(t1);
277 >        awaitTermination(t2);
278      }
279  
280  
# Line 261 | Line 282 | public class ReentrantLockTest extends J
282       * timed tryLock is interruptible.
283       */
284      public void testInterruptedException2() throws InterruptedException {
285 <        final ReentrantLock lock = new ReentrantLock();
286 <        lock.lock();
287 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
285 >        final ReentrantLock lock = new ReentrantLock();
286 >        lock.lock();
287 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
288              public void realRun() throws InterruptedException {
289 <                lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
289 >                lock.tryLock(MEDIUM_DELAY_MS,MILLISECONDS);
290              }});
291  
292 <        t.start();
292 >        delay(SHORT_DELAY_MS);
293          t.interrupt();
294 <        t.join();
294 >        awaitTermination(t);
295      }
296  
297  
# Line 278 | Line 299 | public class ReentrantLockTest extends J
299       * TryLock on a locked lock fails
300       */
301      public void testTryLockWhenLocked() throws InterruptedException {
302 <        final ReentrantLock lock = new ReentrantLock();
303 <        lock.lock();
304 <        Thread t = new Thread(new CheckedRunnable() {
302 >        final ReentrantLock lock = new ReentrantLock();
303 >        lock.lock();
304 >        Thread t = newStartedThread(new CheckedRunnable() {
305              public void realRun() {
306 <                threadAssertFalse(lock.tryLock());
306 >                assertFalse(lock.tryLock());
307              }});
308  
309 <        t.start();
289 <        t.join();
309 >        awaitTermination(t);
310          lock.unlock();
311      }
312  
# Line 294 | Line 314 | public class ReentrantLockTest extends J
314       * Timed tryLock on a locked lock times out
315       */
316      public void testTryLock_Timeout() throws InterruptedException {
317 <        final ReentrantLock lock = new ReentrantLock();
318 <        lock.lock();
319 <        Thread t = new Thread(new CheckedRunnable() {
317 >        final ReentrantLock lock = new ReentrantLock();
318 >        lock.lock();
319 >        Thread t = newStartedThread(new CheckedRunnable() {
320              public void realRun() throws InterruptedException {
321 <                threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
321 >                assertFalse(lock.tryLock(1, MILLISECONDS));
322              }});
323  
324 <        t.start();
305 <        t.join();
324 >        awaitTermination(t);
325          lock.unlock();
326      }
327  
# Line 310 | Line 329 | public class ReentrantLockTest extends J
329       * getHoldCount returns number of recursive holds
330       */
331      public void testGetHoldCount() {
332 <        ReentrantLock lock = new ReentrantLock();
333 <        for (int i = 1; i <= SIZE; i++) {
334 <            lock.lock();
335 <            assertEquals(i, lock.getHoldCount());
336 <        }
337 <        for (int i = SIZE; i > 0; i--) {
338 <            lock.unlock();
339 <            assertEquals(i-1, lock.getHoldCount());
340 <        }
332 >        ReentrantLock lock = new ReentrantLock();
333 >        for (int i = 1; i <= SIZE; i++) {
334 >            lock.lock();
335 >            assertEquals(i, lock.getHoldCount());
336 >        }
337 >        for (int i = SIZE; i > 0; i--) {
338 >            lock.unlock();
339 >            assertEquals(i-1, lock.getHoldCount());
340 >        }
341      }
342  
343  
# Line 326 | Line 345 | public class ReentrantLockTest extends J
345       * isLocked is true when locked and false when not
346       */
347      public void testIsLocked() throws InterruptedException {
348 <        final ReentrantLock lock = new ReentrantLock();
349 <        lock.lock();
350 <        assertTrue(lock.isLocked());
351 <        lock.unlock();
352 <        assertFalse(lock.isLocked());
353 <        Thread t = new Thread(new CheckedRunnable() {
348 >        final ReentrantLock lock = new ReentrantLock();
349 >        lock.lock();
350 >        assertTrue(lock.isLocked());
351 >        lock.unlock();
352 >        assertFalse(lock.isLocked());
353 >        Thread t = newStartedThread(new CheckedRunnable() {
354              public void realRun() throws InterruptedException {
355                  lock.lock();
356 <                Thread.sleep(SMALL_DELAY_MS);
356 >                delay(SMALL_DELAY_MS);
357                  lock.unlock();
358              }});
359  
360 <        t.start();
342 <        Thread.sleep(SHORT_DELAY_MS);
360 >        delay(SHORT_DELAY_MS);
361          assertTrue(lock.isLocked());
362 <        t.join();
362 >        awaitTermination(t);
363          assertFalse(lock.isLocked());
364      }
365  
# Line 350 | Line 368 | public class ReentrantLockTest extends J
368       * lockInterruptibly is interruptible.
369       */
370      public void testLockInterruptibly1() throws InterruptedException {
371 <        final ReentrantLock lock = new ReentrantLock();
372 <        lock.lock();
373 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
374 <        t.start();
357 <        Thread.sleep(SHORT_DELAY_MS);
371 >        final ReentrantLock lock = new ReentrantLock();
372 >        lock.lock();
373 >        Thread t = newStartedThread(new InterruptedLockRunnable(lock));
374 >        delay(SHORT_DELAY_MS);
375          t.interrupt();
376 <        Thread.sleep(SHORT_DELAY_MS);
376 >        delay(SHORT_DELAY_MS);
377          lock.unlock();
378 <        t.join();
378 >        awaitTermination(t);
379      }
380  
381      /**
382       * lockInterruptibly succeeds when unlocked, else is interruptible
383       */
384      public void testLockInterruptibly2() throws InterruptedException {
385 <        final ReentrantLock lock = new ReentrantLock();
385 >        final ReentrantLock lock = new ReentrantLock();
386          lock.lockInterruptibly();
387 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
388 <        t.start();
387 >        Thread t = newStartedThread(new InterruptedLockRunnable(lock));
388 >        delay(SHORT_DELAY_MS);
389          t.interrupt();
390          assertTrue(lock.isLocked());
391          assertTrue(lock.isHeldByCurrentThread());
392 <        t.join();
392 >        awaitTermination(t);
393      }
394  
395      /**
396       * Calling await without holding lock throws IllegalMonitorStateException
397       */
398      public void testAwait_IllegalMonitor() throws InterruptedException {
399 <        final ReentrantLock lock = new ReentrantLock();
399 >        final ReentrantLock lock = new ReentrantLock();
400          final Condition c = lock.newCondition();
401          try {
402              c.await();
# Line 391 | Line 408 | public class ReentrantLockTest extends J
408       * Calling signal without holding lock throws IllegalMonitorStateException
409       */
410      public void testSignal_IllegalMonitor() {
411 <        final ReentrantLock lock = new ReentrantLock();
411 >        final ReentrantLock lock = new ReentrantLock();
412          final Condition c = lock.newCondition();
413          try {
414              c.signal();
# Line 403 | Line 420 | public class ReentrantLockTest extends J
420       * awaitNanos without a signal times out
421       */
422      public void testAwaitNanos_Timeout() throws InterruptedException {
423 <        final ReentrantLock lock = new ReentrantLock();
423 >        final ReentrantLock lock = new ReentrantLock();
424          final Condition c = lock.newCondition();
425          lock.lock();
426          long t = c.awaitNanos(100);
# Line 412 | Line 429 | public class ReentrantLockTest extends J
429      }
430  
431      /**
432 <     *  timed await without a signal times out
432 >     * timed await without a signal times out
433       */
434      public void testAwait_Timeout() throws InterruptedException {
435 <        final ReentrantLock lock = new ReentrantLock();
435 >        final ReentrantLock lock = new ReentrantLock();
436          final Condition c = lock.newCondition();
437          lock.lock();
438 <        assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
438 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
439          lock.unlock();
440      }
441  
# Line 426 | Line 443 | public class ReentrantLockTest extends J
443       * awaitUntil without a signal times out
444       */
445      public void testAwaitUntil_Timeout() throws InterruptedException {
446 <        final ReentrantLock lock = new ReentrantLock();
446 >        final ReentrantLock lock = new ReentrantLock();
447          final Condition c = lock.newCondition();
448          lock.lock();
449          java.util.Date d = new java.util.Date();
# Line 438 | Line 455 | public class ReentrantLockTest extends J
455       * await returns when signalled
456       */
457      public void testAwait() throws InterruptedException {
458 <        final ReentrantLock lock = new ReentrantLock();
458 >        final ReentrantLock lock = new ReentrantLock();
459          final Condition c = lock.newCondition();
460 <        Thread t = new Thread(new CheckedRunnable() {
460 >        Thread t = newStartedThread(new CheckedRunnable() {
461              public void realRun() throws InterruptedException {
462                  lock.lock();
463                  c.await();
464                  lock.unlock();
465              }});
466  
467 <        t.start();
451 <        Thread.sleep(SHORT_DELAY_MS);
467 >        delay(SHORT_DELAY_MS);
468          lock.lock();
469          c.signal();
470          lock.unlock();
471 <        t.join(SHORT_DELAY_MS);
456 <        assertFalse(t.isAlive());
471 >        awaitTermination(t);
472      }
473  
474      /**
475       * hasWaiters throws NPE if null
476       */
477      public void testHasWaitersNPE() {
478 <        final ReentrantLock lock = new ReentrantLock();
478 >        final ReentrantLock lock = new ReentrantLock();
479          try {
480              lock.hasWaiters(null);
481              shouldThrow();
# Line 471 | Line 486 | public class ReentrantLockTest extends J
486       * getWaitQueueLength throws NPE if null
487       */
488      public void testGetWaitQueueLengthNPE() {
489 <        final ReentrantLock lock = new ReentrantLock();
489 >        final ReentrantLock lock = new ReentrantLock();
490          try {
491              lock.getWaitQueueLength(null);
492              shouldThrow();
# Line 483 | Line 498 | public class ReentrantLockTest extends J
498       * getWaitingThreads throws NPE if null
499       */
500      public void testGetWaitingThreadsNPE() {
501 <        final PublicReentrantLock lock = new PublicReentrantLock();
501 >        final PublicReentrantLock lock = new PublicReentrantLock();
502          try {
503              lock.getWaitingThreads(null);
504              shouldThrow();
# Line 495 | Line 510 | public class ReentrantLockTest extends J
510       * hasWaiters throws IAE if not owned
511       */
512      public void testHasWaitersIAE() {
513 <        final ReentrantLock lock = new ReentrantLock();
513 >        final ReentrantLock lock = new ReentrantLock();
514          final Condition c = lock.newCondition();
515 <        final ReentrantLock lock2 = new ReentrantLock();
515 >        final ReentrantLock lock2 = new ReentrantLock();
516          try {
517              lock2.hasWaiters(c);
518              shouldThrow();
# Line 508 | Line 523 | public class ReentrantLockTest extends J
523       * hasWaiters throws IMSE if not locked
524       */
525      public void testHasWaitersIMSE() {
526 <        final ReentrantLock lock = new ReentrantLock();
526 >        final ReentrantLock lock = new ReentrantLock();
527          final Condition c = lock.newCondition();
528          try {
529              lock.hasWaiters(c);
# Line 521 | Line 536 | public class ReentrantLockTest extends J
536       * getWaitQueueLength throws IAE if not owned
537       */
538      public void testGetWaitQueueLengthIAE() {
539 <        final ReentrantLock lock = new ReentrantLock();
539 >        final ReentrantLock lock = new ReentrantLock();
540          final Condition c = lock.newCondition();
541 <        final ReentrantLock lock2 = new ReentrantLock();
541 >        final ReentrantLock lock2 = new ReentrantLock();
542          try {
543              lock2.getWaitQueueLength(c);
544              shouldThrow();
# Line 534 | Line 549 | public class ReentrantLockTest extends J
549       * getWaitQueueLength throws IMSE if not locked
550       */
551      public void testGetWaitQueueLengthIMSE() {
552 <        final ReentrantLock lock = new ReentrantLock();
552 >        final ReentrantLock lock = new ReentrantLock();
553          final Condition c = lock.newCondition();
554          try {
555              lock.getWaitQueueLength(c);
# Line 547 | Line 562 | public class ReentrantLockTest extends J
562       * getWaitingThreads throws IAE if not owned
563       */
564      public void testGetWaitingThreadsIAE() {
565 <        final PublicReentrantLock lock = new PublicReentrantLock();
565 >        final PublicReentrantLock lock = new PublicReentrantLock();
566          final Condition c = lock.newCondition();
567 <        final PublicReentrantLock lock2 = new PublicReentrantLock();
567 >        final PublicReentrantLock lock2 = new PublicReentrantLock();
568          try {
569              lock2.getWaitingThreads(c);
570              shouldThrow();
# Line 560 | Line 575 | public class ReentrantLockTest extends J
575       * getWaitingThreads throws IMSE if not locked
576       */
577      public void testGetWaitingThreadsIMSE() {
578 <        final PublicReentrantLock lock = new PublicReentrantLock();
578 >        final PublicReentrantLock lock = new PublicReentrantLock();
579          final Condition c = lock.newCondition();
580          try {
581              lock.getWaitingThreads(c);
# Line 573 | Line 588 | public class ReentrantLockTest extends J
588       * hasWaiters returns true when a thread is waiting, else false
589       */
590      public void testHasWaiters() throws InterruptedException {
591 <        final ReentrantLock lock = new ReentrantLock();
591 >        final ReentrantLock lock = new ReentrantLock();
592          final Condition c = lock.newCondition();
593 <        Thread t = new Thread(new CheckedRunnable() {
593 >        Thread t = newStartedThread(new CheckedRunnable() {
594              public void realRun() throws InterruptedException {
595                  lock.lock();
596 <                threadAssertFalse(lock.hasWaiters(c));
597 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
596 >                assertFalse(lock.hasWaiters(c));
597 >                assertEquals(0, lock.getWaitQueueLength(c));
598                  c.await();
599                  lock.unlock();
600              }});
601  
602 <        t.start();
588 <        Thread.sleep(SHORT_DELAY_MS);
602 >        delay(SHORT_DELAY_MS);
603          lock.lock();
604          assertTrue(lock.hasWaiters(c));
605          assertEquals(1, lock.getWaitQueueLength(c));
606          c.signal();
607          lock.unlock();
608 <        Thread.sleep(SHORT_DELAY_MS);
608 >        delay(SHORT_DELAY_MS);
609          lock.lock();
610          assertFalse(lock.hasWaiters(c));
611          assertEquals(0, lock.getWaitQueueLength(c));
612          lock.unlock();
613 <        t.join(SHORT_DELAY_MS);
600 <        assertFalse(t.isAlive());
613 >        awaitTermination(t);
614      }
615  
616      /**
617       * getWaitQueueLength returns number of waiting threads
618       */
619      public void testGetWaitQueueLength() throws InterruptedException {
620 <        final ReentrantLock lock = new ReentrantLock();
620 >        final ReentrantLock lock = new ReentrantLock();
621          final Condition c = lock.newCondition();
622 <        Thread t1 = new Thread(new CheckedRunnable() {
622 >        Thread t1 = newStartedThread(new CheckedRunnable() {
623              public void realRun() throws InterruptedException {
624                  lock.lock();
625 <                threadAssertFalse(lock.hasWaiters(c));
626 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
625 >                assertFalse(lock.hasWaiters(c));
626 >                assertEquals(0, lock.getWaitQueueLength(c));
627                  c.await();
628                  lock.unlock();
629              }});
630  
631 <        Thread t2 = new Thread(new CheckedRunnable() {
631 >        delay(SHORT_DELAY_MS);
632 >
633 >        Thread t2 = newStartedThread(new CheckedRunnable() {
634              public void realRun() throws InterruptedException {
635                  lock.lock();
636 <                threadAssertTrue(lock.hasWaiters(c));
637 <                threadAssertEquals(1, lock.getWaitQueueLength(c));
636 >                assertTrue(lock.hasWaiters(c));
637 >                assertEquals(1, lock.getWaitQueueLength(c));
638                  c.await();
639                  lock.unlock();
640              }});
641  
642 <        t1.start();
628 <        Thread.sleep(SHORT_DELAY_MS);
629 <        t2.start();
630 <        Thread.sleep(SHORT_DELAY_MS);
642 >        delay(SHORT_DELAY_MS);
643          lock.lock();
644          assertTrue(lock.hasWaiters(c));
645          assertEquals(2, lock.getWaitQueueLength(c));
646          c.signalAll();
647          lock.unlock();
648 <        Thread.sleep(SHORT_DELAY_MS);
648 >        delay(SHORT_DELAY_MS);
649          lock.lock();
650          assertFalse(lock.hasWaiters(c));
651          assertEquals(0, lock.getWaitQueueLength(c));
652          lock.unlock();
653 <        t1.join(SHORT_DELAY_MS);
654 <        t2.join(SHORT_DELAY_MS);
643 <        assertFalse(t1.isAlive());
644 <        assertFalse(t2.isAlive());
653 >        awaitTermination(t1);
654 >        awaitTermination(t2);
655      }
656  
657      /**
658       * getWaitingThreads returns only and all waiting threads
659       */
660      public void testGetWaitingThreads() throws InterruptedException {
661 <        final PublicReentrantLock lock = new PublicReentrantLock();
661 >        final PublicReentrantLock lock = new PublicReentrantLock();
662          final Condition c = lock.newCondition();
663 <        Thread t1 = new Thread(new CheckedRunnable() {
663 >        Thread t1 = new Thread(new CheckedRunnable() {
664              public void realRun() throws InterruptedException {
665                  lock.lock();
666 <                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
666 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
667                  c.await();
668                  lock.unlock();
669              }});
670  
671 <        Thread t2 = new Thread(new CheckedRunnable() {
671 >        Thread t2 = new Thread(new CheckedRunnable() {
672              public void realRun() throws InterruptedException {
673                  lock.lock();
674 <                threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
674 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
675                  c.await();
676                  lock.unlock();
677              }});
# Line 670 | Line 680 | public class ReentrantLockTest extends J
680          assertTrue(lock.getWaitingThreads(c).isEmpty());
681          lock.unlock();
682          t1.start();
683 <        Thread.sleep(SHORT_DELAY_MS);
683 >        delay(SHORT_DELAY_MS);
684          t2.start();
685 <        Thread.sleep(SHORT_DELAY_MS);
685 >        delay(SHORT_DELAY_MS);
686          lock.lock();
687          assertTrue(lock.hasWaiters(c));
688          assertTrue(lock.getWaitingThreads(c).contains(t1));
689          assertTrue(lock.getWaitingThreads(c).contains(t2));
690          c.signalAll();
691          lock.unlock();
692 <        Thread.sleep(SHORT_DELAY_MS);
692 >        delay(SHORT_DELAY_MS);
693          lock.lock();
694          assertFalse(lock.hasWaiters(c));
695          assertTrue(lock.getWaitingThreads(c).isEmpty());
696          lock.unlock();
697 <        t1.join(SHORT_DELAY_MS);
698 <        t2.join(SHORT_DELAY_MS);
689 <        assertFalse(t1.isAlive());
690 <        assertFalse(t2.isAlive());
697 >        awaitTermination(t1);
698 >        awaitTermination(t2);
699      }
700  
701      /** A helper class for uninterruptible wait tests */
# Line 728 | Line 736 | public class ReentrantLockTest extends J
736          thread.start();
737  
738          while (!thread.lockStarted) {
739 <            Thread.sleep(100);
739 >            delay(100);
740          }
741  
742          lock.lock();
# Line 749 | Line 757 | public class ReentrantLockTest extends J
757       * await is interruptible
758       */
759      public void testAwait_Interrupt() throws InterruptedException {
760 <        final ReentrantLock lock = new ReentrantLock();
760 >        final PublicReentrantLock lock = new PublicReentrantLock();
761          final Condition c = lock.newCondition();
762 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
763 <            public void realRun() throws InterruptedException {
762 >        final CountDownLatch locked = new CountDownLatch(1);
763 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
764 >            public void realRun() throws InterruptedException {
765                  lock.lock();
766 <                c.await();
766 >                assertTrue(lock.isLocked());
767 >                assertTrue(lock.isHeldByCurrentThread());
768 >                assertHasNoWaiters(lock, c);
769 >                locked.countDown();
770 >                try {
771 >                    c.await();
772 >                } finally {
773 >                    assertTrue(lock.isLocked());
774 >                    assertTrue(lock.isHeldByCurrentThread());
775 >                    assertHasNoWaiters(lock, c);
776 >                    lock.unlock();
777 >                    assertFalse(Thread.interrupted());
778 >                }
779              }});
780  
781 <        t.start();
782 <        Thread.sleep(SHORT_DELAY_MS);
781 >        locked.await();
782 >        assertHasWaiters(lock, c, t);
783          t.interrupt();
784 <        t.join(SHORT_DELAY_MS);
785 <        assertFalse(t.isAlive());
784 >        awaitTermination(t);
785 >        assertFalse(lock.isLocked());
786      }
787  
788      /**
789       * awaitNanos is interruptible
790       */
791      public void testAwaitNanos_Interrupt() throws InterruptedException {
792 <        final ReentrantLock lock = new ReentrantLock();
792 >        final ReentrantLock lock = new ReentrantLock();
793          final Condition c = lock.newCondition();
794 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
795 <            public void realRun() throws InterruptedException {
794 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
795 >            public void realRun() throws InterruptedException {
796                  lock.lock();
797 <                c.awaitNanos(1000 * 1000 * 1000); // 1 sec
797 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
798              }});
799  
800 <        t.start();
780 <        Thread.sleep(SHORT_DELAY_MS);
800 >        delay(SHORT_DELAY_MS);
801          t.interrupt();
802 <        t.join(SHORT_DELAY_MS);
783 <        assertFalse(t.isAlive());
802 >        awaitTermination(t);
803      }
804  
805      /**
806       * awaitUntil is interruptible
807       */
808      public void testAwaitUntil_Interrupt() throws InterruptedException {
809 <        final ReentrantLock lock = new ReentrantLock();
809 >        final ReentrantLock lock = new ReentrantLock();
810          final Condition c = lock.newCondition();
811 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
812 <            public void realRun() throws InterruptedException {
811 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
812 >            public void realRun() throws InterruptedException {
813                  lock.lock();
814                  java.util.Date d = new java.util.Date();
815                  c.awaitUntil(new java.util.Date(d.getTime() + 10000));
816              }});
817  
818 <        t.start();
800 <        Thread.sleep(SHORT_DELAY_MS);
818 >        delay(SHORT_DELAY_MS);
819          t.interrupt();
820 <        t.join(SHORT_DELAY_MS);
803 <        assertFalse(t.isAlive());
820 >        awaitTermination(t);
821      }
822  
823      /**
824       * signalAll wakes up all threads
825       */
826      public void testSignalAll() throws InterruptedException {
827 <        final ReentrantLock lock = new ReentrantLock();
827 >        final ReentrantLock lock = new ReentrantLock();
828          final Condition c = lock.newCondition();
829 <        Thread t1 = new Thread(new CheckedRunnable() {
829 >        Thread t1 = newStartedThread(new CheckedRunnable() {
830              public void realRun() throws InterruptedException {
831                  lock.lock();
832                  c.await();
833                  lock.unlock();
834              }});
835  
836 <        Thread t2 = new Thread(new CheckedRunnable() {
836 >        Thread t2 = newStartedThread(new CheckedRunnable() {
837              public void realRun() throws InterruptedException {
838                  lock.lock();
839                  c.await();
840                  lock.unlock();
841              }});
842  
843 <        t1.start();
827 <        t2.start();
828 <        Thread.sleep(SHORT_DELAY_MS);
843 >        delay(SHORT_DELAY_MS);
844          lock.lock();
845          c.signalAll();
846          lock.unlock();
847 <        t1.join(SHORT_DELAY_MS);
848 <        t2.join(SHORT_DELAY_MS);
834 <        assertFalse(t1.isAlive());
835 <        assertFalse(t2.isAlive());
847 >        awaitTermination(t1);
848 >        awaitTermination(t2);
849      }
850  
851      /**
852       * await after multiple reentrant locking preserves lock count
853       */
854      public void testAwaitLockCount() throws InterruptedException {
855 <        final ReentrantLock lock = new ReentrantLock();
855 >        final ReentrantLock lock = new ReentrantLock();
856          final Condition c = lock.newCondition();
857 <        Thread t1 = new Thread(new CheckedRunnable() {
857 >        Thread t1 = newStartedThread(new CheckedRunnable() {
858              public void realRun() throws InterruptedException {
859                  lock.lock();
860 <                threadAssertEquals(1, lock.getHoldCount());
860 >                assertEquals(1, lock.getHoldCount());
861                  c.await();
862 <                threadAssertEquals(1, lock.getHoldCount());
862 >                assertEquals(1, lock.getHoldCount());
863                  lock.unlock();
864              }});
865  
866 <        Thread t2 = new Thread(new CheckedRunnable() {
866 >        Thread t2 = newStartedThread(new CheckedRunnable() {
867              public void realRun() throws InterruptedException {
868                  lock.lock();
869                  lock.lock();
870 <                threadAssertEquals(2, lock.getHoldCount());
870 >                assertEquals(2, lock.getHoldCount());
871                  c.await();
872 <                threadAssertEquals(2, lock.getHoldCount());
872 >                assertEquals(2, lock.getHoldCount());
873                  lock.unlock();
874                  lock.unlock();
875              }});
876  
877 <        t1.start();
865 <        t2.start();
866 <        Thread.sleep(SHORT_DELAY_MS);
877 >        delay(SHORT_DELAY_MS);
878          lock.lock();
879          c.signalAll();
880          lock.unlock();
881 <        t1.join(SHORT_DELAY_MS);
882 <        t2.join(SHORT_DELAY_MS);
872 <        assertFalse(t1.isAlive());
873 <        assertFalse(t2.isAlive());
881 >        awaitTermination(t1);
882 >        awaitTermination(t2);
883      }
884  
885      /**
# Line 886 | Line 895 | public class ReentrantLockTest extends J
895              new ObjectOutputStream(new BufferedOutputStream(bout));
896          out.writeObject(l);
897          out.close();
898 <        
898 >
899          ByteArrayInputStream bin =
900              new ByteArrayInputStream(bout.toByteArray());
901          ObjectInputStream in =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines