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.2 by dl, Sat Sep 6 19:36:05 2003 UTC vs.
Revision 1.12 by dl, Sat Dec 27 19:26:43 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines