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.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.9 by dl, Mon Nov 3 13:50:07 2003 UTC

# Line 8 | Line 8
8   import junit.framework.*;
9   import java.util.concurrent.locks.*;
10   import java.util.concurrent.*;
11 + import java.util.*;
12 + import java.io.*;
13  
14 < public class ReentrantLockTest extends TestCase {
13 <    static int HOLD_COUNT_TEST_LIMIT = 20;
14 <
14 > public class ReentrantLockTest extends JSR166TestCase {
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
17      }
18    
18      public static Test suite() {
19          return new TestSuite(ReentrantLockTest.class);
20      }
21  
22 <    private static long SHORT_DELAY_MS = 100;
23 <    private static long MEDIUM_DELAY_MS = 1000;
25 <    private static long LONG_DELAY_MS = 10000;
26 <
27 <    /*
28 <     * Unlocks an unlocked lock, throws Illegal Monitor State
29 <     *
22 >    /**
23 >     * A runnable calling lockInterruptibly
24       */
25 <    
26 <    public void testIllegalMonitorStateException(){
25 >    class InterruptibleLockRunnable implements Runnable {
26 >        final ReentrantLock lock;
27 >        InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
28 >        public void run() {
29 >            try {
30 >                lock.lockInterruptibly();
31 >            } catch(InterruptedException success){}
32 >        }
33 >    }
34 >
35 >
36 >    /**
37 >     * A runnable calling lockInterruptibly that expects to be
38 >     * interrupted
39 >     */
40 >    class InterruptedLockRunnable implements Runnable {
41 >        final ReentrantLock lock;
42 >        InterruptedLockRunnable(ReentrantLock l) { lock = l; }
43 >        public void run() {
44 >            try {
45 >                lock.lockInterruptibly();
46 >                threadShouldThrow();
47 >            } catch(InterruptedException success){}
48 >        }
49 >    }
50 >
51 >    /**
52 >     * Subclass to expose protected methods
53 >     */
54 >    static class PublicReentrantLock extends ReentrantLock {
55 >        PublicReentrantLock() { super(); }
56 >        public Collection<Thread> getQueuedThreads() {
57 >            return super.getQueuedThreads();
58 >        }
59 >        public ConditionObject newCondition() {
60 >            return new PublicCondition(this);
61 >        }
62 >
63 >        static class PublicCondition extends ReentrantLock.ConditionObject {
64 >            PublicCondition(PublicReentrantLock l) { super(l); }
65 >            public Collection<Thread> getWaitingThreads() {
66 >                return super.getWaitingThreads();
67 >            }
68 >        }
69 >
70 >    }
71 >
72 >    /**
73 >     * Constructor sets given fairness
74 >     */
75 >    public void testConstructor() {
76          ReentrantLock rl = new ReentrantLock();
77 <        try{
78 <            rl.unlock();
79 <            fail("Should of thown Illegal Monitor State Exception");
77 >        assertFalse(rl.isFair());
78 >        ReentrantLock r2 = new ReentrantLock(true);
79 >        assertTrue(r2.isFair());
80 >    }
81  
82 <        }catch(IllegalMonitorStateException sucess){}
82 >    /**
83 >     * locking an unlocked lock succeeds
84 >     */
85 >    public void testLock() {
86 >        ReentrantLock rl = new ReentrantLock();
87 >        rl.lock();
88 >        assertTrue(rl.isLocked());
89 >        rl.unlock();
90 >    }
91 >
92 >    /**
93 >     * locking an unlocked fair lock succeeds
94 >     */
95 >    public void testFairLock() {
96 >        ReentrantLock rl = new ReentrantLock(true);
97 >        rl.lock();
98 >        assertTrue(rl.isLocked());
99 >        rl.unlock();
100 >    }
101  
102 +    /**
103 +     * Unlocking an unlocked lock throws IllegalMonitorStateException
104 +     */
105 +    public void testUnlock_IllegalMonitorStateException() {
106 +        ReentrantLock rl = new ReentrantLock();
107 +        try {
108 +            rl.unlock();
109 +            shouldThrow();
110  
111 +        } catch(IllegalMonitorStateException success){}
112      }
113 <    
114 <    /*
115 <     * makes a lock, locks it, tries to aquire the lock in another thread
45 <     * interrupts that thread and waits for an interrupted Exception to
46 <     * be thrown.
113 >
114 >    /**
115 >     * trylock on an unlocked lock succeeds
116       */
117 +    public void testTryLock() {
118 +        ReentrantLock rl = new ReentrantLock();
119 +        assertTrue(rl.tryLock());
120 +        assertTrue(rl.isLocked());
121 +        rl.unlock();
122 +    }
123 +
124  
125 <    public void testInterruptedException(){
125 >    /**
126 >     * getQueueLength reports number of waiting threads
127 >     */
128 >    public void testGetQueueLength() {
129 >        final ReentrantLock lock = new ReentrantLock();
130 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
131 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
132 >        try {
133 >            assertEquals(0, lock.getQueueLength());
134 >            lock.lock();
135 >            t1.start();
136 >            Thread.sleep(SHORT_DELAY_MS);
137 >            assertEquals(1, lock.getQueueLength());
138 >            t2.start();
139 >            Thread.sleep(SHORT_DELAY_MS);
140 >            assertEquals(2, lock.getQueueLength());
141 >            t1.interrupt();
142 >            Thread.sleep(SHORT_DELAY_MS);
143 >            assertEquals(1, lock.getQueueLength());
144 >            lock.unlock();
145 >            Thread.sleep(SHORT_DELAY_MS);
146 >            assertEquals(0, lock.getQueueLength());
147 >            t1.join();
148 >            t2.join();
149 >        } catch(Exception e){
150 >            unexpectedException();
151 >        }
152 >    }
153 >
154 >    /**
155 >     * getQueuedThreads includes waiting threads
156 >     */
157 >    public void testGetQueuedThreads() {
158 >        final PublicReentrantLock lock = new PublicReentrantLock();
159 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
160 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
161 >        try {
162 >            assertTrue(lock.getQueuedThreads().isEmpty());
163 >            lock.lock();
164 >            assertTrue(lock.getQueuedThreads().isEmpty());
165 >            t1.start();
166 >            Thread.sleep(SHORT_DELAY_MS);
167 >            assertTrue(lock.getQueuedThreads().contains(t1));
168 >            t2.start();
169 >            Thread.sleep(SHORT_DELAY_MS);
170 >            assertTrue(lock.getQueuedThreads().contains(t1));
171 >            assertTrue(lock.getQueuedThreads().contains(t2));
172 >            t1.interrupt();
173 >            Thread.sleep(SHORT_DELAY_MS);
174 >            assertFalse(lock.getQueuedThreads().contains(t1));
175 >            assertTrue(lock.getQueuedThreads().contains(t2));
176 >            lock.unlock();
177 >            Thread.sleep(SHORT_DELAY_MS);
178 >            assertTrue(lock.getQueuedThreads().isEmpty());
179 >            t1.join();
180 >            t2.join();
181 >        } catch(Exception e){
182 >            unexpectedException();
183 >        }
184 >    }
185 >
186 >
187 >    /**
188 >     * timed trylock is interruptible.
189 >     */
190 >    public void testInterruptedException2() {
191          final ReentrantLock lock = new ReentrantLock();
192          lock.lock();
193          Thread t = new Thread(new Runnable() {
194 <                public void run(){
195 <                    try{
196 <                        lock.lockInterruptibly();
197 <                        fail("should throw");
198 <                    }catch(InterruptedException sucess){}
194 >                public void run() {
195 >                    try {
196 >                        lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
197 >                        threadShouldThrow();
198 >                    } catch(InterruptedException success){}
199                  }
200              });
201 <        t.start();
202 <        t.interrupt();
203 <        lock.unlock();
204 <    }
201 >        try {
202 >            t.start();
203 >            t.interrupt();
204 >        } catch(Exception e){
205 >            unexpectedException();
206 >        }
207 >    }
208  
65    /*
66     * tests for interrupted exception on a timed wait
67     *
68     */
69    
209  
210 <    public void testInterruptedException2(){
210 >    /**
211 >     * Trylock on a locked lock fails
212 >     */
213 >    public void testTryLockWhenLocked() {
214          final ReentrantLock lock = new ReentrantLock();
215          lock.lock();
216          Thread t = new Thread(new Runnable() {
217 <                public void run(){
218 <                    try{
77 <                        lock.tryLock(1000,TimeUnit.MILLISECONDS);
78 <                        fail("should throw");
79 <                    }catch(InterruptedException sucess){}
217 >                public void run() {
218 >                    threadAssertFalse(lock.tryLock());
219                  }
220              });
221 <        t.start();
222 <        t.interrupt();
223 <    }
221 >        try {
222 >            t.start();
223 >            t.join();
224 >            lock.unlock();
225 >        } catch(Exception e){
226 >            unexpectedException();
227 >        }
228 >    }
229  
230 +    /**
231 +     * Timed trylock on a locked lock times out
232 +     */
233 +    public void testTryLock_Timeout() {
234 +        final ReentrantLock lock = new ReentrantLock();
235 +        lock.lock();
236 +        Thread t = new Thread(new Runnable() {
237 +                public void run() {
238 +                    try {
239 +                        threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
240 +                    } catch (Exception ex) {
241 +                        threadUnexpectedException();
242 +                    }
243 +                }
244 +            });
245 +        try {
246 +            t.start();
247 +            t.join();
248 +            lock.unlock();
249 +        } catch(Exception e){
250 +            unexpectedException();
251 +        }
252 +    }
253      
254 +    /**
255 +     * getHoldCount returns number of recursive holds
256 +     */
257      public void testGetHoldCount() {
258          ReentrantLock lock = new ReentrantLock();
259 <        for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) {
259 >        for(int i = 1; i <= SIZE; i++) {
260              lock.lock();
261              assertEquals(i,lock.getHoldCount());
262          }
263 <        for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) {
263 >        for(int i = SIZE; i > 0; i--) {
264              lock.unlock();
265              assertEquals(i-1,lock.getHoldCount());
266          }
267      }
268      
269    
270 <
271 <
270 >    /**
271 >     * isLocked is true when locked and false when not
272 >     */
273      public void testIsLocked() {
274          final ReentrantLock lock = new ReentrantLock();
275          lock.lock();
# Line 109 | Line 280 | public class ReentrantLockTest extends T
280                  public void run() {
281                      lock.lock();
282                      try {
283 <                        Thread.sleep(SHORT_DELAY_MS * 2);
283 >                        Thread.sleep(SMALL_DELAY_MS);
284                      }
285 <                    catch(Exception e) {}
285 >                    catch(Exception e) {
286 >                        threadUnexpectedException();
287 >                    }
288                      lock.unlock();
289                  }
290              });
291 <        try{
291 >        try {
292              t.start();
293              Thread.sleep(SHORT_DELAY_MS);
294              assertTrue(lock.isLocked());
295              t.join();
296              assertFalse(lock.isLocked());
297          } catch(Exception e){
298 <            fail("unexpected exception");
298 >            unexpectedException();
299 >        }
300 >    }
301 >
302 >
303 >    /**
304 >     * lockInterruptibly is interruptible.
305 >     */
306 >    public void testLockInterruptibly1() {
307 >        final ReentrantLock lock = new ReentrantLock();
308 >        lock.lock();
309 >        Thread t = new Thread(new InterruptedLockRunnable(lock));
310 >        try {
311 >            t.start();
312 >            t.interrupt();
313 >            lock.unlock();
314 >            t.join();
315 >        } catch(Exception e){
316 >            unexpectedException();
317          }
318 +    }
319  
320 +    /**
321 +     * lockInterruptibly succeeds when unlocked, else is interruptible
322 +     */
323 +    public void testLockInterruptibly2() {
324 +        final ReentrantLock lock = new ReentrantLock();
325 +        try {
326 +            lock.lockInterruptibly();
327 +        } catch(Exception e) {
328 +            unexpectedException();
329 +        }
330 +        Thread t = new Thread(new InterruptedLockRunnable(lock));
331 +        try {
332 +            t.start();
333 +            t.interrupt();
334 +            assertTrue(lock.isLocked());
335 +            assertTrue(lock.isHeldByCurrentThread());
336 +            t.join();
337 +        } catch(Exception e){
338 +            unexpectedException();
339 +        }
340      }
341  
342 <    /*
343 <     * current thread locks interruptibly the thread
132 <     * another thread tries to aquire the lock and blocks
133 <     * on the call. interrupt the attempted aquireLock
134 <     * assert that the first lock() call actually locked the lock
135 <     * assert that the current thread is the one holding the lock
342 >    /**
343 >     * Calling await without holding lock throws IllegalMonitorStateException
344       */
345 +    public void testAwait_IllegalMonitor() {
346 +        final ReentrantLock lock = new ReentrantLock();
347 +        final Condition c = lock.newCondition();
348 +        try {
349 +            c.await();
350 +            shouldThrow();
351 +        }
352 +        catch (IllegalMonitorStateException success) {
353 +        }
354 +        catch (Exception ex) {
355 +            unexpectedException();
356 +        }
357 +    }
358 +
359 +    /**
360 +     * Calling signal without holding lock throws IllegalMonitorStateException
361 +     */
362 +    public void testSignal_IllegalMonitor() {
363 +        final ReentrantLock lock = new ReentrantLock();
364 +        final Condition c = lock.newCondition();
365 +        try {
366 +            c.signal();
367 +            shouldThrow();
368 +        }
369 +        catch (IllegalMonitorStateException success) {
370 +        }
371 +        catch (Exception ex) {
372 +            unexpectedException();
373 +        }
374 +    }
375  
376 <    public void testLockedInterruptibly() {
376 >    /**
377 >     * awaitNanos without a signal times out
378 >     */
379 >    public void testAwaitNanos_Timeout() {
380          final ReentrantLock lock = new ReentrantLock();
381 <        try {lock.lockInterruptibly();} catch(Exception e) {}
381 >        final Condition c = lock.newCondition();
382 >        try {
383 >            lock.lock();
384 >            long t = c.awaitNanos(100);
385 >            assertTrue(t <= 0);
386 >            lock.unlock();
387 >        }
388 >        catch (Exception ex) {
389 >            unexpectedException();
390 >        }
391 >    }
392 >
393 >    /**
394 >     *  timed await without a signal times out
395 >     */
396 >    public void testAwait_Timeout() {
397 >        final ReentrantLock lock = new ReentrantLock();
398 >        final Condition c = lock.newCondition();
399 >        try {
400 >            lock.lock();
401 >            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
402 >            lock.unlock();
403 >        }
404 >        catch (Exception ex) {
405 >            unexpectedException();
406 >        }
407 >    }
408 >
409 >    /**
410 >     * awaitUntil without a signal times out
411 >     */
412 >    public void testAwaitUntil_Timeout() {
413 >        final ReentrantLock lock = new ReentrantLock();
414 >        final Condition c = lock.newCondition();
415 >        try {
416 >            lock.lock();
417 >            java.util.Date d = new java.util.Date();
418 >            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
419 >            lock.unlock();
420 >        }
421 >        catch (Exception ex) {
422 >            unexpectedException();
423 >        }
424 >    }
425 >
426 >    /**
427 >     * await returns when signalled
428 >     */
429 >    public void testAwait() {
430 >        final ReentrantLock lock = new ReentrantLock();
431 >        final ReentrantLock.ConditionObject c = lock.newCondition();
432          Thread t = new Thread(new Runnable() {
433                  public void run() {
434                      try {
435 <                        lock.lockInterruptibly();
436 <                        fail("Failed to generate an Interrupted Exception");
435 >                        lock.lock();
436 >                        c.await();
437 >                        lock.unlock();
438                      }
439 <                    catch(InterruptedException e) {}
439 >                    catch(InterruptedException e) {
440 >                        threadUnexpectedException();
441 >                    }
442                  }
443              });
444 <        t.start();
445 <        t.interrupt();
446 <        assertTrue(lock.isLocked());
447 <        assertTrue(lock.isHeldByCurrentThread());
444 >
445 >        try {
446 >            t.start();
447 >            Thread.sleep(SHORT_DELAY_MS);
448 >            lock.lock();
449 >            c.signal();
450 >            lock.unlock();
451 >            t.join(SHORT_DELAY_MS);
452 >            assertFalse(t.isAlive());
453 >        }
454 >        catch (Exception ex) {
455 >            unexpectedException();
456 >        }
457 >    }
458 >
459 >    /**
460 >     * hasWaiters returns true when a thread is waiting, else false
461 >     */
462 >    public void testHasWaiters() {
463 >        final ReentrantLock lock = new ReentrantLock();
464 >        final ReentrantLock.ConditionObject c = lock.newCondition();
465 >        Thread t = new Thread(new Runnable() {
466 >                public void run() {
467 >                    try {
468 >                        lock.lock();
469 >                        threadAssertFalse(c.hasWaiters());
470 >                        threadAssertEquals(0, c.getWaitQueueLength());
471 >                        c.await();
472 >                        lock.unlock();
473 >                    }
474 >                    catch(InterruptedException e) {
475 >                        threadUnexpectedException();
476 >                    }
477 >                }
478 >            });
479 >
480 >        try {
481 >            t.start();
482 >            Thread.sleep(SHORT_DELAY_MS);
483 >            lock.lock();
484 >            assertTrue(c.hasWaiters());
485 >            assertEquals(1, c.getWaitQueueLength());
486 >            c.signal();
487 >            lock.unlock();
488 >            Thread.sleep(SHORT_DELAY_MS);
489 >            lock.lock();
490 >            assertFalse(c.hasWaiters());
491 >            assertEquals(0, c.getWaitQueueLength());
492 >            lock.unlock();
493 >            t.join(SHORT_DELAY_MS);
494 >            assertFalse(t.isAlive());
495 >        }
496 >        catch (Exception ex) {
497 >            unexpectedException();
498 >        }
499 >    }
500 >
501 >    /**
502 >     * getWaitQueueLength returns number of waiting threads
503 >     */
504 >    public void testGetWaitQueueLength() {
505 >        final ReentrantLock lock = new ReentrantLock();
506 >        final ReentrantLock.ConditionObject c = lock.newCondition();
507 >        Thread t1 = new Thread(new Runnable() {
508 >                public void run() {
509 >                    try {
510 >                        lock.lock();
511 >                        threadAssertFalse(c.hasWaiters());
512 >                        threadAssertEquals(0, c.getWaitQueueLength());
513 >                        c.await();
514 >                        lock.unlock();
515 >                    }
516 >                    catch(InterruptedException e) {
517 >                        threadUnexpectedException();
518 >                    }
519 >                }
520 >            });
521 >
522 >        Thread t2 = new Thread(new Runnable() {
523 >                public void run() {
524 >                    try {
525 >                        lock.lock();
526 >                        threadAssertTrue(c.hasWaiters());
527 >                        threadAssertEquals(1, c.getWaitQueueLength());
528 >                        c.await();
529 >                        lock.unlock();
530 >                    }
531 >                    catch(InterruptedException e) {
532 >                        threadUnexpectedException();
533 >                    }
534 >                }
535 >            });
536 >
537 >        try {
538 >            t1.start();
539 >            Thread.sleep(SHORT_DELAY_MS);
540 >            t2.start();
541 >            Thread.sleep(SHORT_DELAY_MS);
542 >            lock.lock();
543 >            assertTrue(c.hasWaiters());
544 >            assertEquals(2, c.getWaitQueueLength());
545 >            c.signalAll();
546 >            lock.unlock();
547 >            Thread.sleep(SHORT_DELAY_MS);
548 >            lock.lock();
549 >            assertFalse(c.hasWaiters());
550 >            assertEquals(0, c.getWaitQueueLength());
551 >            lock.unlock();
552 >            t1.join(SHORT_DELAY_MS);
553 >            t2.join(SHORT_DELAY_MS);
554 >            assertFalse(t1.isAlive());
555 >            assertFalse(t2.isAlive());
556 >        }
557 >        catch (Exception ex) {
558 >            unexpectedException();
559 >        }
560 >    }
561 >
562 >    /**
563 >     * getWaitingThreads returns only and all waiting threads
564 >     */
565 >    public void testGetWaitingThreads() {
566 >        final PublicReentrantLock lock = new PublicReentrantLock();    
567 >        final PublicReentrantLock.PublicCondition c = (PublicReentrantLock.PublicCondition)lock.newCondition();
568 >        Thread t1 = new Thread(new Runnable() {
569 >                public void run() {
570 >                    try {
571 >                        lock.lock();
572 >                        threadAssertTrue(c.getWaitingThreads().isEmpty());
573 >                        c.await();
574 >                        lock.unlock();
575 >                    }
576 >                    catch(InterruptedException e) {
577 >                        threadUnexpectedException();
578 >                    }
579 >                }
580 >            });
581 >
582 >        Thread t2 = new Thread(new Runnable() {
583 >                public void run() {
584 >                    try {
585 >                        lock.lock();
586 >                        threadAssertFalse(c.getWaitingThreads().isEmpty());
587 >                        c.await();
588 >                        lock.unlock();
589 >                    }
590 >                    catch(InterruptedException e) {
591 >                        threadUnexpectedException();
592 >                    }
593 >                }
594 >            });
595 >
596 >        try {
597 >            lock.lock();
598 >            assertTrue(c.getWaitingThreads().isEmpty());
599 >            lock.unlock();
600 >            t1.start();
601 >            Thread.sleep(SHORT_DELAY_MS);
602 >            t2.start();
603 >            Thread.sleep(SHORT_DELAY_MS);
604 >            lock.lock();
605 >            assertTrue(c.hasWaiters());
606 >            assertTrue(c.getWaitingThreads().contains(t1));
607 >            assertTrue(c.getWaitingThreads().contains(t2));
608 >            c.signalAll();
609 >            lock.unlock();
610 >            Thread.sleep(SHORT_DELAY_MS);
611 >            lock.lock();
612 >            assertFalse(c.hasWaiters());
613 >            assertTrue(c.getWaitingThreads().isEmpty());
614 >            lock.unlock();
615 >            t1.join(SHORT_DELAY_MS);
616 >            t2.join(SHORT_DELAY_MS);
617 >            assertFalse(t1.isAlive());
618 >            assertFalse(t2.isAlive());
619 >        }
620 >        catch (Exception ex) {
621 >            unexpectedException();
622 >        }
623 >    }
624 >
625 >    /**
626 >     * awaitUninterruptibly doesn't abort on interrupt
627 >     */
628 >    public void testAwaitUninterruptibly() {
629 >        final ReentrantLock lock = new ReentrantLock();
630 >        final Condition c = lock.newCondition();
631 >        Thread t = new Thread(new Runnable() {
632 >                public void run() {
633 >                    lock.lock();
634 >                    c.awaitUninterruptibly();
635 >                    lock.unlock();
636 >                }
637 >            });
638 >
639 >        try {
640 >            t.start();
641 >            Thread.sleep(SHORT_DELAY_MS);
642 >            t.interrupt();
643 >            lock.lock();
644 >            c.signal();
645 >            lock.unlock();
646 >            assert(t.isInterrupted());
647 >            t.join(SHORT_DELAY_MS);
648 >            assertFalse(t.isAlive());
649 >        }
650 >        catch (Exception ex) {
651 >            unexpectedException();
652 >        }
653 >    }
654 >
655 >    /**
656 >     * await is interruptible
657 >     */
658 >    public void testAwait_Interrupt() {
659 >        final ReentrantLock lock = new ReentrantLock();
660 >        final Condition c = lock.newCondition();
661 >        Thread t = new Thread(new Runnable() {
662 >                public void run() {
663 >                    try {
664 >                        lock.lock();
665 >                        c.await();
666 >                        lock.unlock();
667 >                        threadShouldThrow();
668 >                    }
669 >                    catch(InterruptedException success) {
670 >                    }
671 >                }
672 >            });
673 >
674 >        try {
675 >            t.start();
676 >            Thread.sleep(SHORT_DELAY_MS);
677 >            t.interrupt();
678 >            t.join(SHORT_DELAY_MS);
679 >            assertFalse(t.isAlive());
680 >        }
681 >        catch (Exception ex) {
682 >            unexpectedException();
683 >        }
684 >    }
685 >
686 >    /**
687 >     * awaitNanos is interruptible
688 >     */
689 >    public void testAwaitNanos_Interrupt() {
690 >        final ReentrantLock lock = new ReentrantLock();
691 >        final Condition c = lock.newCondition();
692 >        Thread t = new Thread(new Runnable() {
693 >                public void run() {
694 >                    try {
695 >                        lock.lock();
696 >                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
697 >                        lock.unlock();
698 >                        threadShouldThrow();
699 >                    }
700 >                    catch(InterruptedException success) {
701 >                    }
702 >                }
703 >            });
704 >
705 >        try {
706 >            t.start();
707 >            Thread.sleep(SHORT_DELAY_MS);
708 >            t.interrupt();
709 >            t.join(SHORT_DELAY_MS);
710 >            assertFalse(t.isAlive());
711 >        }
712 >        catch (Exception ex) {
713 >            unexpectedException();
714 >        }
715 >    }
716 >
717 >    /**
718 >     * awaitUntil is interruptible
719 >     */
720 >    public void testAwaitUntil_Interrupt() {
721 >        final ReentrantLock lock = new ReentrantLock();
722 >        final Condition c = lock.newCondition();
723 >        Thread t = new Thread(new Runnable() {
724 >                public void run() {
725 >                    try {
726 >                        lock.lock();
727 >                        java.util.Date d = new java.util.Date();
728 >                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
729 >                        lock.unlock();
730 >                        threadShouldThrow();
731 >                    }
732 >                    catch(InterruptedException success) {
733 >                    }
734 >                }
735 >            });
736 >
737 >        try {
738 >            t.start();
739 >            Thread.sleep(SHORT_DELAY_MS);
740 >            t.interrupt();
741 >            t.join(SHORT_DELAY_MS);
742 >            assertFalse(t.isAlive());
743 >        }
744 >        catch (Exception ex) {
745 >            unexpectedException();
746 >        }
747 >    }
748 >
749 >    /**
750 >     * signalAll wakes up all threads
751 >     */
752 >    public void testSignalAll() {
753 >        final ReentrantLock lock = new ReentrantLock();
754 >        final Condition c = lock.newCondition();
755 >        Thread t1 = new Thread(new Runnable() {
756 >                public void run() {
757 >                    try {
758 >                        lock.lock();
759 >                        c.await();
760 >                        lock.unlock();
761 >                    }
762 >                    catch(InterruptedException e) {
763 >                        threadUnexpectedException();
764 >                    }
765 >                }
766 >            });
767 >
768 >        Thread t2 = new Thread(new Runnable() {
769 >                public void run() {
770 >                    try {
771 >                        lock.lock();
772 >                        c.await();
773 >                        lock.unlock();
774 >                    }
775 >                    catch(InterruptedException e) {
776 >                        threadUnexpectedException();
777 >                    }
778 >                }
779 >            });
780 >
781 >        try {
782 >            t1.start();
783 >            t2.start();
784 >            Thread.sleep(SHORT_DELAY_MS);
785 >            lock.lock();
786 >            c.signalAll();
787 >            lock.unlock();
788 >            t1.join(SHORT_DELAY_MS);
789 >            t2.join(SHORT_DELAY_MS);
790 >            assertFalse(t1.isAlive());
791 >            assertFalse(t2.isAlive());
792 >        }
793 >        catch (Exception ex) {
794 >            unexpectedException();
795 >        }
796 >    }
797 >
798 >    /**
799 >     * A serialized lock deserializes as unlocked
800 >     */
801 >    public void testSerialization() {
802 >        ReentrantLock l = new ReentrantLock();
803 >        l.lock();
804 >        l.unlock();
805 >
806 >        try {
807 >            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
808 >            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
809 >            out.writeObject(l);
810 >            out.close();
811 >
812 >            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
813 >            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
814 >            ReentrantLock r = (ReentrantLock) in.readObject();
815 >            r.lock();
816 >            r.unlock();
817 >        } catch(Exception e){
818 >            e.printStackTrace();
819 >            unexpectedException();
820 >        }
821      }
155    
822  
823   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines