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.20 by dl, Sun Dec 5 21:56:41 2004 UTC vs.
Revision 1.47 by jsr166, Sun May 15 17:30:21 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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
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 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      /**
25       * A runnable calling lockInterruptibly
26       */
27 <    class InterruptibleLockRunnable implements Runnable {
27 >    class InterruptibleLockRunnable extends CheckedRunnable {
28          final ReentrantLock lock;
29          InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
30 <        public void run() {
31 <            try {
31 <                lock.lockInterruptibly();
32 <            } catch(InterruptedException success){}
30 >        public void realRun() throws InterruptedException {
31 >            lock.lockInterruptibly();
32          }
33      }
34  
36
35      /**
36       * A runnable calling lockInterruptibly that expects to be
37       * interrupted
38       */
39 <    class InterruptedLockRunnable implements Runnable {
39 >    class InterruptedLockRunnable extends CheckedInterruptedRunnable {
40          final ReentrantLock lock;
41          InterruptedLockRunnable(ReentrantLock l) { lock = l; }
42 <        public void run() {
43 <            try {
46 <                lock.lockInterruptibly();
47 <                threadShouldThrow();
48 <            } catch(InterruptedException success){}
42 >        public void realRun() throws InterruptedException {
43 >            lock.lockInterruptibly();
44          }
45      }
46  
# Line 54 | Line 49 | public class ReentrantLockTest extends J
49       */
50      static class PublicReentrantLock extends ReentrantLock {
51          PublicReentrantLock() { super(); }
52 <        public Collection<Thread> getQueuedThreads() {
53 <            return super.getQueuedThreads();
52 >        PublicReentrantLock(boolean fair) { super(fair); }
53 >        public Thread getOwner() {
54 >            return super.getOwner();
55          }
56 <        public Collection<Thread> getWaitingThreads(Condition c) {
57 <            return super.getWaitingThreads(c);
56 >        public Collection<Thread> getQueuedThreads() {
57 >            return super.getQueuedThreads();
58          }
59 +        public Collection<Thread> getWaitingThreads(Condition c) {
60 +            return super.getWaitingThreads(c);
61 +        }
62 +    }
63  
64 +    /**
65 +     * Releases write lock, checking that it had a hold count of 1.
66 +     */
67 +    void releaseLock(PublicReentrantLock lock) {
68 +        assertLockedByMoi(lock);
69 +        lock.unlock();
70 +        assertFalse(lock.isHeldByCurrentThread());
71 +        assertNotLocked(lock);
72 +    }
73  
74 +    /**
75 +     * Spin-waits until lock.hasQueuedThread(t) becomes true.
76 +     */
77 +    void waitForQueuedThread(PublicReentrantLock lock, Thread t) {
78 +        long startTime = System.nanoTime();
79 +        while (!lock.hasQueuedThread(t)) {
80 +            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
81 +                throw new AssertionFailedError("timed out");
82 +            Thread.yield();
83 +        }
84 +        assertTrue(t.isAlive());
85 +        assertTrue(lock.getOwner() != t);
86      }
87  
88      /**
89 <     * Constructor sets given fairness
89 >     * Checks that lock is not locked.
90       */
91 <    public void testConstructor() {
92 <        ReentrantLock rl = new ReentrantLock();
93 <        assertFalse(rl.isFair());
94 <        ReentrantLock r2 = new ReentrantLock(true);
95 <        assertTrue(r2.isFair());
91 >    void assertNotLocked(PublicReentrantLock lock) {
92 >        assertFalse(lock.isLocked());
93 >        assertFalse(lock.isHeldByCurrentThread());
94 >        assertNull(lock.getOwner());
95 >        assertEquals(0, lock.getHoldCount());
96      }
97  
98      /**
99 <     * locking an unlocked lock succeeds
99 >     * Checks that lock is locked by the given thread.
100       */
101 <    public void testLock() {
102 <        ReentrantLock rl = new ReentrantLock();
103 <        rl.lock();
104 <        assertTrue(rl.isLocked());
105 <        rl.unlock();
101 >    void assertLockedBy(PublicReentrantLock lock, Thread t) {
102 >        assertTrue(lock.isLocked());
103 >        assertSame(t, lock.getOwner());
104 >        assertEquals(t == Thread.currentThread(),
105 >                     lock.isHeldByCurrentThread());
106 >        assertEquals(t == Thread.currentThread(),
107 >                     lock.getHoldCount() > 0);
108      }
109  
110      /**
111 <     * locking an unlocked fair lock succeeds
111 >     * Checks that lock is locked by the current thread.
112       */
113 <    public void testFairLock() {
114 <        ReentrantLock rl = new ReentrantLock(true);
92 <        rl.lock();
93 <        assertTrue(rl.isLocked());
94 <        rl.unlock();
113 >    void assertLockedByMoi(PublicReentrantLock lock) {
114 >        assertLockedBy(lock, Thread.currentThread());
115      }
116  
117      /**
118 <     * Unlocking an unlocked lock throws IllegalMonitorStateException
118 >     * Checks that condition c has no waiters.
119       */
120 <    public void testUnlock_IllegalMonitorStateException() {
121 <        ReentrantLock rl = new ReentrantLock();
122 <        try {
103 <            rl.unlock();
104 <            shouldThrow();
120 >    void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
121 >        assertHasWaiters(lock, c, new Thread[] {});
122 >    }
123  
124 <        } catch(IllegalMonitorStateException success){}
124 >    /**
125 >     * Checks that condition c has exactly the given waiter threads.
126 >     */
127 >    void assertHasWaiters(PublicReentrantLock lock, Condition c,
128 >                          Thread... threads) {
129 >        lock.lock();
130 >        assertEquals(threads.length > 0, lock.hasWaiters(c));
131 >        assertEquals(threads.length, lock.getWaitQueueLength(c));
132 >        assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
133 >        assertEquals(threads.length, lock.getWaitingThreads(c).size());
134 >        assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
135 >                     new HashSet<Thread>(Arrays.asList(threads)));
136 >        lock.unlock();
137      }
138  
139 +    enum AwaitMethod { await, awaitNanos, awaitUntil };
140 +
141      /**
142 <     * tryLock on an unlocked lock succeeds
142 >     * Awaits condition using the specified AwaitMethod.
143       */
144 <    public void testTryLock() {
145 <        ReentrantLock rl = new ReentrantLock();
146 <        assertTrue(rl.tryLock());
147 <        assertTrue(rl.isLocked());
148 <        rl.unlock();
144 >    void await(Condition c, AwaitMethod awaitMethod)
145 >            throws InterruptedException {
146 >        switch (awaitMethod) {
147 >        case await:
148 >            c.await();
149 >            break;
150 >        case awaitNanos:
151 >            long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
152 >            assertTrue(nanosRemaining > 0);
153 >            break;
154 >        case awaitUntil:
155 >            java.util.Date d = new java.util.Date();
156 >            assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
157 >            break;
158 >        }
159      }
160  
161 +    /**
162 +     * Constructor sets given fairness, and is in unlocked state
163 +     */
164 +    public void testConstructor() {
165 +        PublicReentrantLock lock;
166 +
167 +        lock = new PublicReentrantLock();
168 +        assertFalse(lock.isFair());
169 +        assertNotLocked(lock);
170 +
171 +        lock = new PublicReentrantLock(true);
172 +        assertTrue(lock.isFair());
173 +        assertNotLocked(lock);
174 +
175 +        lock = new PublicReentrantLock(false);
176 +        assertFalse(lock.isFair());
177 +        assertNotLocked(lock);
178 +    }
179  
180      /**
181 <     * hasQueuedThreads reports whether there are waiting threads
181 >     * locking an unlocked lock succeeds
182       */
183 <    public void testhasQueuedThreads() {
184 <        final ReentrantLock lock = new ReentrantLock();
185 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
186 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
183 >    public void testLock()      { testLock(false); }
184 >    public void testLock_fair() { testLock(true); }
185 >    public void testLock(boolean fair) {
186 >        PublicReentrantLock lock = new PublicReentrantLock(fair);
187 >        lock.lock();
188 >        assertLockedByMoi(lock);
189 >        releaseLock(lock);
190 >    }
191 >
192 >    /**
193 >     * Unlocking an unlocked lock throws IllegalMonitorStateException
194 >     */
195 >    public void testUnlock_IMSE()      { testUnlock_IMSE(false); }
196 >    public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
197 >    public void testUnlock_IMSE(boolean fair) {
198 >        ReentrantLock lock = new ReentrantLock(fair);
199          try {
128            assertFalse(lock.hasQueuedThreads());
129            lock.lock();
130            t1.start();
131            Thread.sleep(SHORT_DELAY_MS);
132            assertTrue(lock.hasQueuedThreads());
133            t2.start();
134            Thread.sleep(SHORT_DELAY_MS);
135            assertTrue(lock.hasQueuedThreads());
136            t1.interrupt();
137            Thread.sleep(SHORT_DELAY_MS);
138            assertTrue(lock.hasQueuedThreads());
200              lock.unlock();
201 <            Thread.sleep(SHORT_DELAY_MS);
202 <            assertFalse(lock.hasQueuedThreads());
203 <            t1.join();
143 <            t2.join();
144 <        } catch(Exception e){
145 <            unexpectedException();
146 <        }
147 <    }
201 >            shouldThrow();
202 >        } catch (IllegalMonitorStateException success) {}
203 >    }
204  
205      /**
206 <     * getQueueLength reports number of waiting threads
206 >     * tryLock on an unlocked lock succeeds
207       */
208 <    public void testGetQueueLength() {
209 <        final ReentrantLock lock = new ReentrantLock();
208 >    public void testTryLock()      { testTryLock(false); }
209 >    public void testTryLock_fair() { testTryLock(true); }
210 >    public void testTryLock(boolean fair) {
211 >        PublicReentrantLock lock = new PublicReentrantLock(fair);
212 >        assertTrue(lock.tryLock());
213 >        assertLockedByMoi(lock);
214 >        assertTrue(lock.tryLock());
215 >        assertLockedByMoi(lock);
216 >        lock.unlock();
217 >        releaseLock(lock);
218 >    }
219 >
220 >    /**
221 >     * hasQueuedThreads reports whether there are waiting threads
222 >     */
223 >    public void testHasQueuedThreads()      { testHasQueuedThreads(false); }
224 >    public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
225 >    public void testHasQueuedThreads(boolean fair) {
226 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
227          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
228          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
229 <        try {
230 <            assertEquals(0, lock.getQueueLength());
231 <            lock.lock();
232 <            t1.start();
233 <            Thread.sleep(SHORT_DELAY_MS);
234 <            assertEquals(1, lock.getQueueLength());
235 <            t2.start();
236 <            Thread.sleep(SHORT_DELAY_MS);
237 <            assertEquals(2, lock.getQueueLength());
238 <            t1.interrupt();
239 <            Thread.sleep(SHORT_DELAY_MS);
240 <            assertEquals(1, lock.getQueueLength());
241 <            lock.unlock();
242 <            Thread.sleep(SHORT_DELAY_MS);
243 <            assertEquals(0, lock.getQueueLength());
244 <            t1.join();
172 <            t2.join();
173 <        } catch(Exception e){
174 <            unexpectedException();
175 <        }
176 <    }
229 >        assertFalse(lock.hasQueuedThreads());
230 >        lock.lock();
231 >        assertFalse(lock.hasQueuedThreads());
232 >        t1.start();
233 >        waitForQueuedThread(lock, t1);
234 >        assertTrue(lock.hasQueuedThreads());
235 >        t2.start();
236 >        waitForQueuedThread(lock, t2);
237 >        assertTrue(lock.hasQueuedThreads());
238 >        t1.interrupt();
239 >        awaitTermination(t1);
240 >        assertTrue(lock.hasQueuedThreads());
241 >        lock.unlock();
242 >        awaitTermination(t2);
243 >        assertFalse(lock.hasQueuedThreads());
244 >    }
245  
246      /**
247       * getQueueLength reports number of waiting threads
248       */
249 <    public void testGetQueueLength_fair() {
250 <        final ReentrantLock lock = new ReentrantLock(true);
249 >    public void testGetQueueLength()      { testGetQueueLength(false); }
250 >    public void testGetQueueLength_fair() { testGetQueueLength(true); }
251 >    public void testGetQueueLength(boolean fair) {
252 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
253          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
254          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
255 <        try {
256 <            assertEquals(0, lock.getQueueLength());
257 <            lock.lock();
258 <            t1.start();
259 <            Thread.sleep(SHORT_DELAY_MS);
260 <            assertEquals(1, lock.getQueueLength());
261 <            t2.start();
262 <            Thread.sleep(SHORT_DELAY_MS);
263 <            assertEquals(2, lock.getQueueLength());
264 <            t1.interrupt();
265 <            Thread.sleep(SHORT_DELAY_MS);
266 <            assertEquals(1, lock.getQueueLength());
267 <            lock.unlock();
268 <            Thread.sleep(SHORT_DELAY_MS);
269 <            assertEquals(0, lock.getQueueLength());
200 <            t1.join();
201 <            t2.join();
202 <        } catch(Exception e){
203 <            unexpectedException();
204 <        }
205 <    }
255 >        assertEquals(0, lock.getQueueLength());
256 >        lock.lock();
257 >        t1.start();
258 >        waitForQueuedThread(lock, t1);
259 >        assertEquals(1, lock.getQueueLength());
260 >        t2.start();
261 >        waitForQueuedThread(lock, t2);
262 >        assertEquals(2, lock.getQueueLength());
263 >        t1.interrupt();
264 >        awaitTermination(t1);
265 >        assertEquals(1, lock.getQueueLength());
266 >        lock.unlock();
267 >        awaitTermination(t2);
268 >        assertEquals(0, lock.getQueueLength());
269 >    }
270  
271      /**
272       * hasQueuedThread(null) throws NPE
273       */
274 <    public void testHasQueuedThreadNPE() {
275 <        final ReentrantLock sync = new ReentrantLock();
274 >    public void testHasQueuedThreadNPE()      { testHasQueuedThreadNPE(false); }
275 >    public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
276 >    public void testHasQueuedThreadNPE(boolean fair) {
277 >        final ReentrantLock lock = new ReentrantLock(fair);
278          try {
279 <            sync.hasQueuedThread(null);
279 >            lock.hasQueuedThread(null);
280              shouldThrow();
281 <        } catch (NullPointerException success) {
216 <        }
281 >        } catch (NullPointerException success) {}
282      }
283  
284      /**
285       * hasQueuedThread reports whether a thread is queued.
286       */
287 <    public void testHasQueuedThread() {
288 <        final ReentrantLock sync = new ReentrantLock();
289 <        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
290 <        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
291 <        try {
292 <            assertFalse(sync.hasQueuedThread(t1));
293 <            assertFalse(sync.hasQueuedThread(t2));
294 <            sync.lock();
295 <            t1.start();
296 <            Thread.sleep(SHORT_DELAY_MS);
297 <            assertTrue(sync.hasQueuedThread(t1));
298 <            t2.start();
299 <            Thread.sleep(SHORT_DELAY_MS);
300 <            assertTrue(sync.hasQueuedThread(t1));
301 <            assertTrue(sync.hasQueuedThread(t2));
302 <            t1.interrupt();
303 <            Thread.sleep(SHORT_DELAY_MS);
304 <            assertFalse(sync.hasQueuedThread(t1));
305 <            assertTrue(sync.hasQueuedThread(t2));
306 <            sync.unlock();
307 <            Thread.sleep(SHORT_DELAY_MS);
308 <            assertFalse(sync.hasQueuedThread(t1));
309 <            Thread.sleep(SHORT_DELAY_MS);
310 <            assertFalse(sync.hasQueuedThread(t2));
311 <            t1.join();
312 <            t2.join();
248 <        } catch(Exception e){
249 <            unexpectedException();
250 <        }
251 <    }
252 <
287 >    public void testHasQueuedThread()      { testHasQueuedThread(false); }
288 >    public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
289 >    public void testHasQueuedThread(boolean fair) {
290 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
291 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
292 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
293 >        assertFalse(lock.hasQueuedThread(t1));
294 >        assertFalse(lock.hasQueuedThread(t2));
295 >        lock.lock();
296 >        t1.start();
297 >        waitForQueuedThread(lock, t1);
298 >        assertTrue(lock.hasQueuedThread(t1));
299 >        assertFalse(lock.hasQueuedThread(t2));
300 >        t2.start();
301 >        waitForQueuedThread(lock, t2);
302 >        assertTrue(lock.hasQueuedThread(t1));
303 >        assertTrue(lock.hasQueuedThread(t2));
304 >        t1.interrupt();
305 >        awaitTermination(t1);
306 >        assertFalse(lock.hasQueuedThread(t1));
307 >        assertTrue(lock.hasQueuedThread(t2));
308 >        lock.unlock();
309 >        awaitTermination(t2);
310 >        assertFalse(lock.hasQueuedThread(t1));
311 >        assertFalse(lock.hasQueuedThread(t2));
312 >    }
313  
314      /**
315       * getQueuedThreads includes waiting threads
316       */
317 <    public void testGetQueuedThreads() {
318 <        final PublicReentrantLock lock = new PublicReentrantLock();
317 >    public void testGetQueuedThreads()      { testGetQueuedThreads(false); }
318 >    public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
319 >    public void testGetQueuedThreads(boolean fair) {
320 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
321          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
322          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
323 <        try {
324 <            assertTrue(lock.getQueuedThreads().isEmpty());
325 <            lock.lock();
326 <            assertTrue(lock.getQueuedThreads().isEmpty());
327 <            t1.start();
328 <            Thread.sleep(SHORT_DELAY_MS);
329 <            assertTrue(lock.getQueuedThreads().contains(t1));
330 <            t2.start();
331 <            Thread.sleep(SHORT_DELAY_MS);
332 <            assertTrue(lock.getQueuedThreads().contains(t1));
333 <            assertTrue(lock.getQueuedThreads().contains(t2));
334 <            t1.interrupt();
335 <            Thread.sleep(SHORT_DELAY_MS);
336 <            assertFalse(lock.getQueuedThreads().contains(t1));
337 <            assertTrue(lock.getQueuedThreads().contains(t2));
338 <            lock.unlock();
339 <            Thread.sleep(SHORT_DELAY_MS);
340 <            assertTrue(lock.getQueuedThreads().isEmpty());
341 <            t1.join();
342 <            t2.join();
343 <        } catch(Exception e){
282 <            unexpectedException();
283 <        }
284 <    }
285 <
323 >        assertTrue(lock.getQueuedThreads().isEmpty());
324 >        lock.lock();
325 >        assertTrue(lock.getQueuedThreads().isEmpty());
326 >        t1.start();
327 >        waitForQueuedThread(lock, t1);
328 >        assertEquals(1, lock.getQueuedThreads().size());
329 >        assertTrue(lock.getQueuedThreads().contains(t1));
330 >        t2.start();
331 >        waitForQueuedThread(lock, t2);
332 >        assertEquals(2, lock.getQueuedThreads().size());
333 >        assertTrue(lock.getQueuedThreads().contains(t1));
334 >        assertTrue(lock.getQueuedThreads().contains(t2));
335 >        t1.interrupt();
336 >        awaitTermination(t1);
337 >        assertFalse(lock.getQueuedThreads().contains(t1));
338 >        assertTrue(lock.getQueuedThreads().contains(t2));
339 >        assertEquals(1, lock.getQueuedThreads().size());
340 >        lock.unlock();
341 >        awaitTermination(t2);
342 >        assertTrue(lock.getQueuedThreads().isEmpty());
343 >    }
344  
345      /**
346       * timed tryLock is interruptible.
347       */
348 <    public void testInterruptedException2() {
349 <        final ReentrantLock lock = new ReentrantLock();
350 <        lock.lock();
351 <        Thread t = new Thread(new Runnable() {
352 <                public void run() {
353 <                    try {
354 <                        lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
355 <                        threadShouldThrow();
356 <                    } catch(InterruptedException success){}
299 <                }
300 <            });
301 <        try {
302 <            t.start();
303 <            t.interrupt();
304 <        } catch(Exception e){
305 <            unexpectedException();
306 <        }
307 <    }
348 >    public void testTryLock_Interruptible()      { testTryLock_Interruptible(false); }
349 >    public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
350 >    public void testTryLock_Interruptible(boolean fair) {
351 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
352 >        lock.lock();
353 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
354 >            public void realRun() throws InterruptedException {
355 >                lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
356 >            }});
357  
358 +        waitForQueuedThread(lock, t);
359 +        t.interrupt();
360 +        awaitTermination(t);
361 +        releaseLock(lock);
362 +    }
363  
364      /**
365 <     * TryLock on a locked lock fails
365 >     * tryLock on a locked lock fails
366       */
367 <    public void testTryLockWhenLocked() {
368 <        final ReentrantLock lock = new ReentrantLock();
369 <        lock.lock();
370 <        Thread t = new Thread(new Runnable() {
371 <                public void run() {
372 <                    threadAssertFalse(lock.tryLock());
373 <                }
374 <            });
375 <        try {
376 <            t.start();
377 <            t.join();
378 <            lock.unlock();
379 <        } catch(Exception e){
326 <            unexpectedException();
327 <        }
328 <    }
367 >    public void testTryLockWhenLocked()      { testTryLockWhenLocked(false); }
368 >    public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); }
369 >    public void testTryLockWhenLocked(boolean fair) {
370 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
371 >        lock.lock();
372 >        Thread t = newStartedThread(new CheckedRunnable() {
373 >            public void realRun() {
374 >                assertFalse(lock.tryLock());
375 >            }});
376 >
377 >        awaitTermination(t);
378 >        releaseLock(lock);
379 >    }
380  
381      /**
382       * Timed tryLock on a locked lock times out
383       */
384 <    public void testTryLock_Timeout() {
385 <        final ReentrantLock lock = new ReentrantLock();
386 <        lock.lock();
387 <        Thread t = new Thread(new Runnable() {
388 <                public void run() {
389 <                    try {
390 <                        threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
391 <                    } catch (Exception ex) {
392 <                        threadUnexpectedException();
393 <                    }
394 <                }
395 <            });
396 <        try {
397 <            t.start();
398 <            t.join();
348 <            lock.unlock();
349 <        } catch(Exception e){
350 <            unexpectedException();
351 <        }
352 <    }
353 <    
354 <    /**
355 <     * getHoldCount returns number of recursive holds
356 <     */
357 <    public void testGetHoldCount() {
358 <        ReentrantLock lock = new ReentrantLock();
359 <        for(int i = 1; i <= SIZE; i++) {
360 <            lock.lock();
361 <            assertEquals(i,lock.getHoldCount());
362 <        }
363 <        for(int i = SIZE; i > 0; i--) {
364 <            lock.unlock();
365 <            assertEquals(i-1,lock.getHoldCount());
366 <        }
384 >    public void testTryLock_Timeout()      { testTryLock_Timeout(false); }
385 >    public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
386 >    public void testTryLock_Timeout(boolean fair) {
387 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
388 >        lock.lock();
389 >        Thread t = newStartedThread(new CheckedRunnable() {
390 >            public void realRun() throws InterruptedException {
391 >                long startTime = System.nanoTime();
392 >                long timeoutMillis = 10;
393 >                assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS));
394 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
395 >            }});
396 >
397 >        awaitTermination(t);
398 >        releaseLock(lock);
399      }
400 <    
369 <  
400 >
401      /**
402 <     * isLocked is true when locked and false when not
402 >     * getHoldCount returns number of recursive holds
403       */
404 <    public void testIsLocked() {
405 <        final ReentrantLock lock = new ReentrantLock();
406 <        lock.lock();
407 <        assertTrue(lock.isLocked());
408 <        lock.unlock();
409 <        assertFalse(lock.isLocked());
410 <        Thread t = new Thread(new Runnable() {
411 <                public void run() {
412 <                    lock.lock();
413 <                    try {
414 <                        Thread.sleep(SMALL_DELAY_MS);
384 <                    }
385 <                    catch(Exception e) {
386 <                        threadUnexpectedException();
387 <                    }
388 <                    lock.unlock();
389 <                }
390 <            });
391 <        try {
392 <            t.start();
393 <            Thread.sleep(SHORT_DELAY_MS);
394 <            assertTrue(lock.isLocked());
395 <            t.join();
396 <            assertFalse(lock.isLocked());
397 <        } catch(Exception e){
398 <            unexpectedException();
404 >    public void testGetHoldCount()      { testGetHoldCount(false); }
405 >    public void testGetHoldCount_fair() { testGetHoldCount(true); }
406 >    public void testGetHoldCount(boolean fair) {
407 >        ReentrantLock lock = new ReentrantLock(fair);
408 >        for (int i = 1; i <= SIZE; i++) {
409 >            lock.lock();
410 >            assertEquals(i, lock.getHoldCount());
411 >        }
412 >        for (int i = SIZE; i > 0; i--) {
413 >            lock.unlock();
414 >            assertEquals(i-1, lock.getHoldCount());
415          }
416      }
417  
402
418      /**
419 <     * lockInterruptibly is interruptible.
419 >     * isLocked is true when locked and false when not
420       */
421 <    public void testLockInterruptibly1() {
422 <        final ReentrantLock lock = new ReentrantLock();
423 <        lock.lock();
409 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
421 >    public void testIsLocked()      { testIsLocked(false); }
422 >    public void testIsLocked_fair() { testIsLocked(true); }
423 >    public void testIsLocked(boolean fair) {
424          try {
425 <            t.start();
426 <            t.interrupt();
425 >            final ReentrantLock lock = new ReentrantLock(fair);
426 >            assertFalse(lock.isLocked());
427 >            lock.lock();
428 >            assertTrue(lock.isLocked());
429 >            lock.lock();
430 >            assertTrue(lock.isLocked());
431              lock.unlock();
432 <            t.join();
433 <        } catch(Exception e){
434 <            unexpectedException();
432 >            assertTrue(lock.isLocked());
433 >            lock.unlock();
434 >            assertFalse(lock.isLocked());
435 >            final CyclicBarrier barrier = new CyclicBarrier(2);
436 >            Thread t = newStartedThread(new CheckedRunnable() {
437 >                    public void realRun() throws Exception {
438 >                        lock.lock();
439 >                        assertTrue(lock.isLocked());
440 >                        barrier.await();
441 >                        barrier.await();
442 >                        lock.unlock();
443 >                    }});
444 >
445 >            barrier.await();
446 >            assertTrue(lock.isLocked());
447 >            barrier.await();
448 >            awaitTermination(t);
449 >            assertFalse(lock.isLocked());
450 >        } catch (Exception e) {
451 >            threadUnexpectedException(e);
452          }
453 <    }
453 >    }
454  
455      /**
456       * lockInterruptibly succeeds when unlocked, else is interruptible
457       */
458 <    public void testLockInterruptibly2() {
459 <        final ReentrantLock lock = new ReentrantLock();
460 <        try {
461 <            lock.lockInterruptibly();
427 <        } catch(Exception e) {
428 <            unexpectedException();
429 <        }
430 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
458 >    public void testLockInterruptibly()      { testLockInterruptibly(false); }
459 >    public void testLockInterruptibly_fair() { testLockInterruptibly(true); }
460 >    public void testLockInterruptibly(boolean fair) {
461 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
462          try {
463 <            t.start();
464 <            t.interrupt();
465 <            assertTrue(lock.isLocked());
435 <            assertTrue(lock.isHeldByCurrentThread());
436 <            t.join();
437 <        } catch(Exception e){
438 <            unexpectedException();
463 >            lock.lockInterruptibly();
464 >        } catch (InterruptedException ie) {
465 >            threadUnexpectedException(ie);
466          }
467 +        assertLockedByMoi(lock);
468 +        Thread t = newStartedThread(new InterruptedLockRunnable(lock));
469 +        waitForQueuedThread(lock, t);
470 +        t.interrupt();
471 +        assertTrue(lock.isLocked());
472 +        assertTrue(lock.isHeldByCurrentThread());
473 +        awaitTermination(t);
474 +        releaseLock(lock);
475      }
476  
477      /**
478       * Calling await without holding lock throws IllegalMonitorStateException
479       */
480 <    public void testAwait_IllegalMonitor() {
481 <        final ReentrantLock lock = new ReentrantLock();
480 >    public void testAwait_IMSE()      { testAwait_IMSE(false); }
481 >    public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
482 >    public void testAwait_IMSE(boolean fair) {
483 >        final ReentrantLock lock = new ReentrantLock(fair);
484          final Condition c = lock.newCondition();
485 +        long startTime = System.nanoTime();
486          try {
487 <            c.await();
488 <            shouldThrow();
489 <        }
490 <        catch (IllegalMonitorStateException success) {
491 <        }
492 <        catch (Exception ex) {
493 <            unexpectedException();
487 >            try {
488 >                c.await();
489 >                shouldThrow();
490 >            } catch (IllegalMonitorStateException success) {}
491 >            try {
492 >                c.await(LONG_DELAY_MS, MILLISECONDS);
493 >                shouldThrow();
494 >            } catch (IllegalMonitorStateException success) {}
495 >            try {
496 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
497 >                shouldThrow();
498 >            } catch (IllegalMonitorStateException success) {}
499 >            try {
500 >                c.awaitUninterruptibly();
501 >                shouldThrow();
502 >            } catch (IllegalMonitorStateException success) {}
503 >        } catch (InterruptedException ie) {
504 >            threadUnexpectedException(ie);
505          }
506 +        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
507      }
508  
509      /**
510       * Calling signal without holding lock throws IllegalMonitorStateException
511       */
512 <    public void testSignal_IllegalMonitor() {
513 <        final ReentrantLock lock = new ReentrantLock();
512 >    public void testSignal_IMSE()      { testSignal_IMSE(false); }
513 >    public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
514 >    public void testSignal_IMSE(boolean fair) {
515 >        final ReentrantLock lock = new ReentrantLock(fair);
516          final Condition c = lock.newCondition();
517          try {
518              c.signal();
519              shouldThrow();
520 <        }
469 <        catch (IllegalMonitorStateException success) {
470 <        }
471 <        catch (Exception ex) {
472 <            unexpectedException();
473 <        }
520 >        } catch (IllegalMonitorStateException success) {}
521      }
522  
523      /**
524       * awaitNanos without a signal times out
525       */
526 <    public void testAwaitNanos_Timeout() {
527 <        final ReentrantLock lock = new ReentrantLock();
528 <        final Condition c = lock.newCondition();
526 >    public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
527 >    public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
528 >    public void testAwaitNanos_Timeout(boolean fair) {
529          try {
530 +            final ReentrantLock lock = new ReentrantLock(fair);
531 +            final Condition c = lock.newCondition();
532              lock.lock();
533 <            long t = c.awaitNanos(100);
534 <            assertTrue(t <= 0);
533 >            long startTime = System.nanoTime();
534 >            long timeoutMillis = 10;
535 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
536 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
537 >            assertTrue(nanosRemaining <= 0);
538 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
539              lock.unlock();
540 <        }
541 <        catch (Exception ex) {
489 <            unexpectedException();
540 >        } catch (InterruptedException e) {
541 >            threadUnexpectedException(e);
542          }
543      }
544  
545      /**
546 <     *  timed await without a signal times out
546 >     * timed await without a signal times out
547       */
548 <    public void testAwait_Timeout() {
549 <        final ReentrantLock lock = new ReentrantLock();
550 <        final Condition c = lock.newCondition();
548 >    public void testAwait_Timeout()      { testAwait_Timeout(false); }
549 >    public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
550 >    public void testAwait_Timeout(boolean fair) {
551          try {
552 +            final ReentrantLock lock = new ReentrantLock(fair);
553 +            final Condition c = lock.newCondition();
554              lock.lock();
555 <            c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
555 >            long startTime = System.nanoTime();
556 >            long timeoutMillis = 10;
557 >            assertFalse(c.await(timeoutMillis, MILLISECONDS));
558 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
559              lock.unlock();
560 <        }
561 <        catch (Exception ex) {
505 <            unexpectedException();
560 >        } catch (InterruptedException e) {
561 >            threadUnexpectedException(e);
562          }
563      }
564  
565      /**
566       * awaitUntil without a signal times out
567       */
568 <    public void testAwaitUntil_Timeout() {
569 <        final ReentrantLock lock = new ReentrantLock();
570 <        final Condition c = lock.newCondition();
568 >    public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
569 >    public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
570 >    public void testAwaitUntil_Timeout(boolean fair) {
571          try {
572 +            final ReentrantLock lock = new ReentrantLock(fair);
573 +            final Condition c = lock.newCondition();
574              lock.lock();
575 +            long startTime = System.nanoTime();
576 +            long timeoutMillis = 10;
577              java.util.Date d = new java.util.Date();
578 <            c.awaitUntil(new java.util.Date(d.getTime() + 10));
578 >            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
579 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
580              lock.unlock();
581 <        }
582 <        catch (Exception ex) {
522 <            unexpectedException();
581 >        } catch (InterruptedException e) {
582 >            threadUnexpectedException(e);
583          }
584      }
585  
586      /**
587       * await returns when signalled
588       */
589 <    public void testAwait() {
590 <        final ReentrantLock lock = new ReentrantLock();
589 >    public void testAwait()      { testAwait(false); }
590 >    public void testAwait_fair() { testAwait(true); }
591 >    public void testAwait(boolean fair) {
592 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
593          final Condition c = lock.newCondition();
594 <        Thread t = new Thread(new Runnable() {
595 <                public void run() {
596 <                    try {
597 <                        lock.lock();
598 <                        c.await();
599 <                        lock.unlock();
600 <                    }
601 <                    catch(InterruptedException e) {
540 <                        threadUnexpectedException();
541 <                    }
542 <                }
543 <            });
594 >        final CountDownLatch locked = new CountDownLatch(1);
595 >        Thread t = newStartedThread(new CheckedRunnable() {
596 >            public void realRun() throws InterruptedException {
597 >                lock.lock();
598 >                locked.countDown();
599 >                c.await();
600 >                lock.unlock();
601 >            }});
602  
603 <        try {
604 <            t.start();
605 <            Thread.sleep(SHORT_DELAY_MS);
606 <            lock.lock();
607 <            c.signal();
608 <            lock.unlock();
609 <            t.join(SHORT_DELAY_MS);
610 <            assertFalse(t.isAlive());
553 <        }
554 <        catch (Exception ex) {
555 <            unexpectedException();
556 <        }
603 >        await(locked);
604 >        lock.lock();
605 >        assertHasWaiters(lock, c, t);
606 >        c.signal();
607 >        assertHasNoWaiters(lock, c);
608 >        assertTrue(t.isAlive());
609 >        lock.unlock();
610 >        awaitTermination(t);
611      }
612  
613      /**
614       * hasWaiters throws NPE if null
615       */
616 <    public void testHasWaitersNPE() {
617 <        final ReentrantLock lock = new ReentrantLock();
616 >    public void testHasWaitersNPE()      { testHasWaitersNPE(false); }
617 >    public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
618 >    public void testHasWaitersNPE(boolean fair) {
619 >        final ReentrantLock lock = new ReentrantLock(fair);
620          try {
621              lock.hasWaiters(null);
622              shouldThrow();
623 <        } catch (NullPointerException success) {
568 <        } catch (Exception ex) {
569 <            unexpectedException();
570 <        }
623 >        } catch (NullPointerException success) {}
624      }
625  
626      /**
627       * getWaitQueueLength throws NPE if null
628       */
629 <    public void testGetWaitQueueLengthNPE() {
630 <        final ReentrantLock lock = new ReentrantLock();
629 >    public void testGetWaitQueueLengthNPE()      { testGetWaitQueueLengthNPE(false); }
630 >    public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
631 >    public void testGetWaitQueueLengthNPE(boolean fair) {
632 >        final ReentrantLock lock = new ReentrantLock(fair);
633          try {
634              lock.getWaitQueueLength(null);
635              shouldThrow();
636 <        } catch (NullPointerException success) {
582 <        } catch (Exception ex) {
583 <            unexpectedException();
584 <        }
636 >        } catch (NullPointerException success) {}
637      }
638  
587
639      /**
640       * getWaitingThreads throws NPE if null
641       */
642 <    public void testGetWaitingThreadsNPE() {
643 <        final PublicReentrantLock lock = new PublicReentrantLock();
642 >    public void testGetWaitingThreadsNPE()      { testGetWaitingThreadsNPE(false); }
643 >    public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
644 >    public void testGetWaitingThreadsNPE(boolean fair) {
645 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
646          try {
647              lock.getWaitingThreads(null);
648              shouldThrow();
649 <        } catch (NullPointerException success) {
597 <        } catch (Exception ex) {
598 <            unexpectedException();
599 <        }
649 >        } catch (NullPointerException success) {}
650      }
651  
602
652      /**
653 <     * hasWaiters throws IAE if not owned
653 >     * hasWaiters throws IllegalArgumentException if not owned
654       */
655 <    public void testHasWaitersIAE() {
656 <        final ReentrantLock lock = new ReentrantLock();
657 <        final Condition c = (lock.newCondition());
658 <        final ReentrantLock lock2 = new ReentrantLock();
655 >    public void testHasWaitersIAE()      { testHasWaitersIAE(false); }
656 >    public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
657 >    public void testHasWaitersIAE(boolean fair) {
658 >        final ReentrantLock lock = new ReentrantLock(fair);
659 >        final Condition c = lock.newCondition();
660 >        final ReentrantLock lock2 = new ReentrantLock(fair);
661          try {
662              lock2.hasWaiters(c);
663              shouldThrow();
664 <        } catch (IllegalArgumentException success) {
614 <        } catch (Exception ex) {
615 <            unexpectedException();
616 <        }
664 >        } catch (IllegalArgumentException success) {}
665      }
666  
667      /**
668 <     * hasWaiters throws IMSE if not locked
668 >     * hasWaiters throws IllegalMonitorStateException if not locked
669       */
670 <    public void testHasWaitersIMSE() {
671 <        final ReentrantLock lock = new ReentrantLock();
672 <        final Condition c = (lock.newCondition());
670 >    public void testHasWaitersIMSE()      { testHasWaitersIMSE(false); }
671 >    public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
672 >    public void testHasWaitersIMSE(boolean fair) {
673 >        final ReentrantLock lock = new ReentrantLock(fair);
674 >        final Condition c = lock.newCondition();
675          try {
676              lock.hasWaiters(c);
677              shouldThrow();
678 <        } catch (IllegalMonitorStateException success) {
629 <        } catch (Exception ex) {
630 <            unexpectedException();
631 <        }
678 >        } catch (IllegalMonitorStateException success) {}
679      }
680  
634
681      /**
682 <     * getWaitQueueLength throws IAE if not owned
682 >     * getWaitQueueLength throws IllegalArgumentException if not owned
683       */
684 <    public void testGetWaitQueueLengthIAE() {
685 <        final ReentrantLock lock = new ReentrantLock();
686 <        final Condition c = (lock.newCondition());
687 <        final ReentrantLock lock2 = new ReentrantLock();
684 >    public void testGetWaitQueueLengthIAE()      { testGetWaitQueueLengthIAE(false); }
685 >    public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
686 >    public void testGetWaitQueueLengthIAE(boolean fair) {
687 >        final ReentrantLock lock = new ReentrantLock(fair);
688 >        final Condition c = lock.newCondition();
689 >        final ReentrantLock lock2 = new ReentrantLock(fair);
690          try {
691              lock2.getWaitQueueLength(c);
692              shouldThrow();
693 <        } catch (IllegalArgumentException success) {
646 <        } catch (Exception ex) {
647 <            unexpectedException();
648 <        }
693 >        } catch (IllegalArgumentException success) {}
694      }
695  
696      /**
697 <     * getWaitQueueLength throws IMSE if not locked
697 >     * getWaitQueueLength throws IllegalMonitorStateException if not locked
698       */
699 <    public void testGetWaitQueueLengthIMSE() {
700 <        final ReentrantLock lock = new ReentrantLock();
701 <        final Condition c = (lock.newCondition());
699 >    public void testGetWaitQueueLengthIMSE()      { testGetWaitQueueLengthIMSE(false); }
700 >    public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
701 >    public void testGetWaitQueueLengthIMSE(boolean fair) {
702 >        final ReentrantLock lock = new ReentrantLock(fair);
703 >        final Condition c = lock.newCondition();
704          try {
705              lock.getWaitQueueLength(c);
706              shouldThrow();
707 <        } catch (IllegalMonitorStateException success) {
661 <        } catch (Exception ex) {
662 <            unexpectedException();
663 <        }
707 >        } catch (IllegalMonitorStateException success) {}
708      }
709  
666
710      /**
711 <     * getWaitingThreads throws IAE if not owned
711 >     * getWaitingThreads throws IllegalArgumentException if not owned
712       */
713 <    public void testGetWaitingThreadsIAE() {
714 <        final PublicReentrantLock lock = new PublicReentrantLock();    
715 <        final Condition c = (lock.newCondition());
716 <        final PublicReentrantLock lock2 = new PublicReentrantLock();    
713 >    public void testGetWaitingThreadsIAE()      { testGetWaitingThreadsIAE(false); }
714 >    public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
715 >    public void testGetWaitingThreadsIAE(boolean fair) {
716 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
717 >        final Condition c = lock.newCondition();
718 >        final PublicReentrantLock lock2 = new PublicReentrantLock(fair);
719          try {
720              lock2.getWaitingThreads(c);
721              shouldThrow();
722 <        } catch (IllegalArgumentException success) {
678 <        } catch (Exception ex) {
679 <            unexpectedException();
680 <        }
722 >        } catch (IllegalArgumentException success) {}
723      }
724  
725      /**
726 <     * getWaitingThreads throws IMSE if not locked
726 >     * getWaitingThreads throws IllegalMonitorStateException if not locked
727       */
728 <    public void testGetWaitingThreadsIMSE() {
729 <        final PublicReentrantLock lock = new PublicReentrantLock();    
730 <        final Condition c = (lock.newCondition());
728 >    public void testGetWaitingThreadsIMSE()      { testGetWaitingThreadsIMSE(false); }
729 >    public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
730 >    public void testGetWaitingThreadsIMSE(boolean fair) {
731 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
732 >        final Condition c = lock.newCondition();
733          try {
734              lock.getWaitingThreads(c);
735              shouldThrow();
736 <        } catch (IllegalMonitorStateException success) {
693 <        } catch (Exception ex) {
694 <            unexpectedException();
695 <        }
736 >        } catch (IllegalMonitorStateException success) {}
737      }
738  
698
699
739      /**
740       * hasWaiters returns true when a thread is waiting, else false
741       */
742 <    public void testHasWaiters() {
743 <        final ReentrantLock lock = new ReentrantLock();
742 >    public void testHasWaiters()      { testHasWaiters(false); }
743 >    public void testHasWaiters_fair() { testHasWaiters(true); }
744 >    public void testHasWaiters(boolean fair) {
745 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
746          final Condition c = lock.newCondition();
747 <        Thread t = new Thread(new Runnable() {
748 <                public void run() {
749 <                    try {
750 <                        lock.lock();
751 <                        threadAssertFalse(lock.hasWaiters(c));
752 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
753 <                        c.await();
754 <                        lock.unlock();
755 <                    }
756 <                    catch(InterruptedException e) {
757 <                        threadUnexpectedException();
758 <                    }
718 <                }
719 <            });
747 >        final CountDownLatch locked = new CountDownLatch(1);
748 >        Thread t = newStartedThread(new CheckedRunnable() {
749 >            public void realRun() throws InterruptedException {
750 >                lock.lock();
751 >                assertHasNoWaiters(lock, c);
752 >                assertFalse(lock.hasWaiters(c));
753 >                locked.countDown();
754 >                c.await();
755 >                assertHasNoWaiters(lock, c);
756 >                assertFalse(lock.hasWaiters(c));
757 >                lock.unlock();
758 >            }});
759  
760 <        try {
761 <            t.start();
762 <            Thread.sleep(SHORT_DELAY_MS);
763 <            lock.lock();
764 <            assertTrue(lock.hasWaiters(c));
765 <            assertEquals(1, lock.getWaitQueueLength(c));
766 <            c.signal();
767 <            lock.unlock();
768 <            Thread.sleep(SHORT_DELAY_MS);
769 <            lock.lock();
731 <            assertFalse(lock.hasWaiters(c));
732 <            assertEquals(0, lock.getWaitQueueLength(c));
733 <            lock.unlock();
734 <            t.join(SHORT_DELAY_MS);
735 <            assertFalse(t.isAlive());
736 <        }
737 <        catch (Exception ex) {
738 <            unexpectedException();
739 <        }
760 >        await(locked);
761 >        lock.lock();
762 >        assertHasWaiters(lock, c, t);
763 >        assertTrue(lock.hasWaiters(c));
764 >        c.signal();
765 >        assertHasNoWaiters(lock, c);
766 >        assertFalse(lock.hasWaiters(c));
767 >        lock.unlock();
768 >        awaitTermination(t);
769 >        assertHasNoWaiters(lock, c);
770      }
771  
772      /**
773       * getWaitQueueLength returns number of waiting threads
774       */
775 <    public void testGetWaitQueueLength() {
776 <        final ReentrantLock lock = new ReentrantLock();
775 >    public void testGetWaitQueueLength()      { testGetWaitQueueLength(false); }
776 >    public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
777 >    public void testGetWaitQueueLength(boolean fair) {
778 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
779          final Condition c = lock.newCondition();
780 <        Thread t1 = new Thread(new Runnable() {
781 <                public void run() {
782 <                    try {
783 <                        lock.lock();
784 <                        threadAssertFalse(lock.hasWaiters(c));
785 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
786 <                        c.await();
787 <                        lock.unlock();
788 <                    }
789 <                    catch(InterruptedException e) {
790 <                        threadUnexpectedException();
791 <                    }
792 <                }
793 <            });
794 <
795 <        Thread t2 = new Thread(new Runnable() {
796 <                public void run() {
797 <                    try {
798 <                        lock.lock();
799 <                        threadAssertTrue(lock.hasWaiters(c));
800 <                        threadAssertEquals(1, lock.getWaitQueueLength(c));
769 <                        c.await();
770 <                        lock.unlock();
771 <                    }
772 <                    catch(InterruptedException e) {
773 <                        threadUnexpectedException();
774 <                    }
775 <                }
776 <            });
780 >        final CountDownLatch locked1 = new CountDownLatch(1);
781 >        final CountDownLatch locked2 = new CountDownLatch(1);
782 >        Thread t1 = new Thread(new CheckedRunnable() {
783 >            public void realRun() throws InterruptedException {
784 >                lock.lock();
785 >                assertFalse(lock.hasWaiters(c));
786 >                assertEquals(0, lock.getWaitQueueLength(c));
787 >                locked1.countDown();
788 >                c.await();
789 >                lock.unlock();
790 >            }});
791 >
792 >        Thread t2 = new Thread(new CheckedRunnable() {
793 >            public void realRun() throws InterruptedException {
794 >                lock.lock();
795 >                assertTrue(lock.hasWaiters(c));
796 >                assertEquals(1, lock.getWaitQueueLength(c));
797 >                locked2.countDown();
798 >                c.await();
799 >                lock.unlock();
800 >            }});
801  
802 <        try {
803 <            t1.start();
804 <            Thread.sleep(SHORT_DELAY_MS);
805 <            t2.start();
806 <            Thread.sleep(SHORT_DELAY_MS);
807 <            lock.lock();
808 <            assertTrue(lock.hasWaiters(c));
809 <            assertEquals(2, lock.getWaitQueueLength(c));
810 <            c.signalAll();
811 <            lock.unlock();
812 <            Thread.sleep(SHORT_DELAY_MS);
813 <            lock.lock();
814 <            assertFalse(lock.hasWaiters(c));
815 <            assertEquals(0, lock.getWaitQueueLength(c));
816 <            lock.unlock();
817 <            t1.join(SHORT_DELAY_MS);
818 <            t2.join(SHORT_DELAY_MS);
819 <            assertFalse(t1.isAlive());
820 <            assertFalse(t2.isAlive());
821 <        }
822 <        catch (Exception ex) {
823 <            unexpectedException();
824 <        }
802 >        lock.lock();
803 >        assertEquals(0, lock.getWaitQueueLength(c));
804 >        lock.unlock();
805 >
806 >        t1.start();
807 >        await(locked1);
808 >
809 >        lock.lock();
810 >        assertHasWaiters(lock, c, t1);
811 >        assertEquals(1, lock.getWaitQueueLength(c));
812 >        lock.unlock();
813 >
814 >        t2.start();
815 >        await(locked2);
816 >
817 >        lock.lock();
818 >        assertHasWaiters(lock, c, t1, t2);
819 >        assertEquals(2, lock.getWaitQueueLength(c));
820 >        c.signalAll();
821 >        assertHasNoWaiters(lock, c);
822 >        lock.unlock();
823 >
824 >        awaitTermination(t1);
825 >        awaitTermination(t2);
826 >
827 >        assertHasNoWaiters(lock, c);
828      }
829  
830      /**
831       * getWaitingThreads returns only and all waiting threads
832       */
833 <    public void testGetWaitingThreads() {
834 <        final PublicReentrantLock lock = new PublicReentrantLock();    
833 >    public void testGetWaitingThreads()      { testGetWaitingThreads(false); }
834 >    public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
835 >    public void testGetWaitingThreads(boolean fair) {
836 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
837          final Condition c = lock.newCondition();
838 <        Thread t1 = new Thread(new Runnable() {
839 <                public void run() {
840 <                    try {
841 <                        lock.lock();
842 <                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
843 <                        c.await();
844 <                        lock.unlock();
845 <                    }
846 <                    catch(InterruptedException e) {
847 <                        threadUnexpectedException();
848 <                    }
849 <                }
850 <            });
851 <
852 <        Thread t2 = new Thread(new Runnable() {
853 <                public void run() {
854 <                    try {
855 <                        lock.lock();
856 <                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
828 <                        c.await();
829 <                        lock.unlock();
830 <                    }
831 <                    catch(InterruptedException e) {
832 <                        threadUnexpectedException();
833 <                    }
834 <                }
835 <            });
838 >        final CountDownLatch locked1 = new CountDownLatch(1);
839 >        final CountDownLatch locked2 = new CountDownLatch(1);
840 >        Thread t1 = new Thread(new CheckedRunnable() {
841 >            public void realRun() throws InterruptedException {
842 >                lock.lock();
843 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
844 >                locked1.countDown();
845 >                c.await();
846 >                lock.unlock();
847 >            }});
848 >
849 >        Thread t2 = new Thread(new CheckedRunnable() {
850 >            public void realRun() throws InterruptedException {
851 >                lock.lock();
852 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
853 >                locked2.countDown();
854 >                c.await();
855 >                lock.unlock();
856 >            }});
857  
858 <        try {
859 <            lock.lock();
860 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
840 <            lock.unlock();
841 <            t1.start();
842 <            Thread.sleep(SHORT_DELAY_MS);
843 <            t2.start();
844 <            Thread.sleep(SHORT_DELAY_MS);
845 <            lock.lock();
846 <            assertTrue(lock.hasWaiters(c));
847 <            assertTrue(lock.getWaitingThreads(c).contains(t1));
848 <            assertTrue(lock.getWaitingThreads(c).contains(t2));
849 <            c.signalAll();
850 <            lock.unlock();
851 <            Thread.sleep(SHORT_DELAY_MS);
852 <            lock.lock();
853 <            assertFalse(lock.hasWaiters(c));
854 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
855 <            lock.unlock();
856 <            t1.join(SHORT_DELAY_MS);
857 <            t2.join(SHORT_DELAY_MS);
858 <            assertFalse(t1.isAlive());
859 <            assertFalse(t2.isAlive());
860 <        }
861 <        catch (Exception ex) {
862 <            unexpectedException();
863 <        }
864 <    }
858 >        lock.lock();
859 >        assertTrue(lock.getWaitingThreads(c).isEmpty());
860 >        lock.unlock();
861  
862 +        t1.start();
863 +        await(locked1);
864  
865 +        lock.lock();
866 +        assertHasWaiters(lock, c, t1);
867 +        assertTrue(lock.getWaitingThreads(c).contains(t1));
868 +        assertFalse(lock.getWaitingThreads(c).contains(t2));
869 +        assertEquals(1, lock.getWaitingThreads(c).size());
870 +        lock.unlock();
871 +
872 +        t2.start();
873 +        await(locked2);
874 +
875 +        lock.lock();
876 +        assertHasWaiters(lock, c, t1, t2);
877 +        assertTrue(lock.getWaitingThreads(c).contains(t1));
878 +        assertTrue(lock.getWaitingThreads(c).contains(t2));
879 +        assertEquals(2, lock.getWaitingThreads(c).size());
880 +        c.signalAll();
881 +        assertHasNoWaiters(lock, c);
882 +        lock.unlock();
883 +
884 +        awaitTermination(t1);
885 +        awaitTermination(t2);
886 +
887 +        assertHasNoWaiters(lock, c);
888 +    }
889  
890      /**
891       * awaitUninterruptibly doesn't abort on interrupt
892       */
893 <    public void testAwaitUninterruptibly() {
894 <        final ReentrantLock lock = new ReentrantLock();
893 >    public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
894 >    public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
895 >    public void testAwaitUninterruptibly(boolean fair) {
896 >        final ReentrantLock lock = new ReentrantLock(fair);
897          final Condition c = lock.newCondition();
898 <        Thread t = new Thread(new Runnable() {
899 <                public void run() {
900 <                    lock.lock();
901 <                    c.awaitUninterruptibly();
902 <                    lock.unlock();
903 <                }
904 <            });
898 >        final CountDownLatch locked = new CountDownLatch(1);
899 >        Thread t = newStartedThread(new CheckedRunnable() {
900 >            public void realRun() {
901 >                lock.lock();
902 >                locked.countDown();
903 >                c.awaitUninterruptibly();
904 >                assertTrue(Thread.interrupted());
905 >                lock.unlock();
906 >            }});
907  
908 <        try {
909 <            t.start();
910 <            Thread.sleep(SHORT_DELAY_MS);
911 <            t.interrupt();
912 <            lock.lock();
913 <            c.signal();
914 <            lock.unlock();
915 <            assert(t.isInterrupted());
916 <            t.join(SHORT_DELAY_MS);
917 <            assertFalse(t.isAlive());
892 <        }
893 <        catch (Exception ex) {
894 <            unexpectedException();
895 <        }
908 >        await(locked);
909 >        lock.lock();
910 >        lock.unlock();
911 >        t.interrupt();
912 >        long timeoutMillis = 10;
913 >        assertThreadStaysAlive(t, timeoutMillis);
914 >        lock.lock();
915 >        c.signal();
916 >        lock.unlock();
917 >        awaitTermination(t);
918      }
919  
920      /**
921 <     * await is interruptible
922 <     */
923 <    public void testAwait_Interrupt() {
924 <        final ReentrantLock lock = new ReentrantLock();
921 >     * await/awaitNanos/awaitUntil is interruptible
922 >     */
923 >    public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
924 >    public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
925 >    public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
926 >    public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
927 >    public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
928 >    public void testInterruptible_awaitUntil_fair() { testInterruptible(true,  AwaitMethod.awaitUntil); }
929 >    public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
930 >        final PublicReentrantLock lock =
931 >            new PublicReentrantLock(fair);
932          final Condition c = lock.newCondition();
933 <        Thread t = new Thread(new Runnable() {
934 <                public void run() {
935 <                    try {
936 <                        lock.lock();
937 <                        c.await();
938 <                        lock.unlock();
939 <                        threadShouldThrow();
940 <                    }
941 <                    catch(InterruptedException success) {
942 <                    }
943 <                }
944 <            });
945 <
946 <        try {
947 <            t.start();
948 <            Thread.sleep(SHORT_DELAY_MS);
949 <            t.interrupt();
950 <            t.join(SHORT_DELAY_MS);
951 <            assertFalse(t.isAlive());
952 <        }
953 <        catch (Exception ex) {
954 <            unexpectedException();
926 <        }
933 >        final CountDownLatch locked = new CountDownLatch(1);
934 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
935 >            public void realRun() throws InterruptedException {
936 >                lock.lock();
937 >                assertLockedByMoi(lock);
938 >                assertHasNoWaiters(lock, c);
939 >                locked.countDown();
940 >                try {
941 >                    await(c, awaitMethod);
942 >                } finally {
943 >                    assertLockedByMoi(lock);
944 >                    assertHasNoWaiters(lock, c);
945 >                    lock.unlock();
946 >                    assertFalse(Thread.interrupted());
947 >                }
948 >            }});
949 >
950 >        await(locked);
951 >        assertHasWaiters(lock, c, t);
952 >        t.interrupt();
953 >        awaitTermination(t);
954 >        assertNotLocked(lock);
955      }
956  
957      /**
958 <     * awaitNanos is interruptible
958 >     * signalAll wakes up all threads
959       */
960 <    public void testAwaitNanos_Interrupt() {
961 <        final ReentrantLock lock = new ReentrantLock();
960 >    public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
961 >    public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
962 >    public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
963 >    public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
964 >    public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
965 >    public void testSignalAll_awaitUntil_fair() { testSignalAll(true,  AwaitMethod.awaitUntil); }
966 >    public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
967 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
968          final Condition c = lock.newCondition();
969 <        Thread t = new Thread(new Runnable() {
970 <                public void run() {
971 <                    try {
972 <                        lock.lock();
973 <                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
974 <                        lock.unlock();
975 <                        threadShouldThrow();
976 <                    }
943 <                    catch(InterruptedException success) {
944 <                    }
945 <                }
946 <            });
947 <
948 <        try {
949 <            t.start();
950 <            Thread.sleep(SHORT_DELAY_MS);
951 <            t.interrupt();
952 <            t.join(SHORT_DELAY_MS);
953 <            assertFalse(t.isAlive());
954 <        }
955 <        catch (Exception ex) {
956 <            unexpectedException();
969 >        final CountDownLatch locked = new CountDownLatch(2);
970 >        class Awaiter extends CheckedRunnable {
971 >            public void realRun() throws InterruptedException {
972 >                lock.lock();
973 >                locked.countDown();
974 >                await(c, awaitMethod);
975 >                lock.unlock();
976 >            }
977          }
978 +
979 +        Thread t1 = newStartedThread(new Awaiter());
980 +        Thread t2 = newStartedThread(new Awaiter());
981 +
982 +        await(locked);
983 +        lock.lock();
984 +        assertHasWaiters(lock, c, t1, t2);
985 +        c.signalAll();
986 +        assertHasNoWaiters(lock, c);
987 +        lock.unlock();
988 +        awaitTermination(t1);
989 +        awaitTermination(t2);
990      }
991  
992      /**
993 <     * awaitUntil is interruptible
994 <     */
995 <    public void testAwaitUntil_Interrupt() {
996 <        final ReentrantLock lock = new ReentrantLock();
993 >     * signal wakes up waiting threads in FIFO order.
994 >     */
995 >    public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
996 >    public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
997 >    public void testSignalWakesFifo(boolean fair) {
998 >        final PublicReentrantLock lock =
999 >            new PublicReentrantLock(fair);
1000          final Condition c = lock.newCondition();
1001 <        Thread t = new Thread(new Runnable() {
1002 <                public void run() {
1003 <                    try {
1004 <                        lock.lock();
1005 <                        java.util.Date d = new java.util.Date();
1006 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1007 <                        lock.unlock();
1008 <                        threadShouldThrow();
1009 <                    }
1010 <                    catch(InterruptedException success) {
1011 <                    }
1012 <                }
1013 <            });
1001 >        final CountDownLatch locked1 = new CountDownLatch(1);
1002 >        final CountDownLatch locked2 = new CountDownLatch(1);
1003 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1004 >            public void realRun() throws InterruptedException {
1005 >                lock.lock();
1006 >                locked1.countDown();
1007 >                c.await();
1008 >                lock.unlock();
1009 >            }});
1010 >
1011 >        await(locked1);
1012 >
1013 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1014 >            public void realRun() throws InterruptedException {
1015 >                lock.lock();
1016 >                locked2.countDown();
1017 >                c.await();
1018 >                lock.unlock();
1019 >            }});
1020  
1021 <        try {
1022 <            t.start();
1023 <            Thread.sleep(SHORT_DELAY_MS);
1024 <            t.interrupt();
1025 <            t.join(SHORT_DELAY_MS);
1026 <            assertFalse(t.isAlive());
1027 <        }
1028 <        catch (Exception ex) {
1029 <            unexpectedException();
1030 <        }
1021 >        await(locked2);
1022 >
1023 >        lock.lock();
1024 >        assertHasWaiters(lock, c, t1, t2);
1025 >        assertFalse(lock.hasQueuedThreads());
1026 >        c.signal();
1027 >        assertHasWaiters(lock, c, t2);
1028 >        assertTrue(lock.hasQueuedThread(t1));
1029 >        assertFalse(lock.hasQueuedThread(t2));
1030 >        c.signal();
1031 >        assertHasNoWaiters(lock, c);
1032 >        assertTrue(lock.hasQueuedThread(t1));
1033 >        assertTrue(lock.hasQueuedThread(t2));
1034 >        lock.unlock();
1035 >        awaitTermination(t1);
1036 >        awaitTermination(t2);
1037      }
1038  
1039      /**
1040 <     * signalAll wakes up all threads
1041 <     */
1042 <    public void testSignalAll() {
1043 <        final ReentrantLock lock = new ReentrantLock();
1040 >     * await after multiple reentrant locking preserves lock count
1041 >     */
1042 >    public void testAwaitLockCount()      { testAwaitLockCount(false); }
1043 >    public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1044 >    public void testAwaitLockCount(boolean fair) {
1045 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
1046          final Condition c = lock.newCondition();
1047 <        Thread t1 = new Thread(new Runnable() {
1048 <                public void run() {
1049 <                    try {
1050 <                        lock.lock();
1051 <                        c.await();
1052 <                        lock.unlock();
1053 <                    }
1054 <                    catch(InterruptedException e) {
1055 <                        threadUnexpectedException();
1056 <                    }
1057 <                }
1058 <            });
1059 <
1060 <        Thread t2 = new Thread(new Runnable() {
1061 <                public void run() {
1062 <                    try {
1063 <                        lock.lock();
1064 <                        c.await();
1065 <                        lock.unlock();
1066 <                    }
1067 <                    catch(InterruptedException e) {
1068 <                        threadUnexpectedException();
1069 <                    }
1070 <                }
1071 <            });
1047 >        final CountDownLatch locked = new CountDownLatch(2);
1048 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1049 >            public void realRun() throws InterruptedException {
1050 >                lock.lock();
1051 >                assertLockedByMoi(lock);
1052 >                assertEquals(1, lock.getHoldCount());
1053 >                locked.countDown();
1054 >                c.await();
1055 >                assertLockedByMoi(lock);
1056 >                assertEquals(1, lock.getHoldCount());
1057 >                lock.unlock();
1058 >            }});
1059 >
1060 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1061 >            public void realRun() throws InterruptedException {
1062 >                lock.lock();
1063 >                lock.lock();
1064 >                assertLockedByMoi(lock);
1065 >                assertEquals(2, lock.getHoldCount());
1066 >                locked.countDown();
1067 >                c.await();
1068 >                assertLockedByMoi(lock);
1069 >                assertEquals(2, lock.getHoldCount());
1070 >                lock.unlock();
1071 >                lock.unlock();
1072 >            }});
1073  
1074 <        try {
1075 <            t1.start();
1076 <            t2.start();
1077 <            Thread.sleep(SHORT_DELAY_MS);
1078 <            lock.lock();
1079 <            c.signalAll();
1080 <            lock.unlock();
1081 <            t1.join(SHORT_DELAY_MS);
1082 <            t2.join(SHORT_DELAY_MS);
1033 <            assertFalse(t1.isAlive());
1034 <            assertFalse(t2.isAlive());
1035 <        }
1036 <        catch (Exception ex) {
1037 <            unexpectedException();
1038 <        }
1074 >        await(locked);
1075 >        lock.lock();
1076 >        assertHasWaiters(lock, c, t1, t2);
1077 >        assertEquals(1, lock.getHoldCount());
1078 >        c.signalAll();
1079 >        assertHasNoWaiters(lock, c);
1080 >        lock.unlock();
1081 >        awaitTermination(t1);
1082 >        awaitTermination(t2);
1083      }
1084  
1085      /**
1086       * A serialized lock deserializes as unlocked
1087       */
1088 <    public void testSerialization() {
1089 <        ReentrantLock l = new ReentrantLock();
1090 <        l.lock();
1091 <        l.unlock();
1088 >    public void testSerialization()      { testSerialization(false); }
1089 >    public void testSerialization_fair() { testSerialization(true); }
1090 >    public void testSerialization(boolean fair) {
1091 >        ReentrantLock lock = new ReentrantLock(fair);
1092 >        lock.lock();
1093  
1094 <        try {
1095 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1096 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1097 <            out.writeObject(l);
1098 <            out.close();
1099 <
1100 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1101 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1102 <            ReentrantLock r = (ReentrantLock) in.readObject();
1103 <            r.lock();
1104 <            r.unlock();
1105 <        } catch(Exception e){
1106 <            e.printStackTrace();
1107 <            unexpectedException();
1108 <        }
1094 >        ReentrantLock clone = serialClone(lock);
1095 >        assertEquals(lock.isFair(), clone.isFair());
1096 >        assertTrue(lock.isLocked());
1097 >        assertFalse(clone.isLocked());
1098 >        assertEquals(1, lock.getHoldCount());
1099 >        assertEquals(0, clone.getHoldCount());
1100 >        clone.lock();
1101 >        clone.lock();
1102 >        assertTrue(clone.isLocked());
1103 >        assertEquals(2, clone.getHoldCount());
1104 >        assertEquals(1, lock.getHoldCount());
1105 >        clone.unlock();
1106 >        clone.unlock();
1107 >        assertTrue(lock.isLocked());
1108 >        assertFalse(clone.isLocked());
1109      }
1110  
1111      /**
1112       * toString indicates current lock state
1113       */
1114 <    public void testToString() {
1115 <        ReentrantLock lock = new ReentrantLock();
1114 >    public void testToString()      { testToString(false); }
1115 >    public void testToString_fair() { testToString(true); }
1116 >    public void testToString(boolean fair) {
1117 >        ReentrantLock lock = new ReentrantLock(fair);
1118          String us = lock.toString();
1119          assertTrue(us.indexOf("Unlocked") >= 0);
1120          lock.lock();
1121          String ls = lock.toString();
1122          assertTrue(ls.indexOf("Locked") >= 0);
1123      }
1077
1124   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines